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;
}
Error: in printf
Error: in emp int xx;
No error.
None of these.
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 3 of 3.

Vasantha deepika said:   1 decade ago
& operator should not be used in printf() statement .

Ravi said:   1 decade ago
Here & in printf is absolutely correct as it is going to print the address of a.

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.

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.

Mithilesh Upadhyay said:   1 decade ago
I think the address operator '&' should not be in the Printf() statement.

Swetha said:   1 decade ago
Can any one explain in detail?

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?

Rohit said:   1 decade ago
In case of printf statmnt.

Can we write ampersand sign not?

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.

Revathy said:   1 decade ago
struct emp xx;

What does tis statement do?


Post your comments here:

Your comments will be displayed after verification.