'Is x=; and if(d<) invalid to use in c language
At the end of this question there is Mid-Point circle drawing algorithm in c language. I saw the following two statements in the code
x=;
if(d<);
in the program online and in our book as well, so basically i thought that the compiler might automatically be initializing them to 0(zero) so i wrote the same code in our homework. But the teacher said that it is wrong and indeed it gives Invalid Expression in Turbo C++ compiler. Thats why I want to know if it is wrong then why is it written like this everywhere.
Here is the code :
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main() {
int gd=DETECT,gm;
int i,r,x,y,xc,yc;
float d;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\");
printf("Enter Radius\n");
scanf("%d",&r);
printf("Enter Center of circle\n");
scanf("%d",&xc);
scanf("%d",&yc);
d=1.25-r;
x=;
y=r;
do {
if(d<) {
x=x+1;
d=d+2*x+1;
} else {
x=x+1;
y=y-1;
d=d+2*x-2*y+10;
}
putpixel(xc+x,yc+y,5);
putpixel(xc-y,yc-x,5);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc+x,5);
putpixel(xc+y,yc+x,5);
putpixel(xc-x,yc-y,5);
putpixel(xc+x,yc-y,5);
putpixel(xc-x,yc+y,5);
} while(x<y);
getch();
}
Also it is the link of above code:
Mid Point Circle Drawing Algorithm Code in c Language
Please help me out. Thanks.
Solution 1:[1]
Ques. What is storage class of variable x?
ans. local variables i.e. var x used in a block statement( as you used inside the main() ) is belong to "AUTO" storage class in C.
One More Ques :-
Now, question arises in you mind that, what is the default value with ,auto variable initialised with**??**
Ans. Initialized with "Garbage Value".
In declaration of x:-
int i,r,x,y,xc,yc;
Note:- x is initialized with garbage value.
but you write,
x=;
Note:- which is niether the declaration nor the initilaization statement,
but it is the Incomplete statement which'll not understand by the compilor so it gives the error :
Invalid Expression
Now, come to your next doubt:-
if(d<);
it's also a invalid expresion because just think in general maths if you compute:- if 2 is greater than ______??
can you imagine the value in blank space?? yes, you can but, compilor(or computer) can'not :)
Solution 2:[2]
No, those expressions are not syntactically valid.
Books have typos, and fishy-looking reproductions of books online also have typos. That image looks like a cell phone photo or something, really dubious.
Just because something is said online, it doesn't have to be true1.
1 Said an anonymous stranger online. :)
Solution 3:[3]
The code on the link you provided has obviously been mutilated.
The correct code is here:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
void main()
{
int gd = DETECT, gm;
int r, x, y, p, xc = 320, yc = 240;
initgraph(&gd, &gm, "C:\\TC\\BGI");
cleardevice();
printf("Enter the radius ");
scanf("%d", &r);
x = 0;
y = r;
putpixel(xc + x, yc - y, 1);
p = 3 - (2 * r);
for (x = 0; x <= y; x++)
{
if (p<0)
{
y = y;
p = (p + (4 * x) + 6);
}
else
{
y = y - 1;
p = p + ((4 * (x - y) + 10));
}
putpixel(xc + x, yc - y, 1);
putpixel(xc - x, yc - y, 2);
putpixel(xc + x, yc + y, 3);
putpixel(xc - x, yc + y, 4);
putpixel(xc + y, yc - x, 5);
putpixel(xc - y, yc - x, 6);
putpixel(xc + y, yc + x, 7);
putpixel(xc - y, yc + x, 8);
}
getch();
closegraph();
}
Note: this code as it stands here works only on the antique Turbo C. For other platforms adaptions need to be made.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Shivam Sharma |
| Solution 2 | unwind |
| Solution 3 |
