4. int calculateHeight( Node *root)
{
if(root == NULL)
return 0;
int leftHeight = calculateHeight(root->left);
int rightHeight = calculateHeight (root->right);
return max(leftHeight , rightHeight) + 1;
}
5. FILE1.C
int x = 5;
void print( )
int main( )
{
print( );
printf(“Main x = %d n”,x);
}
FILE2.C
extern int x;
void print( )
{
printf(“Print : x = %d n”,x);
}
6. FILE1.C
extern int x;
void print( );
int main( )
{
printf(“Main x = %d n”,x);
print( );
}
FILE2.C
int x = 5;
void print( )
{
printf(“Print : x = %d n”,x);
}
7. Learning : We require one definition and a declaration in
other file(s). It does not matter which file contains the
definition.
8. FILE1.C
int x = 8;
void print( );
int main( )
{
printf(“Main x = %d n”,x);
print( );
}
FILE2.C
int x = 5;
void print( )
{
printf(“Print : x = %d n”,x);
}
9. Multiple Definition Error.
An external declaration for an object is a
definition if it has an initializer (Refer K&R
A10.2 ).
As both have initialization, both are
definition.
We will get Linker Error (not a compile-time
error)
10. FILE1.C
static int x;
void print( );
int main( )
{
printf(“Main x = %d n”,x);
print( );
}
FILE2.C
int x = 5;
void print( )
{
printf(“Print : x = %d n”,x);
}
11. X in file1 has internal linkage and is not visible
during linking of multiple files.
No error of multiple definition.
File1 will access the variable x defined in file1,
as it is not initialized, the default value is 0.
File2 will access the variable x defined in file2,
whose value is 5
12. FILE1.C
static int x = 8;
void print( );
int main( )
{
extern int x;
printf(“Main x = %d n”,x);
print( );
}
Output is 8.
Extern says that the variable
is defined externally, within
the same file / other file. As
an external definition is
found, it refers to it.
13. FILE1.C
int x ;
void print( );
int main( )
{
printf(“Main x = %d n”,x);
print( );
}
FILE2.C
int x = 5;
void print( )
{
printf(“Print : x = %d n”,x);
}
14. An external object declaration that does not have an
initializer, and does not contain the extern specifier, is a
tentative definition
If a definition for an object appears , any tentative definitions
are treated merely as redundant declarations. If no definition
for the object appears, all its tentative definitions become a
single definition with initializer 0.
The declaration of x in file1 is tentative definition;
As the definition for x is found in file2, x in file1 is treated as
declaration only.
15. FILE1.C
int x ;
void print( );
int main( )
{
printf(“Main x = %d n”,x);
print( );
printf(“Main x = %d n”,x);
}
FILE2.C
int x;
void print( )
{
x = 5;
printf(“Print : x = %d n”,x);
}
40. static int x = 5;
int main( )
{
int x;
printf(“x = %d n”,x);
}
41. static int x = 5;
int main( )
{
extern int x;
printf(“x = %d n”,x);
}
42. int a = 5;
int b;
static int c = 7;
static int d;
int main( )
{
}
• Mention the memory segments of
variables after compiling (.o file) and
after linking (a.out)
• What is the difference between a
and c.
• Use nm utility to view the memory
segments
$gcc –c file.c
$nm file.o
$gcc file.c
$nm ./a.out
43. void func( );
int x;
int main( )
{
x = 5;
func( );
printf(“x = %d “ , x);
}
int x;
void func( )
{
x = 10;
}
44. [sikander@localhost ~]$ gcc -c f1.c
[sikander@localhost ~]$ gcc -c f2.c
[sikander@localhost ~]$ nm f1.o
U func
00000000 T main
U printf
00000004 C x
[sikander@localhost ~]$ nm f2.o
00000000 T func
00000004 C x
[sikander@localhost ~]$ gcc f1.o f2.o
[sikander@localhost ~]$ nm a.out
080495ac B x
45. void func( );
int x = 5;
int main( )
{
func( );
printf(“x = %d “ , x);
}
int x = 10;
void func( )
{
x++;
}
46. [sikander@localhost ~]$ gcc -c f1.c
[sikander@localhost ~]$ gcc -c f2.c
[sikander@localhost ~]$ nm f1.o
00000000 D x
[sikander@localhost ~]$ nm f2.o
00000000 D x
[sikander@localhost ~]$ gcc f1.o f2.o
f2.o(.data+0x0): multiple definition of `x'
f1.o(.data+0x0): first defined here
collect2: ld returned 1 exit status
47. C C
B
C D
D
D D
Multiple Definition
d d
No Conflict, two different variables
b b
No Conflict, two different variables
d b
No Conflict, two different variables
b d
No Conflict, two different variables
b D
No Conflict, two different variables
48. int x ; //C
int main( )
{
printf(“Main &x= %p” , &x);
printf(“x = %d n”, x);
func( );
}
int x ; //C
void func( )
{
printf(“Func &x= %p” , &x);
printf(“x = %d n”, x);
}
int x ; //C
int main( )
{
printf(“Main &x= %p” , &x);
printf(“x = %d n”, x);
func( );
}
int x = 5 ; //D
void func( )
{
printf(“Func &x= %p” , &x);
printf(“x = %d n”, x);
}
49. void func( );
int x = 5; //D
int main( )
{
printf(“Main &x= %p” , &x);
printf(“x = %d n”, x);
func( );
}
int x = 10; //D
void func( )
{
printf(“Func &x= %p” , &x);
printf(“x = %d n”, x);
}
void func( );
static int x = 5; //d
int main( )
{
printf(“Main &x= %p” , &x);
printf(“x = %d n”, x);
func( );
}
int x = 10; //D
void func( )
{
printf(“Func &x= %p” , &x);
printf(“x = %d n”, x);
}
50. const int x = 10; //Read Only
int main( )
{
const int y = 5; //Stack
}
51. const int x = 10; //Read Only
int main( )
{
const int y = 5; //Stack
printf(“Enter the value for local const variable : “);
scanf(“ %d”,&y);
printf(“Local Const = %d n” , y);
printf(“Enter the value for global const variable : “);
scanf(“ %d”,&x);
printf(“Global Const = %d n”,x);
}
[sikander@localhost ~]$ ./a.out
Enter the value for local const variable : 89
Local Const = 89
Enter the value for global const variable : 6
Segmentation fault
52. [sikander@localhost ~]$ nm f1.o
00000000 t display
0000000a T main
00000005 T print
static void display()
{
}
void print()
{
}
int main( )
{
display( );
print( );
}