Thursday, 19 June 2014

#15. Concepts in C -- only through Examples & Explanations

This is a continuation of last post. 

Before seeing the answer try to predict it & then see the explanation.

All the Best!!

36) #include<stdio.h>
main()
{
struct xx
{
int x=3;
char name[]="hello";
};
struct xx *s=malloc(sizeof(struct xx));
printf("%d",s->x);
printf("%s",s->name);
}

37) #include<stdio.h>
main()
{
struct xx

{
int x;
struct yy
{
char s;
struct xx *p;
};
struct yy *q;
};
}

38) main()
{
extern int i;
i=20;
printf("%d",sizeof(i));
}

44) main()
{
printf("%d", out);
}
int out=100;

40) main()
{
extern out;
printf("%d", out);
}
int out=100;

========================================================
36.
Answer:
Compiler Error
Explanation:
Initialization should not be done for structure members inside the structure
declaration

37.
Answer:
Compiler Error
Explanation:
in the end of nested structure yy a member have to be declared.

38.
Answer:
Linker error: undefined symbol '_i'.
Explanation:
extern declaration specifies that the variable i is defined somewhere else.
The compiler passes the external variable to be resolved by the linker. So
compiler doesn't find an error. During linking the linker searches for the
definition of i. Since it is not found the linker flags an error.

39.
Answer:
Compiler error: undefined symbol out in function main.

Explanation:
The rule is that a variable is available for use from the point of declaration.
Even though a is a global variable, it is not available for main. Hence an
error.

40.
Answer:
100
Explanation:

This is the correct way of writing the previous program.

To be continued...
========================================================

Thanks for coming till this point!!
VSG
SQL DBA

No comments:

Post a Comment