int *pi;
char *pc;
short int *psi;
printf(“ %d n”,sizeof(pi));
printf(“ %d n”,sizeof(pc));
printf(“ %d n”,sizeof(psi));
int *pi;
char *pc;
short int *psi;
printf(“ %d n”,sizeof(*pi));
printf(“ %d n”,sizeof(*pc));
printf(“ %d n”,sizeof(*psi));
int *pi = (int *)2000;
char *pc = (char *)2000;
pi++;
pc++;
printf(“ %u %u “ , pi , pc);
int x = 0x1234;
char c = x;
printf(“ %x “ , c);
const int x = 10;
1.int * const ptr1 = &x;
2.int const * ptr2 = &x;
3.const int * ptr3 = &x;
void update(int *p)
{
*p = *p + 5;
}
int main( )
{
int x = 10;
int *ptr = &x;
update(ptr);
printf(“x = %d “ , x);
}
void update(int *p)
{
p = p + 1;
}
int main( )
{
int arr[ ] = {10 , 12 , 25 , 45};
int *ptr = arr;
update(ptr);
printf(“*ptr = %d “ , *ptr);
}
void update(int *p)
{
*p = *p + 1;
}
int main( )
{
int arr[ ] = {10 , 12 , 25 , 45};
int *ptr = arr;
update(ptr);
printf(“*ptr = %d “ , *ptr);
}
void update(char *str)
{
str = “Devendra”;
}
int main( )
{
char *name = “Nimisha”;
update(name);
printf(“ %s n” , name);
}
void update(char *str)
{
str[0] = ‘H’;
}
int main( )
{
char name[ ] = “Nimisha”;
char *ptr = name;
update(ptr);
printf(“ %s n” , name);
}
void update(char *str)
{
*++str = ‘a’;
}
int main( )
{
char name[ ] = “Nimisha”;
char *ptr = name;
update(ptr);
printf(“ %s n” , name);
}
int main( )
{
char names[]
[10]={“OBAMA”,”PUTIN”,”MODI”,”CAMEROON”};
printf(“ %__ “ , *(names + 2) + 3);
printf(“ %__ “ , **(names + 2) + 3);
}
int main( )
{
char names[][10]={“OBAMA”,”PUTIN”,”MODI”,”CAMEROON”};
1. printf(“ %s “ , *(++names));
2. printf(“ %s “ , ++*(names));
3. printf(“ %c “ , ++**(names));
}
int main( )
{
char *names[10]={“OBAMA”,”PUTIN”,”MODI”,”CAMEROON”};
1. printf(“ %s “ , *(++names));
2. printf(“ %s “ , ++*(names));
3. printf(“ %c “ , ++**(names));
}
struct Student
{
int id;
char name[20];
};
int main( )
{
struct Student s1 = {1 , “Ayushi”};
struct Student s2 = {2 , “Ayushi”};
if(s1.name == s2.name)
printf(“Equal”);
else
printf(“Not Equal”);
}
char *names[ ] = {“Nimisha”,”Devender”,”Vikram”,”Balwant”};
printf(“ %d “ , sizeof(names));
printf(“ %d “ , sizeof(names[0]));
char names[ ][10] = {“Nimisha”,”Devender”,”Vikram”,”Balwant”};
printf(“ %d “ , sizeof(names));
printf(“ %d “ , sizeof(names[0]));
void display( char *names[ ])
{
printf(“Display : %d n “ , sizeof(names));
}
int main( )
{
char *names[ ] = {“Nimisha”,”Devender”,”Vikram”,”Balwant”};
printf(“Main : %d n“ , sizeof(names));
display( names );
}
static int x = 5;
int main( )
{
int x;
printf(“x = %d n”,x);
}
static int x = 5;
int main( )
{
extern int x;
printf(“x = %d n”,x);
}
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
void func( );
int x;
int main( )
{
x = 5;
func( );
printf(“x = %d “ , x);
}
int x;
void func( )
{
x = 10;
}
[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
void func( );
int x = 5;
int main( )
{
func( );
printf(“x = %d “ , x);
}
int x = 10;
void func( )
{
x++;
}
 [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
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
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);
}
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);
}
const int x = 10; //Read Only
int main( )
{
const int y = 5; //Stack
}
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
 [sikander@localhost ~]$ nm f1.o
 00000000 t display
 0000000a T main
 00000005 T print
static void display()
{
}
void print()
{
}
int main( )
{
display( );
print( );
}

C questions

  • 2.
    int *pi; char *pc; shortint *psi; printf(“ %d n”,sizeof(pi)); printf(“ %d n”,sizeof(pc)); printf(“ %d n”,sizeof(psi)); int *pi; char *pc; short int *psi; printf(“ %d n”,sizeof(*pi)); printf(“ %d n”,sizeof(*pc)); printf(“ %d n”,sizeof(*psi));
  • 3.
    int *pi =(int *)2000; char *pc = (char *)2000; pi++; pc++; printf(“ %u %u “ , pi , pc);
  • 4.
    int x =0x1234; char c = x; printf(“ %x “ , c);
  • 5.
    const int x= 10; 1.int * const ptr1 = &x; 2.int const * ptr2 = &x; 3.const int * ptr3 = &x;
  • 6.
    void update(int *p) { *p= *p + 5; } int main( ) { int x = 10; int *ptr = &x; update(ptr); printf(“x = %d “ , x); }
  • 7.
    void update(int *p) { p= p + 1; } int main( ) { int arr[ ] = {10 , 12 , 25 , 45}; int *ptr = arr; update(ptr); printf(“*ptr = %d “ , *ptr); }
  • 8.
    void update(int *p) { *p= *p + 1; } int main( ) { int arr[ ] = {10 , 12 , 25 , 45}; int *ptr = arr; update(ptr); printf(“*ptr = %d “ , *ptr); }
  • 9.
    void update(char *str) { str= “Devendra”; } int main( ) { char *name = “Nimisha”; update(name); printf(“ %s n” , name); }
  • 10.
    void update(char *str) { str[0]= ‘H’; } int main( ) { char name[ ] = “Nimisha”; char *ptr = name; update(ptr); printf(“ %s n” , name); }
  • 11.
    void update(char *str) { *++str= ‘a’; } int main( ) { char name[ ] = “Nimisha”; char *ptr = name; update(ptr); printf(“ %s n” , name); }
  • 12.
    int main( ) { charnames[] [10]={“OBAMA”,”PUTIN”,”MODI”,”CAMEROON”}; printf(“ %__ “ , *(names + 2) + 3); printf(“ %__ “ , **(names + 2) + 3); }
  • 13.
    int main( ) { charnames[][10]={“OBAMA”,”PUTIN”,”MODI”,”CAMEROON”}; 1. printf(“ %s “ , *(++names)); 2. printf(“ %s “ , ++*(names)); 3. printf(“ %c “ , ++**(names)); }
  • 14.
    int main( ) { char*names[10]={“OBAMA”,”PUTIN”,”MODI”,”CAMEROON”}; 1. printf(“ %s “ , *(++names)); 2. printf(“ %s “ , ++*(names)); 3. printf(“ %c “ , ++**(names)); }
  • 15.
    struct Student { int id; charname[20]; }; int main( ) { struct Student s1 = {1 , “Ayushi”}; struct Student s2 = {2 , “Ayushi”}; if(s1.name == s2.name) printf(“Equal”); else printf(“Not Equal”); }
  • 16.
    char *names[ ]= {“Nimisha”,”Devender”,”Vikram”,”Balwant”}; printf(“ %d “ , sizeof(names)); printf(“ %d “ , sizeof(names[0])); char names[ ][10] = {“Nimisha”,”Devender”,”Vikram”,”Balwant”}; printf(“ %d “ , sizeof(names)); printf(“ %d “ , sizeof(names[0]));
  • 17.
    void display( char*names[ ]) { printf(“Display : %d n “ , sizeof(names)); } int main( ) { char *names[ ] = {“Nimisha”,”Devender”,”Vikram”,”Balwant”}; printf(“Main : %d n“ , sizeof(names)); display( names ); }
  • 18.
    static int x= 5; int main( ) { int x; printf(“x = %d n”,x); }
  • 19.
    static int x= 5; int main( ) { extern int x; printf(“x = %d n”,x); }
  • 20.
    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
  • 21.
    void func( ); intx; int main( ) { x = 5; func( ); printf(“x = %d “ , x); } int x; void func( ) { x = 10; }
  • 22.
    [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
  • 23.
    void func( ); intx = 5; int main( ) { func( ); printf(“x = %d “ , x); } int x = 10; void func( ) { x++; }
  • 24.
     [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
  • 25.
    C C B C D D DD 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
  • 26.
    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); }
  • 27.
    void func( ); intx = 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); }
  • 28.
    const int x= 10; //Read Only int main( ) { const int y = 5; //Stack }
  • 29.
    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
  • 30.
     [sikander@localhost ~]$nm f1.o  00000000 t display  0000000a T main  00000005 T print static void display() { } void print() { } int main( ) { display( ); print( ); }