c programming practice questions

C Programming Practice Questions

c programming practice questions

What is the output of the following code? 

1.

#include<stdio.h>

main()

{

int i;

for(i=0;i<5;i++)

{

static int a=0;

int b=0;

a++;

b++;

printf(“\na=%d”,a);

printf(“,b=%d”,b);

}

return 0;

}

Ans:

a=1

b=1

a=2

b=1

a=3

b=1

a=4

b=1

a=5

b=1

2.

#include<stdio.h>

main()

{

static int s;

++s;

printf(“\n%d”,s);

if(s<=3)

main();

printf(“\n%d”,s);

return 0;

}

Ans: 1 2 3 4 4 4 4 4

3.

#include<stdio.h>

extern int a;

main()

{

printf(“\na=%d”,a);

return 0;

}

Ans: Linking Error undefined symbol _a

4.

#include<stdio.h>

int a;

main()

{

printf(“\n a = %d”,a);

return 0;

}

Ans: a=0

5.

#include<stdio.h>

extern int a;

main()

{

printf(“\n a = %d”,a);

return 0;

}int a;

Ans: a=0

6.

#include<stdio.h>

extern int a;

main()

{

printf(“\n a = %d”,a);

return 0;

}

int a=5;

Ans: a=5

7.

#include<stdio.h>

extern int a=5;

main()

{

void fun();

printf(“\n a = %d”,a);

fun();

return 0;

}

int a;

void fun()

{

printf(“\n in fun a = %d”,a);

}

Ans: a=5,a=5

8.

#include<stdio.h>

extern int a;

main()

{

void fun();

printf(“\n a = %d”,a);

fun();

return 0;

}

int a=7;

void fun()

{

printf(“\n in fun a = %d”,a);

}

Ans: a=7,a=7

9.

#include<stdio.h>

extern int a=5;

main()

{

void fun();

printf(“\n a = %d”,a);

fun();

return 0;

}

int a;

void fun()

{

printf(“\n in fun a = %d”,a);

}

Ans: a=5,a=5

10.

#include<stdio.h>

void fun(int _)

{

printf(“%d”,_);

}

main()

{

fun(23);

return 0;

}

Ans: 23

11.

#include<stdio.h>

main()

{

auto a;

register r;

static s;

extern e;

printf(“\n%d”,sizeof a);

printf(“\n%d”,sizeof r);

printf(“\n%d”,sizeof s);

printf(“\n%d”,sizeof e);

return 0;

}

Ans: 2 2 2 2

Identify the compiler error in the following code?

#include<stdio.h>

void fun(auto int _)

{

printf(“%d”,_);

}

main()

{

fun(23);

return 0;   }

Ans: Error

Identify the compiler error in the following code?

#include<stdio.h>

void fun(static int _)

{

printf(“%d”,_);

}

main()

{

fun(23);

return 0;

}

Ans: Error

Identify the compiler error in the following code?

#include<stdio.h>

void fun( extern int _)

{

printf(“%d”,_);

}

main()

{

fun(23);

return 0;

}

Ans: Error

Identify the compiler error in the following code?

#include<stdio.h>

void fun(register int _)

{

printf(“%d”,_);

}

main()

{

fun(23);

return 0;

}

Ans: 23

Identify the compiler error in the following code?

#include<stdio.h>

void fun(typedef int _)

{

printf(“%d”,_);

}

main()

{

fun(23);

return 0;

}

Ans: Error

Identify the error in the following code?
static extern

int a=5;

static int b = 6;

main()

{

printf(“\n%d”,a); printf(“\n%d”,b);

return 0;

}

Ans: 5 6
Identify the error in the following code?

#include<stdio.h>

main()

{

extern int a=5;

printf(“\n %d”,a);

return 0;

}

Ans: 5

Is it possible to declare static function in c language?

Ans: yes it become file scope function

Can we return more than one value by using return statement?

Ans: No

How to return more than one value from a function?

Ans: call by address

Can we use more than one return statement in one function?

Ans: yes

Give an example for function returning void but contains return statement?

Ans:

void abc()

{

printf(“Hello abc”);

return;

}

void main()

{

abc();

}

What is the differences between K&R C style and ANSI C style?

Ans:

K&R-C

int sum(a,b) int a; int b;

{

return (a+b);

}

ANSII-C

int sum(int a,int b)

{

return (a+b);

}

Write a small ‘c’ function to receive any number of arguments?

Ans:   Argument list concept

What is the output of the following program?

main()

{

int *p1;

float *p2;

double *p3;

char *p4;

printf(“\n%u”,sizeof(p1));

printf(“\n%u”,sizeof(p2));

printf(“\n%u”,sizeof(p3));

printf(“\n%u”,sizeof(p4));

return 0;

}

Ans: 2 2 2 2

What is the output of the following program?

main()

{

int far *p1;

float far *p2;

double far *p3;

char far *p4;

printf(“\n%u”,sizeof(p1));

printf(“\n%u”,sizeof(p2));

printf(“\n%u”,sizeof(p3));

printf(“\n%u”,sizeof(p4));

return 0;

}

Ans: 4 4 4 4

What is the output of the following program?

main()

{

int *p;

int a=5;

*p++;

printf(“%d”,a);

return 0;

}
Ans: 5

Join Our WhatsApp Channel Join Now
Join Our Telegram Group Join Now

Leave a Comment