C Programming - Typedef - Discussion

Discussion Forum : Typedef - Find Output of Program (Q.No. 5)
5.
What will be the output of the program?
#include<stdio.h>

typedef struct error {int warning, err, exception;} ERROR;
int main()
{
    ERROR e;
    e.err=1;
    printf("%d\n", e.err);
    return 0;
}
0
1
2
Error
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
14 comments Page 1 of 2.

Rohan said:   7 years ago
#include<stdio.h>
typedef struct error {int warning, err, exception;} ERROR;

/* typedef is used to rename. Here the name of the struct is changed from error to ERROR.............. */
int main()
{
ERROR e;
/* e is the object of ERROR structure ... */
e.err=1;
/* the value of err has been initialized with the help of object to the value 1 */

printf("%d\n", e.err);
/* 1 is the output............ */

return 0;
}

ElemenT said:   1 decade ago
@Everyone.

Guys don't confuse here with error and ERROR these two are different things error will act like struct keyword because you used typedef with it. ERROR is your actual structure for which your creating e as a reference so whenever you wrote e. Err = 1, it will store 1 at e. Err address and then it is just printing that value in printf.

I hope everyone got it.

Bharath kumar said:   7 years ago
@ALL.

As per my knowledge;

#include<stdio.h>

typedef struct error {int warning, err, exception;} ERROR;
int main()
{
ERROR e; /*typedef is used to rename. So (ERROR) as changed to (struct error) */
/* the above line code (ERROR e;) as changed to (struct error e;)*/

e.err=1;
printf("%d\n", e.err);
return 0;
}

Saravanan.C said:   1 decade ago
typedef struct error
{
int warning;
int err;
int exception;
}ERROR;

This is how this normally looks....

Here a structure named error is typedefed as ERROR(means in place of struct error it can be said as ERROR)...
then e is a structure..
e.err is accessing a member in the structure... thats it.... is it correct......?

Karim said:   1 decade ago
Like, int itemp;
itemp =1;

typedef int myinteger;

myinteger imytemp; we cannot store value in to int or myinteger.

Same like structure.

Shreyas said:   1 decade ago
@ Sathya
We are Typedefin the structure error to ERROR.
Therefore ERROR is acting like struct error.
ERROR e; is the same as struct error e;

Sathya said:   1 decade ago
In c how can u create an object like ERROR e; ? Is it correct?

Instead of using e.err , why cant we simply use ERROR.err?

Nagaraj said:   1 decade ago
Hey in the structure, how can we declare like that ?

Every variablemust be declared separately, am I right?

Mohan said:   1 decade ago
@Nagraj.

Generally we declare every variable separately. But it is not a necessary condition.

Hiren Raiyani said:   1 decade ago
Because its simple structure to give ERROR name and we initialize that so 1 is answer.


Post your comments here:

Your comments will be displayed after verification.