C Programming - Declarations and Initializations - Discussion
Discussion Forum : Declarations and Initializations - Point Out Errors (Q.No. 3)
3.
Point out the error in the following program.
#include<stdio.h>
struct emp
{
char name[20];
int age;
};
int main()
{
emp int xx;
int a;
printf("%d\n", &a);
return 0;
}
Answer: Option
Explanation:
There is an error in the line emp int xx;
To overcome this error, remove the int and add the struct at the begining of emp int xx;
#include<stdio.h>
struct emp
{
char name[20];
int age;
};
int main()
{
struct emp xx;
int a;
printf("%d\n", &a);
return 0;
}
Discussion:
30 comments Page 1 of 3.
Revathy said:
1 decade ago
struct emp xx;
What does tis statement do?
What does tis statement do?
Sandeep said:
1 decade ago
It creates a structure variable xx of type emp.
Then we can access the members of the structure by that variable as xx.name and xx.age.
Then we can access the members of the structure by that variable as xx.name and xx.age.
Rohit said:
1 decade ago
In case of printf statmnt.
Can we write ampersand sign not?
Can we write ampersand sign not?
Shankar said:
1 decade ago
Then what about ampersand sign in printf?
Ampersand shows address, then %u want to be used in case of %d,
Why printf statement cannot be wrong?
Ampersand shows address, then %u want to be used in case of %d,
Why printf statement cannot be wrong?
Swetha said:
1 decade ago
Can any one explain in detail?
Mithilesh Upadhyay said:
1 decade ago
I think the address operator '&' should not be in the Printf() statement.
Rupinderjit said:
1 decade ago
%u--->to print unsigned number.
%p--->to print Segment:offset address combination(as it is in host computer(different on different machines).
%x--->to print address in hex.
%o--->to print address in octal.
%p--->to print Segment:offset address combination(as it is in host computer(different on different machines).
%x--->to print address in hex.
%o--->to print address in octal.
Raghav Nagnanathan said:
1 decade ago
The ampersand(&) sign must be only used in a scanf() statement.
Eg. scanf("%d",&a);
For printf statement, there is no need for the ampersand sign
Eg. printf("%d",a);
Another very important note for ampersand sign is that there is no necessity of it for accepting a character.
eg. scanf("%c",a); instead of scanf("%c",&a);
I am not saying that using the ampersand for accepting a character is wrong, but the code will work without the ampersand as well.
Eg. scanf("%d",&a);
For printf statement, there is no need for the ampersand sign
Eg. printf("%d",a);
Another very important note for ampersand sign is that there is no necessity of it for accepting a character.
eg. scanf("%c",a); instead of scanf("%c",&a);
I am not saying that using the ampersand for accepting a character is wrong, but the code will work without the ampersand as well.
Ravi said:
1 decade ago
Here & in printf is absolutely correct as it is going to print the address of a.
Vasantha deepika said:
1 decade ago
& operator should not be used in printf() statement .
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers