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.
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.
Balki said:
1 decade ago
The & symbol here will print the address of the variable 'a' and there is nothing wrong in using it in a printf statement.
Here it has been mentioned just to confuse us.
The real error lies in the declaration of structure.
Here it has been mentioned just to confuse us.
The real error lies in the declaration of structure.
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.
Mahi said:
8 years ago
The normal declaration of structure is:
Struct emp xx;
Coming to printf statement it gives the address of 'a' in negative because pointer is unsigned integer.
Struct emp xx;
Coming to printf statement it gives the address of 'a' in negative because pointer is unsigned integer.
Harsh said:
8 years ago
@Sesi Praveen.
'a' is not initialized. The Compiler will take by the default value for a in print statement. The Main error is in emp int xx statement.
'a' is not initialized. The Compiler will take by the default value for a in print statement. The Main error is in emp int xx statement.
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?
Vasudev said:
8 years ago
Generally just after declaration of a variable, no memory will be allotted to the variable so, how can it print the address of the variable a?
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.
Shrutika said:
3 years ago
Why we're taking %d as a format specifier because actually we want the address to get printed So, we must use %p as a format specifier.
(2)
Karthik said:
8 years ago
The struct emp xx also doesn't work as it gives an error "two or more data types in declaration of xx"
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers