C Linux interview questions and answers

C Linux interview questions and answers

(1)What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
int main(){
    int a=5;
    printf("%d %d %d",a++,a++,++a);
    return 0;
}

Output:
In LINUX GCC compiler
7 6 8
In TURBO C
7 6 6

Hints: In Turbo c parameter is passed from right to left in printf function but not in the Linux.


(2)What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
int main(){
    int a=5,b=10,c=15,d=20;
    printf("%d %d %d");
        return 0;
}

Output:
In LINUX GCC compiler
Garbage values
In TURBO C
5 10 15

Hints: Local variables stores in the stack.

(3) What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
int main(){
    int i=5,j=5,y;
    int x=++i + ++i + ++i;
    y=++j + ++j + ++j;
    printf("%d  %d  %d %d",x,y,i,j);
    return 0;
}

Output:
In LINUX GCC compiler
22  22  8  8
In TURBO C
21  24  8  8

(4) What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
int main(){
        int  near *p;
        int  far *q;
        int  huge *r;
        printf("%d  %d  %d",sizeof(p),sizeof(q),sizeof(r));
        return 0;
}

Output:
In LINUX GCC compiler
Compilation error
In TURBO C
2  4  4

Note: In Linux there is not any concept of near, far and huge pointers

(5) What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
int main(){
    char *p;
    int *q;
    float **r;
    printf("%d  %d  %d",sizeof(p),sizeof(q),sizeof(r));
    return 0;
}

Output:
In LINUX GCC compiler
4  4  4
In TURBO C
2  2  2

Hints: size of any type of pointer in Linux is 4 and in turbo c is 2.

(6) What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
int main(){
    short int a=5;
    int b=5;
    long int c=5l;
    float d=5.0f;
    double e=5.0;
    long double f=5.0L;
    char g='5';
    printf("Size of short int: %d\n",sizeof(a));
    printf("Size of int: %d\n",sizeof(b));
    printf("Size of long int: %d\n",sizeof(c));
    printf("Size of float: %d\n",sizeof(d));
    printf("Size of double: %d\n",sizeof(e));
    printf("Size of long double: %d\n",sizeof(f));
    printf("Size of char: %d\n",sizeof(g));
        return 0;
}

Output:
In LINUX GCC compiler
Size of short int: 2
Size of int: 4
Size of long int: 4
Size of float: 4
Size of double: 8
Size of long double: 12
Size of char: 1
In TURBO C
Size of short int: 2
Size of int: 2
Size of long int: 4
Size of float: 4
Size of double: 8
Size of long double: 10
Size of char: 1

(7) What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
    int main(){
        int a=300;
    char *p=(char *)&a;
        printf("%d\n",*p);
    printf("%d",*++p);
        return 0;
}

Output:
In LINUX GCC compiler
44
1
In TURBO C
44
1

(8) What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
int main(){
    char c='A';
    printf("%d   %d",sizeof(c),sizeof('A'));
    return 0;
}

Output:
In LINUX
1  4
In TURBO C
1  2

(9) What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
int main(){
    enum color{RED,BLUE,GREEN=-2,YELLOW,PINK};
    printf("%d  %d",BLUE,PINK);
    return 0;
}

Output:
In LINUX GCC compiler
1 0
In TURBO C
1 0

(10) What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
int main(){
    char c=127;
    printf("%d",++c);
    printf("  %d",++c);
    return 0;
}

Output:
In LINUX GCC compiler
-128 -127
In TURBO C
-128 -127
Hints: char data type cyclic property.

(11) What will be output if you will execute following program by gcc compiler in Linux?

#include"stdio.h"
struct info1{
    char *title;
    long int size;
    double grade;
}hero1;
union info2{
        char *title;
        long int size;
        double grade;
}hero2;
int main(){
    printf("Size of structure: %d\n",sizeof(hero1));
    printf("Size of union:  %d",sizeof(hero2));
    return 0;
}

Output:
In LINUX GCC compiler
Size of structure: 16
Size of union:  8
In TURBO C
Size of structure: 14
Size of union:  8

(12) What will be output if you will execute following program by gcc compiler in Linux?

#define size(x) (char *)(x+1)-(char *)x
#include<stdio.h>
int main(){
    long int *p;
    long double *q;
        printf("Size of long int: %d\n",size(p));
    printf("Size of long double: %d",size(q));
        return 0;
}

