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

In Turbo C, parameters are passed from right to left in the printf function, whereas in Linux, the standard convention is to pass parameters from left to right.


(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 are stored on the stack in C programming.

(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, the concepts of near, far, and huge pointers, commonly used in some other systems, are not applicable

(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: In Linux, the size of any type of pointer is typically 4 bytes, while in Turbo C, it is 2 bytes.

(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: In C programming, conditional and relational operators return 1 if the condition is true; otherwise, they return 0.



If you have any questions or suggestions regarding the C/Linux interview topics mentioned above, please feel free to share them.

24 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.

Biju MK 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

Unknown 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

Unknown 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 ... :)

Unknown 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 ... :)

Unknown 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 .

Unknown 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

Unknown 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;

Unknown 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

Unknown 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.

Anonymous said...

Input:1a2b3c
Output: 123
abc

Unknown said...

anyone explain the answer of following program

#include
int main(){
int a=500;
char *p=(char *)&a;
printf("%d\n",*p);
printf("%d",*++p);
return 0;
}

Vijayakash said...


Awesome Blog!!! Thanks for it, it is more useful for us.


ethical hacking books
best hacking books
python interview questions and answers
aws interview questions and answers
devops interview questions and answers
pega interview questions
seo interview questions

Rupesh Kumar said...

This is my first time i visit here and I found so many interesting stuff in your blog. Discover top-quality online tuition for GCSE classes with Ziyyar Edutech. Our dedicated platform offers comprehensive GCSE online tuition, providing students with expert guidance and support.
For more info visit Tuition for GCSE classes

Rupesh Kumar said...

This Information Very Helpful to everyone. online English tuition program – where language evolution begins at home. Ziyyara’s meticulously crafted curriculum, led by experienced tutors, caters to learners of all levels.
For more info visit Online english tuition