003 # puzzle on c
By dholey
@dholey (1383)
India
January 20, 2007 12:58pm CST
/* FIND OUT THE ERRORS AND CORRECT THEM */
struct STUDENT
{
char nm[20];
int age=17;
}student;
void main()
{
printf("\n enter name ");
gets(nm);
printf(" YOUR NAME IS %S ",nm);
}
4 responses
@swaroop_sv2003 (531)
• India
21 Jan 07
I almost lost touch with C and C++ . I am not sure what i provide below is correct. Anyway i am just giving it a try. Here is the corrected code.
struct STUDENT
{
char nm[20];
int age;
}student;
void main()
{
printf("\n enter name ");
gets(student.nm);
printf(" YOUR NAME IS%S ",student.nm);
}
Is that correct?
1 person likes this
@swaroop_sv2003 (531)
• India
21 Jan 07
Thanks dholey. Sure i would check out other puzzles and would try to take part in it.
@tmaheshwari (170)
• India
22 Jan 07
Ohh !! i m late... swaroop has already done everything... :) ha.haa Just fun....
well... i have a another point, which may not be related to this problem but still important..
if u run you program on g++ (or any ANSI standard compiler) then this will give u compilation errorr.... why ????
see the first line ... "void main()' it means... main() can not return any value... In ANSI, this is wrong...
this program will compile on Microsoft VC6 or VC7 ... but not on g++.
so we have to write it like 'int main()'
and in the last line 'return errorCode' :)
Weell... guys.. keep it up these discussions.. really intresting.... good job.
@swaroop_sv2003 (531)
• India
22 Jan 07
I hope you would be early next time ;). Ok i too found out this problem with gcc. I actually don't think it is an error with gcc compiler. According to latest ANSI standards I think we need to return an integer to a main(). So the above program should be
struct STUDENT
{
char nm[20];
int age;
}student;
int main()
{
printf("\n enter name ");
gets(student.nm);
printf(" YOUR NAME IS%s ",student.nm);
return 0;
}
in order to be executed with gcc.
@swaroop_sv2003 (531)
• India
22 Jan 07
Did u use the same program that i have listed above? Any way it is working for me. I am using the latest gcc version that came with opensuse 10.2. Maybe some bug with your version. Would you mind posting those errors here?
@tmaheshwari (170)
• India
22 Jan 07
have u tried it with g++. Which version of gcc u r using?
After ur comment, i run a sample program and it is giving me 'error' with g++?
I think we have different versions of compiler...
@nihalnihal (660)
• India
20 Jan 07
I guess you can't initialise a variable inside a data structure. Is it correct? I am not sure because I am onto java..Thanks
@stonehr (818)
• Croatia (Hrvatska)
21 Jan 07
I'm not so much coding in C language but this seems to me to be an error:
}student; I don't see what 'student;' do here after closed bracket?.. Only if you are going to call Sub code But I guess that must be done with some prefix, like ' &student; ' .