Output:
In LINUX GCC compiler
Size of long int: 4
Size of long double: 12
In TURBO C
Size of long int: 4
Size of long double: 10

(13) What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
int main(){
      int i=2,j=5,k=3;
      int a=i&&j>=k;
      printf("%d",a);
      return 0;
}

Output:
In LINUX GCC compiler
1
In TURBO C
1
Hints: Any conditional or relational operator returns 1 if condition is true otherwise it returns 0.



If you have any queries or suggestions in above c Linux interview questions, please share it.

20 comments:

Imran Mirza said...

Very Useful Tutorial

Anonymous said...

why answer of 1st question is 7 6 8 in linux . what is the concept behind it.

Bijoooos said...

(a++, a++, ++a) means it starts from right-side , ++a will increment a value and replaces the final value of a, now a=6 and next expr a++ increment a and replaces previous value of a ( 6 ), now a= 7, next a++ also increment a and replaces previous value of a (7), now a=8, that value in place of ++a,
whenever ++a is coming all will replaced by final value of a

kibrom gebre said...

would you clarify me why the output of question number 3 is 22 22 8.Thanks!

Anonymous said...

Could any one please clarify the post fix and pre fix argument evaluation .

Anonymous said...

it is good.

Pradeep G said...

Could any one please clarify the post fix and pre fix argument evaluation . qn (3)

Anonymous said...

it's like a magic:P
again x is incremented by 1;
if you scan the number i and j then it will not incremented..
test it :D

Yogesh Jain said...

hey dear there is a matter of sequence point in c..
whenever you perform many operation on a single variable in one statement the the output is undefined..

i have one conclusion
i = 5;
x = ++i + ++i ++i ;
unary operator evalute right to left so
first time i becomes 6
then 7
but here is interesting thing is that i value of first RHS depends on second RHS so.

answer will be like this..

x = 8 + 7 + 7 = 22.

test different test you will find my calculation correct ... :)

Yogesh Jain said...

hey dear there is a matter of sequence point in c..
whenever you perform many operation on a single variable in one statement the the output is undefined..

i have one conclusion
i = 5;
x = ++i + ++i ++i ;
unary operator evalute right to left so
first time i becomes 6
then 7
but here is interesting thing is that i value of first RHS depends on second RHS so.

answer will be like this..

x = 8 + 7 + 7 = 22.

test different test you will find my calculation correct ... :)

Yogesh Jain said...

hey dear
i tell you about prefix and postfix ..

(1) prefix :- first increment after then use..
(2)postfix :- first use after then increment..

example for prefix
#include
int main()
{
int i = 5;
printf("%d\n",++i);// here i incrementing and then printing so output is 6
return 0;
}
output - 6

example for postfix
#include
int main()
{
int i = 5;
printf("%d\n",i++); // here we using i but i not increment because it is not using
return 0; // anywhere else
}

output - 5 .

Arivazhagan P said...

But some c heaterfiles are not run.
Ex:
#include
#include

kmlrob said...

Hello Mr. Programmer I have a question for you:
int i=2;
printf("%d",(++i)+(i)+(++i));

whenever i change the the value of (i) to take the samples the program output goes out of expectations.
can you evaluate this expression by taking different values of (i) and also by changing order of precedence of (i).
thankyou

Hanish Maghu said...

please read k&r...it says the behaviour of such an expression is undefined....

Anonymous said...

i think u've to evaluate from left to right.
in this case x= 7+7+8;
if its from right to left , j=5;;y=--j + ++j + ++j; should give value of j=20;
but output=16; 5+5+6;

pankaj verma said...

does printf() calls the system function : write()?
[fwrite,fputs and fputs uses write() system function and I know working of these but how printf uses write() system call ? ]
anybody explain plzz.. thanks

Anonymous said...

does anyone explain the answer of question no 3
that why what is reason of 24 in answer sequence

Kiran Bhosale said...

the printf function eventually ends up calling write system call . you can check that using strace command
e. g . strace ./a.out . it will give the trace of all systems calls called by a program

Unknown said...

OMG so interesting questions I like it <3.

RAJ KUMAR said...

Input:1a2b3c
Output: 123
abc