Sunday, 25 October 2009

C PROGRAMMING TUTORIAL

INDEX
Chapter 1 Memory map
Chapter 2 Data type
Chapter 3 Variables in c
Chapter 4 Operators and expression
Chapter 5 Control structure if else and switch case
Chapter 6 Looping in c for, while, do while
Chapter 7 Array
Chapter 8 Pointer
Chapter 9 String
Chapter 10 Functions
Chapter 11 C Preprocessor
Chapter 12 Structures
Chapter 13 Union
Chapter 14 File handling
Chapter 15 Input output
Chapter 16 Command line arguments
Chapter 17 Advance C
Chapter 18 Traditional questions of c programming
Chapter 19 Quiz in C
Chapter 20 Data structure
Chapter 21 Objective questions of c proramming
Chapter 22 Mutiple choice questions of c
Chapter 23 Test your self
Chapter 24 Assembly language programming in c

c programming quiz with solution

Time: 6 minute
-------------------------------------------------

(1)

void main(){

float a=30.3f;

int y=5;

clrscr();

printf("%d",a%y);

getch();

}

What will be output when you will compile above code?

(a) 6

(b) 6.0

(c) 7

(d) Compiler error

(e) None of these
-------------------------------------------------
(2)

void main(){

int x=5;

float r=5.0;

clrscr();

printf("%x %X %o",sizeof(x,r),sizeof(r,x),sizeof(sizeof(5.0)));

getch();

}

What will be output when you will compile above code?

(a) 4 2 2

(b) 2 4 4

(c) 2 4 2

(d) Compiler error

(e) None of these
-------------------------------------------------
(3)

void main(){

int y=15,z=25;

function(&y,&z);

clrscr();

printf("%d\t%d",z,y);

getch();

}

function (int *p,int *q){

return(*p=(*p+*q)-(*q=*p));

}

What will be output when you will compile above code?

(a) 25 15

(b) 15 25

(c) 25 25

(d) Compiler error

(e) None of these
-------------------------------------------------
(4)

void main(){

int a=-1;

static int count;

clrscr();

while(a){

count++;

a&=a-1;

}

printf("%d",count);

getch();

}

What will be output when you will compile above code?

(a) 15

(b) 16

(c) 17

(d) Compiler error

(e) None of these
-------------------------------------------------
(5)

void main(){

typedef float arr[10];

arr b={2,4,6};

clrscr();

printf("%d",sizeof(b));

getch();

}

What will be output when you will compile above code?

(a) 40

(b) 12

(c) 4

(d) Compiler error

(e) None of these
-------------------------------------------------
(6)

void change(int const *p){

*((int *)p)=20;

}

void main(){

int const x=10;

change(&x);

clrscr();

printf("%d",x);

getch();

}

What will be output when you will compile above code?

(a) 10

(b) 20

(c) 30

(d) Compiler error

(e) None of these




Solution:

(1)(d) Modular division is only possible for integral data type

(2)(a)

(3)(b)

(4)(b)

(5)(a)

(6)(b)

Note: If you want to explanation of any questions you can ask through comment.

MCQ Questions in c

C ,C++, SQL,PL/SQL Search Engine
-------------------------------------------------
Custom Search
Java,Advance Java,Java script and ebook Search Engine

Big collection of c and java questions
Explanations of questions in easily way
MCQ Questions in c

-------------------------------------------------
(1)
void main(){
clrscr();
printf("%d",sizeof(3.8));
getch();
}

Which of the following is true?
(a)4
(b)8
(c)10
(d)Compiler error
(e)None of these

-------------------------------------------------
(2)
void main(){
char *str1="powla";
char *str2="er";
clrscr();
printf("%s\b\b%s",str1,str2);
getch();
}

Which of the following is true?
(a)powlaer
(b)pow
(c)power
(d)Compiler error
(e)None of these
-------------------------------------------------
(3)
void main(){
int a=270;
char *p;
p=(char *)&a;
clrscr();
printf("%d",*p);
getch();
}
Which of the following is true?
(a)270
(b)address of variable a
(c)16
(d)Compiler error
(e)None of these
-------------------------------------------------
(4)
What is missing statement of in the following program?
void main(){
int sort(int,int);
int I;
i=sort(5,6);
}
int sort(int a,int b){
int c;
c=a;
a=b;
b=c;
return a;
}
-------------------------------------------------
(5)Write following in term of if and else:
void main(){
int a=1,b=2,c=3;
clrscr();
if(a==5&&b==6&&c==7)
printf("india");
else
printf("pak");
getch();
}
-------------------------------------------------
(6)
Draw memory representation of
struct xxx{
char a;
int b;
char c;
};
-------------------------------------------------
(7)
Write the following program in term of switch and case?
void main()
{
int a=3;
if(x>2){
printf(“INDIA IS BEST”);
}
else{
printf(“PAK IS BEST”);
}
}
-------------------------------------------------
(8)
void main(){
int far *a=(int far*)0x50000011;
int far *b=(int far*)0x50010001;
int huge *c=(int huge*)0x50000011;
int huge *d=(int huge*)0x50010001;
clrscr();
if(a==b)
printf("I know C");
else
printf("I don't know C");
if(c==d)
printf("\nI know C");
else
printf("\nI don't know C");
getch();
}
Which of the following is true?
(a)I know C
I Know C
(b)I know C
I don’t know C
(c)I don’t know C
I know C
(d)Compiler error
(e)None of these
-------------------------------------------------
(9)
#define power(a) #a
void main(){
clrscr();
printf("%d",*power(432));
getch();
}
Which of the following is true?
(a)*”432”
(b)432
(c)16
(d)32
(e)Compiler error
-------------------------------------------------
(10)
void main(){
int arr[]={1,2,3,4,5,6};
void xxx(int[5]);
xxx(arr);
getch();
}
void xxx(int ch[5]){
clrscr();
printf("%d",-1[ch]);
}
Which of the following true?
(a)2
(b)-2
(c)3
(d)-3
(e)Compiler error
-------------------------------------------------
(11)
What is difference between a, b, c and in following declaration ?
#define xxx char *
typedef char * yyy;
void main(){
yyy a,b;
xxx c,d;
}
-------------------------------------------------
(12)
Write a c program to find the HCF of any two numbers?
-------------------------------------------------
(13)
void main(){
int a=5;{
a++;
}
clrscr();
printf("%d",a);
getch();
}
Which of the following is true?
(a)5
(b)6
(c)7
(d)Compiler error
(e)None of these
-------------------------------------------------
(14)
void main(){
int a=5;{
int a=7;
a++;
printf(“%d”,a);
}
clrscr();
printf("%d",a);
getch();
}
Which of the following is true?
(a)5 7
(b)5 8
(c)8 5
(d)7 5
(e)Compiler error

-------------------------------------------------
-------------------------------------------------
Answer:
(1)(b)
(2)(c)
(3)(c)
(4) See in explanation.
(5) See in explanation.
(6) See in explanation.
(7) See in explanation.
(8)(c)
(9)(c)
(10)(b)
(11) See in explanation.
(12) See in explanation.
(13)(b)
(14)(c)
-------------------------------------------------
Explanation:
-------------------------------------------------
(1) 3.8f is float constant, 3.8 is double constant and 3.8L is long double constant .Here are finding size of double constantan which is 8.
(2) \b escape sequence back the cursor one position left .We are using two /b so after writing str1 cursor is at the position of l of powal .So when it write er it will override the la so output will be power.
(4) Function sort returning a value but we are not using return value so there is wastage of two byte memory. So missing statement is, there should statement which uses the return value.
(5)
void main(){
int a=1,b=2,c=3;
clrscr();
if(a==1){
if(b==2){
if(c==3){
printf("india");
}
else{
printf("pak");
}
}
else{
printf("pak");
}
}
else{
printf("pak");
}
getch();
}
(7)if condition always return two value.
1 if condition is true.
0 if condition is false.
So program is
void main(){
int x=3;
switch(x>2){
case 0:printf("India is best");
break;
case 1:printf("Pak is best");
}
getch();
}
(8)
far pointer always compare its whole far address. Since both or not equal so first output is: I don’t know C
Huge pointer always compare its physical address both c and d are representing same physical address so a and b are equal.
(9) # is string zinging operator. It makes the string constant of any data. So 432 is converted into “432” by macro power .Now *”432” means first char which is 4.Since we are using %d so it will print ASCII value of char 4 i.e. 52
(10) We are passing the array by xxx function. 1[ch] means *(ch+1) which is ch[1] =2.
(11) Both and b are char * type but c is char * type while d is char type.
(12) void main(){
int a,b,c;
scanf(“%d%d%d”,a,b,c);
clrscr();
while((c=a%b)!=0){
a=b;
b=c;
}
printf("%d",b);
getch();
}
(14) Scope of the auto variable is within {} if it is declared in {}.Also local variable has more priority than global variable.

-------------------------------------------------

FAQ questions in c programming

FAQ questions in c programming
-------------------------------------------------
Using only c programming language
-------------------------------------------------
check given number is prime number or not using

check the given number is armstrong number or not

check the given number is palindrome number or not

CHECKING LEAP YEAR

CHECK STRING IS PALINDROME OR NOT

PASSING ONE-DIMENSIONAL ARRAY TO A FUNCTION

CREATE A FILE AND STORE DATA IN IT

TO PASSING 2-DIMENSIONAL ARRAY TO A FUNCTION

WRITING OF STRINGS TO A FILE

READING OF STRINGS FROM A FILE

WRITING OF ENTIRE ARRAY TO A FILE

CONCATENATE MANY FILES AND STORE THEM IN A FILE NAMED FILES

SWAPPING OF STRINGS

SWAP TWO VARIABLES WITHOUT USING THIRD USING VARIABLE

SWAPING OF TWO ARRAYS USING C PROGRAM

CONCATENATE MANY FILES AND STORE THEM IN A FILE NAMED FILES

FIND PRIME FACTORS OF A NUMBER

TO FIND MULTIPLICATION TABLE

TO FIND FACTORIAL OF A NUMBER

TO FIND FIBONACCI SERIES

PRINTING ASCII VALUE USING

CONVERSION OF DECIMAL TO BINARY

CONVERSION OF BINARY TO DECIMAL

CONVERSION FROM DECIMAL TO OCTAL

CONVERSION FROM UPPERCASE TO LOWER CASE

CONVERSION FROM LOWER CASE TO UPPER CASE

CONVERSION OF FAREHNITE TO CENTIGRADE

DELETE THE VOWELS FROM A STRING

ADDITION OF TWO MATRICES

COPY DATA FROM ONE FILE TO ANOTHER FILE

ADDITION & SUBTRACTION OF TWO COMPLEX NUMBERS

FIND SUM OF THE SERIES 1+2+3+---------+n

FIND OUT LARGEST NUMBER IN AN ARRAY

FIND OUT SECOND LARGEST NUMBER IN AN UNSORTED ARRAY

MULTIPLICATION OF TWO MATRICES

FIND OUT SUM OF DIAGONAL ELEMENTS OF A MATRIX

FIND OUT TRASPOSE OF A MATRIX

find out the perfect number

COUNTING DIFFERENT CHARACTERS IN A STRING

DISPLAY SOURCE CODE AS OUTPUT

FIND FACTORIAL OF A NUMBER USING RECURSION

FIND GCD OF A NUMBER USING RECURSION

FIND SUM OF DIGITS OF A NUMBER USING RECURSION

FIND POWER OF A NUMBER

FIND POWER OF A NUMBER USING RECURSION

CONCATENATION OF TWO STRINGS USING POINTER

CONCATENATION OF TWO STRINGS
SORTING OF STRING

BUBBLE SORT
SELECTION SORT

INSERTION SORT

QUICK SORT

LINEAR SEARCH

BINARY SEARCH

BINARY SEARCH THROUGH RECURSSION

REMOVE DUPLICATE ELEMENTS IN AN ARRAY

REVERSE A NUMBER USING RECURSION

DELETE ELEMENT FROM AN ARRAY AT DESIRED POSITION

FIND GREATEST AMONG 3 NUMBERS USING CONDITIONAL OPERATOR

FIND OUT GENERIC ROOT OF A NUMBER

find g.c.d of two number
find sum of the digit of number
reverse any number

Write a scanf statement which can store one line of string which includes white space ?

HOW can TAKE a paragraph at time USING scanf FUNCTION?

STRING QUESTIONS AND ANSWER IN C

(1) Without using any semicolon (;) in program write a c program which output is: HELLO WORLD?
Answer:
void main()
{
if(printf("HELLO WORLD"))
{
}
}
(2)What will be output of following code?
void main()
{
char a[5];
a[0]='q';
a[1]='u';
a[2]='e';
clrscr();
printf("%s",a);
getch();
}
Output: garbage
Explanation: %s is used for string but a is not a string it is only array of character since its last character is not null character.
If you will write a[3]=’\0’; then output will be que.
(3) Write a scanf statement which can store one line of string which includes white space.
Answer:
void main()
{
char a[30];
clrscr();
scanf("%[^\n]",a);
printf("%s",a);
getch();
}
(4) Write a c program in which a scanf function can store a paragraph at time?
Answer:
void main()
{
char a[30];
clrscr();
scanf("%[^\t]",a);
printf("%s",a);
getch();
}
Note: Paragraph will end when you will press tab key.
(5)In the following program
void main()
{
extern char a[30];
}
How much array a is reserving memory space?
Answer:
It is not reserving any memory space because array a has only declared it has not initialized .Any not initialized variable doesn’t reserve any memory space.

FAQ questions of preprocessor in c

Question section
(1)
What will be output of following code?
#define max 10
void main()
{
int i;
i=++max;
clrscr();
printf("%d",i);
getch();
}
(2)
What will be output of following code?
#define max 10+2
void main()
{
int i;
i=max*max;
clrscr();
printf("%d",i);
getch();
}
(3)
What will be output of following code?
#define A 4-2
#define B 3-1
void main()
{
int ratio=A/B;
printf("%d ",ratio);
getch();
}
(4)
What will be output of following code?
#define MAN(x,y) (x)>(y)?(x):(y)
void main()
{
int i=10,j=9,k=0;
k=MAN(i++,++j);
printf("%d %d %d",i,j,k);
getch();
}
(5)
What will be output of following code?
#define START main() {
#define PRINT printf("*******");
#define END }
START
PRINT
END
(6)
What will be output of following code?
#define CUBE(x) (x*x*x)
#define M 5
#define N M+1
#define PRINT printf("RITESH");
void main()
{
int volume =CUBE(3+2);
clrscr();
printf("%d %d ",volume,N);
PRINT
getch();
}
Best questions of preprocessor questions
Big collection of preprocessor questions
Solution section
To read theory click here

(1)
output: compiler error.
explanation:
max is preprocesor macro which process first before the actual compilation.
preprocessor paste the symbol to the its constant value in entire the program before the compilation.so in this progrom max will be replaced by 10 before compilation.Thus program will be converted as:

void main()
{
int i;
i=++10;
clrscr();
printf("%d",i);
getch();
}

To know how we can see intermediate file click here
In this program we are trying to incrment a constant symbol.
meanig of ++10 IS :-
10=10+1
or 10=11
which is error because we cannot assigne constant value to another constant value .Hence compiler will give error.

(2)
output: 32

explanation:
max is preprocesor macro which process first before the actual compilation.
preprocessor paste the symbol to the its constant value without any calulation in entire the program before the
compilation.so in this progrom max will be replaced by 10+2 before compilation.Thus program
will be converted as:


void main()
{
int i;
i=10+2*10+2;
clrscr();
printf("%d",i);
getch();
}

now i=10+2*10+2
i=10+20+2
i=32

(3)
output : 3
explanation:

A and B preprocesor macro which process first before the actual compilation.
preprocessor paste the symbol to the its constant value without any calculation in entire the program before the
compilation.so in this progrom A and B will be replaced by 4-2 and 3-1 respectively before compilation.Thus program
will be converted as:


void main()
{
int ratio=4-2/3-1;
printf("%d ",ratio);
getch();
}


here ratio=4-2/3-1
ratio=4-0-1
ratio=3

(4)
output: 11 11 11

explanation:
Preprocesor macro which process first before the actual compilation.
preprocessor paste the symbol to the its constant value without any calculation in entire the program before the
compilation.Thus program will be converted as:


void main()
{
int i=10,j=9,k=0;
k=(i++)>(++j)?(i++):(++j);
printf("%d %d %d",i,j,k);
getch();
}

now k=(i++)>(++j)?(i++):(++j);
first it will check the condion
(i++)>(++j)
i++ i.e when postfix is used with variable in expression then expression is evaluated first with original
value then variable is incemented
or 10>10
this condition is false.
now i=10+1=11
there is rule , only false part will execute after ? i.e ++j ,i++ will be not execute.
so after ++j
j=10+1=11;
and k will assign value of j .so k=11;

(5)
output: *******
explanation:
Preprocesor macro which process first before the actual compilation.
preprocessor paste the symbol to the its constant value i.e symbol without any calculation in entire the program before the compilation.Thus program will be converted as:


main()
{
printf("*******");
}
(6)
output: 17 6
explanation:
Preprocessor macro which process first before the actual compilation.
preprocessor paste the symbol to the its constant value without any calculation in entire the program before the compilation.Thus program will be converted as:

void main()
{
int volume =(3+2*3+2*3+2);
clrscr();
printf("%d %d ",volume,5+1);
PRINT
getch();
}

C POINTER QUESTIONS

(1) What will be output of following program?void main()

{

int a=320;

char *ptr;

ptr=(char *)&a;

printf("%d ",*ptr);

getch();

}

(a) 2

(b) 320

(c) 64

(d)Compiler error

(e)None of above

(2) What will be output of following program?#include"stdio.h"

#include"conio.h"

void main()

{

void (*p)();

int (*q)();

int (*r)();

p=clrscr;

q=getch;

r=puts;

(*p)();

(*r)("cquestionbank.blogspot.com");

(*q)();

}

(a) NULL

(b) cquestionbank.blogspot.com

(c) c

(d) Compiler error

(e) None of above

(3) What will be output of following program?
void main()

{

int i=3;

int *j;

int **k;

j=&i;

k=&j;

printf(“%u %u %d ”,k,*k,**k);

}

(a) Address, Address, 3

(b)Address, 3, 3

(c) 3, 3, 3

(d) Compiler error

(e) None of above

(4) What will be output of following program?void main()

{

char far *p=(char far *)0x55550005;

char far *q=(char far *)0x53332225;

*p=80;

(*p)++;

printf("%d",*q);

getch();

}

(a) 80

(b) 81

(c) 82

(d) Compiler error

(e) None of above

(5) What will be output of following program?
#include"stdio.h"

#include"string.h"

void main()

{

char *ptr1=NULL;

char *ptr2=0;

strcpy(ptr1," c");

strcpy(ptr2,"questions");

printf("\n%s %s",ptr1,ptr2);

getch();

}

(a) c questions

(b) c (null)

(c) (null) (null)

(d) Compiler error

(e) None of above


(6) What will be output of following program?void main()

{

int huge *a=(int huge *)0x59990005;

int huge *b=(int huge *)0x59980015;

if(a==b)

printf("power of pointer");

else

printf("power of c");

getch();

}

(a) power of pointer

(b) power of c

(c) power of cpower of c

(d) Compiler error

(e) None of above


(7) What will be output of following program?
#include"stdio.h"

#include"string.h"

void main()

{

register a=25;

int far *p;

p=&a;

printf("%d ",*p);

getch();

}

(a) 25

(b) 4

(c) Address

(d) Compiler error

(e) None of above


(8) What will be output of following program?#include"stdio.h"

#include"string.h"

void main()

{

char far *p,*q;

printf("%d %d",sizeof(p),sizeof(q));

getch();

}

(a) 2 2

(b) 4 4

(c) 4 2

(d) 2 4

(e) None of above

(9) What will be output of following program?void main()

{

int a=10;

void *p=&a;

int *ptr=p;

printf("%u",*ptr);

getch();

}

(a) 10

(b) Address

(c) 2

(d) Compiler error

(e) None of above


(10) What will be output of following program?#include"stdio.h"

#include"string.h"

void main()

{

int register a;

scanf("%d",&a);

printf("%d",a);

getch();

}

//if a=25


(a) 25

(b) Address

(c) 0

(d) Compiler error

(e) None of above

(11) What will be output of following program?void main()

{

char arr[10];

arr="world";

printf("%s",arr);

getch();

}

(a) world

(b) w

(c) Null

(d) Compiler error

(e) None of above

(12) What will be output of following program?
#include"stdio.h"

#include"string.h"

void main()

{

int a,b,c,d;

char *p=0;

int *q=0;

float *r=0;

double *s=0;

a=(int)(p+1);

b=(int)(q+1);

c=(int)(r+1);

d=(int)(s+1);

printf("%d %d %d %d",a,b,c,d);

}

(a)2 2 2 2

(b)1 2 4 8

(c)1 2 2 4

(d) Compiler error

(e) None of above

(13) What will be output of following program?
#include"stdio.h"

#include"string.h"

void main()

{

int a=5,b=10,c;

int *p=&a,*q=&b;

c=p-q;

printf("%d",c);

getch();

}

(a) 1

(b) 5

(c) -5

(d) Compiler error

(e) None of above


(14) What will be output of following program?
unsigned long int (* avg())[3]

{

static unsigned long int arr[3]={1,2,3};

return &arr;

}

void main()

{

unsigned long int (*ptr)[3];

ptr=avg();

printf("%d",*(*ptr+2));

getch();

}

(a) 1

(b) 2

(c) 3

(d) Compiler error

(e) None of above

(15) What will be output of following program?
void main()

{

int * p,b;

b=sizeof(p);

printf(“%d”,b);

}

(a) 2

(b) 4

(c) 8

(d) Compiler error

(e) None of above


(16) What will be output of following program?
void main()

{

int i=5,j;

int *p,*q;

p=&i;

q=&j;

j=5;

printf("value of i : %d value of j : %d",*p,*q);

getch();

}

(a) 5 5

(b) Address Address

(c) 5 Address

(d) Compiler error

(e) None of above


(17) What will be output of following program?
{

int i=5;

int *p;

p=&i;

printf(" %u %u",*&p,&*p);

getch();

}

(a) 5 Address

(b) Address Address

(c) Address 5

(d) Compiler error

(e) None of above


(18) What will be output of following program? void main()

{

int i=100;

printf("value of i : %d addresss of i : %u",i,&i);

i++;

printf("\nvalue of i : %d addresss of i : %u",i,&i);

getch();

}

(a) value of i : 100 addresss of i : Address

value of i : 101 addresss of i : Address

(b) value of i : 100 addresss of i : Address

value of i : 100 addresss of i : Address

(c) value of i : 101 addresss of i : Address

value of i : 101 addresss of i : Address

(d) Compiler error

(e) None of above

(19) What will be output of following program? void main()

{

char far *p=(char far *)0x55550005;

char far *q=(char far *)0x53332225;

*p=25;

(*p)++;

printf("%d",*q);

getch();

}

(a) 25

(b) Address

(c) Garbage

(d) Compiler error

(e) None of above



(20) What will be output of following program? void main()

{

int i=3;

int *j;

int **k;

j=&i;

k=&j;

printf(“%u %u %u”,i,j,k);

}

(a) 3 Address 3

(b) 3 Address Address

(c) 3 3 3

(d) Compiler error

(e) None of above















Answer:

(1) c

(2) b

(3) a

(4) b

(5) c

(6) a

(7) d

(8) c

(9) a

(10) d

(11) d

(12) b

(13) a

(14) c

(15) e

(16) a

(17) b

(18) a

(19) e

(20) b


Explanation

(1)

As we know int is two byte data byte while char is one byte data byte. char pointer can keep the address one byte at time.

Binary value of 320 is 00000001 01000000 (In 16 bit)

Memory representation of int a=320 is:









So ptr is pointing only first 8 bit which color is green and

Decimal value is 64.

(2)

p is pointer to function whose parameter is void and return type

is also void. r and q is pointer to function whose parameter is

void and return type is int . So they can hold the address of

such function.

(3)

Memory representation




Here 6024, 8085, 9091 is any arbitrary address, it may be different.

Value of k is content of k in memory which is 8085

Value of *k means content of memory location which address k keeps.

k keeps address 8085 .

Content of at memory location 8085 is 6024

In the same way **k will equal to 3.


Short cut way to calculate:

Rule: * and & always cancel to each other

i.e. *&a=a

So *k=*(&j) since k=&j

*&j=j =6024

And

**k=**(&j)=*(*&j)=*j=*(&i)=*&i=i=3

(4)

Far address of p and q are representing same physical address .

Physical address of 0x55550005= (0x5555)*(0x10) + (0x0005) = 0x55555

Physical address of 0x53332225= (0x5333*0x10) + (0x2225) =0x55555

*p =80, means content at memory location 0x55555 is assigning value 25

(*p)++ means increase the content by one at memory location 0x5555 so now


content at memory location 0x55555 is 81

*q also means content at memory location 0x55555 which is 26

(5)

We cannot assign any string constant in null pointer by strcpy function.

(6)

Here we are performing relational operation between two huge addresses. So


first both a and b will normalize.

a= (0x5999)* (0x10) + (0x0005) =0x9990+0x0005=0x9995

b= (0x5998)* (0x10) + (0x0015) =0x9980+0x0015=0x9995

Here both huge addresses are representing same physical address. So a==b


is true.

(7)

Register data type stores in CPU. So it has not any memory

address. Hence we cannot write &a.

(8)

p is far pointer which size is 4 byte.

By default q is near pointer which size is 2 byte.

(9)

void pointer can hold address of any data type without type casting. Any


pointer can hold void pointer without type casting.

(10)

Register data type stores in CPU. So it has not any memory address. Hence


we cannot write &a.


(11) Compiler error Lvalue required

Array name is constant pointer and we cannot assign any

value in constant data type after declaration.


(12)

Address=next address

Since initial address of all data type is zero. So its

next address will be size of data type.


(13)

Difference of two same type of pointer is always one.


(15)

Output: 2 or 4

since in this question it has not written p is which type

of pointer. So its output will depend upon which memory

model has selected. Default memory model is small.



(17)

Since * and & always cancel to each other.

i.e. *&a=a

so *&p=p which store address of integer i

&*p=&*(&i) //since p=&i

=&(*&i)

=&i

So second output is also address of i


(18)

Within the scope of any variable, value of variable

may change but its address will never change in any

modification of variable.


(19)

Far address of p and q are representing same physical

address. Physical address of

0x55550005= 0x5555*ox10+ox0005= 0x55555

Physical address of

0x53332225=0x5333*0x10+ox2225=0x55555

*p =25, means content at memory location 0x55555 is

assigning value 25

(*p)++ means increase the content by one at memory

location 0x5555 so now content at memory location 0x55555

is 26

*q also means content at memory location 0x55555 which is

26



(20)



Here 6024, 8085, 9091 is any arbitrary address, it may be different.

C operator question with answer

Question section
(1)
what will be output of the following program?
void main(){
float a=0.7;
if(a<0.7){ printf("C"); } else{ printf("C++"); } } For solution and detail explanation click here (2) what will be output of the following program? void main() { int i=5,j; j=++i+++i+++i; printf("%d %d",i,j); } For solution and detail explanation click here (3) what will be output of the following program? void main() { int i=1; i=2+2*i++; printf("%d",i); } For solution and detail explanation click here (4) what will be output of the following program? void main() { int a=2,b=7,c=10; c=a==b; printf("%d",c); } For solution and detail explanation click here (5) what will be output of the following program? void main() { int x; x=10,20,30; printf("%d",x); } For solution and detail explanation click here (6) what will be output of the following program? void main() { int a=0,b=10; if(a=0) { printf("true"); } else { printf("false"); } } For solution and detail explanation click here (7) what will be output of the following program? void main() { int a; a=015 + 0x71 +5; printf("%d",a); } For solution and detail explanation click here (8) what will be output of the following program? void main() { printf("%d %d %d",sizeof(3.14),sizeof(3.14f),sizeof(3.14L)); } For solution and detail explanation click here (9) what will be output of the following program? void main() { int x=100,y=20,z=5; printf("%d %d %d"); } For solution and detail explanation click here (10) what will be output of the following program? void main(){ int a=2; a=a++ + ~++a; printf("%d",a); } For solution and detail explanation click here (11) what will be output of the following program? void main() { int a; a=sizeof(!5.6); printf(“%d”,a); } For solution and detail explanation click here (12) what will be output of the following program? void main() { float a; (int)a= 45; printf(“%d”,a); } For solution and detail explanation click here (13) what will be output of the following program? void main() { int i=5; int a=++i + ++i + ++i; printf(“%d”,a); } Solution of good question C operator question Solution section (1) output: c explanation : 0.7f is float constant. Its binary value is written in 32 bit. 0.7 is double constant(default). Its binary value is written in 64 bit. 0.7L is long double constant. Its binary value is written in 80 bit. binary value of 0.7=(0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011) now here a is a float variable while 0.7 is double constant .so a contain only 32 bit value i.e a=0.1011 0011 0011 0011 0011 0011 0011 0011 while 0.7=0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011.... It is obvious a<0.7>

(2)
output: 8 24
explanation :
rule :- ++ is preincrement operator so in any arithmetic operation it first increment the value of variable by one in whole equation then start assingning the end value of variable in the equation . j=++i + ++i + ++i; initial value of i=5 after three times increment i=8 now final value of i i.e. 8 will assigne to each . so j=8+8+8; j=24; i=8;

(3)
output: 5
explanation :
i++ i.e when postfix is used with variable in expression then expression is evaluated first with original value then variable is incremented. so i=2+2*1 i=4 now i will be incremented by one so i=4+1=5

(4)
output: 0
explanation :
== is relation operator which will give only two value they are 0 : if a==b is false 1 : if a==b is true now a=2 b=7 so a==b is false hence b=0

(5)
output: 10
explanation :
comma operator (,) has least precedence and its associativity is from left to right so assignment operator (=) has more precedence than comma operator .So = operator will be evaluated first than comma operator x = 10 , 20 , 30 precedence 1 2 3 first x =10 will be evaluated then control will transfer to 20 then 30.

(6)
output: false
explanation :
if(a=0) then a=0 so if(0) means false if(1) means true note : if(n) means true, where n is any number either positive or negative except zero

(7)
output: 131
explanation :
015 is octal number its decimal equivalent is =5*8^0+1*8^1 =5+8 =13 0x71 is hexadecimal number (0x is symbol of hexadecimal) its decimal equivalent is =1*16^0+7*16^1 =1+112 =113 so a=13+113+5=131

(8)
output: 8 4 10
explanation :
3.14f is float constant. Its size is 4 byte. 3.14 is double constant(default). Its size is 8 byte. 3.14L is long double constant. Its size is 10 byte. sizeof() operator always return the size of data type which is written inside the(). It is keyword.
(9)
output: 5 20 100
By default x,y,z are auto type data which stores in stack in memory.Stack is LIFO data structure. So in stack first stores 100 then 20 then 5 and program counter (2 byte) points top element i.e 5 .Default value of %d in printf is data which is persent in stack.
So output is revere order of dclaration. So output will be 5 20 100.
note. Initialize variables are more near then uninialize variable.


(10)
output: -1
Explanation: Same theory as question (2) and (13).

(11)
Output : 2
Explanation: ! is negation operator it return either integer 0 or 1.
!operand=0 if operand is non zero.
!operand=1 if operand is zero.
So !5.6=0 ,since 0 is integer number and size of integer data type is two byte.

(12)
Output : error
Explanation: After using any operator on operand it always return some integer value. (int) i.e type casting operator is not exception for this. (int) a is converted in any integer value and we cannot assign any constant value to another constant value in c .So
(int)a =45; is equivalent to
3456=45 .( here 3456 in any garbage value of int(a)).

(13)
output : 21
explanation : rule :- ++ is preincrement operator so in any arithmetic operation it first increment the value of variable by one in whole equation upto break point then start assingning the value of variable in the equation . There are three break point.
(1) declaration or intialization statement.
(2) && or operator.
(3) comma (,) operator.
int a=++i + ++i + ++i;
here break point is due to declaration and inialization operator .It break after each increment i.e ( initial value of i=5) after first increment value 6 assign to i then next incrementation occur and so on . so a=6+7+8;

Looping questions in c

(1)



extern int x;

void main(){

clrscr();

do{

do{

printf("%o",x);

}

while(!-2);

}

while(0);

getch();

}

int x=8;



Output: 10



(2)



void main(){

int i=2,j=2;

clrscr();

while(i+1?--i:j++)

printf("%d",i);

getch();

}



Output: 1

(3)



void main(){

int x=011,i;

clrscr();

for(i=0;i>1)

continue;

}while(0);

break;

}

}

getch();

}



Output: 1 1 1 1 1 1



(14)



void main(){

char c=125;

clrscr();

do

printf("%d ",c);

while(c++);

getch();

}



Output: finite time



(15)



void main(){

int x=123;

int i={

printf("c" "++")

};

for(x=0;x<=i;x++){

printf("%x ",x);

}

getch();

}



Output: c++0 1 2 3

C QUESTIONS AND ANSWER

1. PERFECT NUMBER.

void main()

{

int n,i=1,sum=0;

clrscr();

printf("\nEnter a number:-");

scanf("%d",&n);

while(in2)

n1=n1-n2;

else

n2=n2-n1;

}

printf("\nGCD=%d",n1);

getch();

}

9. L.C.M OF TWO NUMBERS.

void main()

{

int n1,n2,x,y;

clrscr();

printf("\nEnter two numbers:");

scanf("%d %d",&n1,&n2);

x=n1,y=n2;

while(n1!=n2)

{

if(n1>n2)

n1=n1-n2;

else

n2=n2-n1;

}

printf("L.C.M=%d",x*y/n1);

getch();

}

10. SWAP TWO VARIABLES WITHOUT USING THIRD VARIABLE

void main()

{

int a,b;

clrscr();

printf("\nEnter two numbers:");

scanf("%d %d",&a,&b);

printf("\nBefore swapping a=%d b=%d",a,b);

a=a^b;

b=b^a;

a=a^b;

printf("\nAfter swapping a=%d b=%d",a,b);

getch();

}

11. FLOYD’S TRIANGLE

1

2 3

4 5 6

void main()

{

int i,j,r,k=1;

clrscr();

printf("\nEnter the range:");

scanf("%d",&r);

printf("\nFLOYD'S TRIANGLE\n\n");

for(i=1;i<=r;i++)

{

for(j=1;j<=i;j++,k++)

printf(" %d",k);

printf("\n");

}

getch();

}

12. PRIME FACTORS OF A NUMBER

void main()

{

int num,i=1,j,k;

clrscr();

printf("\nEnter a number:");

scanf("%d",&num);

while(i<=num)

{

k=0;

if(num%i==0)

{

j=1;

while(j<=i)

{

if(i%j==0)

k++;

j++;

}

if(k==2)

printf("\n%d is a prime factor",i);

}

i++;

}

getch();

}

13. MULTIPLICATION TABLE

void main()

{

int r,i,j,k;

clrscr();

printf("\nEnter the number range:-");

scanf("%d",&r);

for(i=1;i<=r;i++)

{

for(j=1;j<=10;j++)

printf(" %d*%d=%d",i,j,i*j);

printf("\n");

}

getch();

}

14. FACTORIAL OF A NUMBER

void main()

{

int i=1,f=1,num;

clrscr();

printf("\nEnter a number:");

scanf("%d",&num);

while(i<=num)

{

f=f*i;

i++;

}

printf("\nFactorial of %d is:%d",num,f);

getch();

}

15. FIBONACCI SERIES

void main()

{

int i=0,j=1,k=2,r,f;

clrscr();

printf("Enter the number range:");

scanf("%d",&r);

printf("\nFIBONACCI SERIES: ");

printf("%d %d",i,j);

while(k
{

f=i+j;

i=j;

j=f;

printf(" %d",j);

k++;

}

getch();

}

C questions and answer

(51) What will be output if you will compile and execute the following c code?



struct marks{

int p:3;

int c:3;

int m:2;

};

void main(){

struct marks s={2,-6,5};

printf("%d %d %d",s.p,s.c,s.m);

}



(a) 2 -6 5

(b) 2 -6 1

(c) 2 2 1

(d) Compiler error

(e) None of these













Answer: (c)

Explanation:

Binary value of 2: 00000010 (Select three two bit)

Binary value of 6: 00000110

Binary value of -6: 11111001+1=11111010

(Select last three bit)

Binary value of 5: 00000101 (Select last two bit)



Complete memory representation:











Structure tutorial



More questions



(52) What will be output if you will compile and execute the following c code?



void main(){

static char *s[3]={"math","phy","che"};

typedef char *( *ppp)[3];

static ppp p1=&s,p2=&s,p3=&s;

char * (*(*array[3]))[3]={&p1,&p2,&p3};

char * (*(*(*ptr)[3]))[3]=&array;

p2+=1;

p3+=2;

printf("%s",(***ptr[0])[2]);



}



(a) math

(b) phy

(c) che

(d) Compiler error

(e) None of these













Answer: (c)

Explanation:

Here

ptr: is pointer to array of pointer to string.

P1, p2, p3: are pointers to array of string.

array[3]: is array which contain pointer to array of string.

Pictorial representation:









Note: In the above figure upper part of box represent content and lower part represent memory address. We have assumed arbitrary address.



As we know p[i]=*(p+i)

(***ptr[0])[2]=(*(***ptr+0))[2]=(***ptr)[2]

=(***(&array))[2] //ptr=&array

=(**array)[2] //From rule *&p=p

=(**(&p1))[2] //array=&p1

=(*p1)[2]

=(*&s)[2] //p1=&s

=s[2]=”che”



How to read complex pointer?



Pointer tutorial.



(53) What will be output if you will compile and execute the following c code?



#include"conio.h"

int display();

int(*array[3])();

int(*(*ptr)[3])();

void main(){

array[0]=display;

array[1]=getch;

ptr=&array;

printf("%d",(**ptr)());

(*(*ptr+1))();

}

int display(){

int x=5;

return x++;

}



(a)5

(b)6

(c)0

(d)Compiler error

(e)None of these











Answer: (a)

Explanation:

In this example:

array []: It is array of pointer to such function which parameter is void and return type is int data type.



ptr: It is pointer to array which contents are pointer to such function which parameter is void and return type is int type data.



(**ptr)() = (** (&array)) () //ptr=&array

= (*array) () // from rule *&p=p

=array [0] () //from rule *(p+i)=p[i]

=display () //array[0]=display



(*(*ptr+1))() =(*(*&array+1))() //ptr=&array

=*(array+1) () // from rule *&p=p

=array [1] () //from rule *(p+i)=p[i]

=getch () //array[1]=getch



How to read complex array?



Array tutorial.



(54) What will be output if you will compile and execute the following c code?



void main(){

int i;

char far *ptr=(char *)0XB8000000;

*ptr='A';

*(ptr+1)=1;

*(ptr+2)='B';

*(ptr+3)=2;

*(ptr+4)='C';

*(ptr+5)=4;

}













Answer:

It output will be A, B and C in blue, green and red color respectively. As shown in following figure:









What is far pointer?



Advance c tutorial?



Working with text video memory.



(55) What will be output if you will compile and execute the following c code?



#include "dos.h"

void main(){

int j;

union REGS i,o;

char far *ptr=(char *)0XA0000000;

i.h.ah=0;

i.h.al=0x13;

int86(0x10,&i,&o);

for(j=1;j<=100;j++){ *(ptr+j)=4; } } Answer: One red color line in the graphics console as shown in the following figure What is union REGS? Advance c tutorial. Working with graphics video memory. (56) What will be output if you will compile and execute the following c code? void main(){ int huge*p=(int huge*)0XC0563331; int huge*q=(int huge*)0xC2551341; *p=200; printf("%d",*q); } (a)0 (b)Garbage value (c)null (d) 200 (e)Compiler error Answer: (d) Explanation: Physical address of huge pointer p Huge address: 0XC0563331 Offset address: 0x3331 Segment address: 0XC056 Physical address= Segment address * 0X10 + Offset address =0XC056 * 0X10 +0X3331 =0XC0560 + 0X3331 =0XC3891 Physical address of huge pointer q Huge address: 0XC2551341 Offset address: 0x1341 Segment address: 0XC255 Physical address= Segment address * 0X10 + Offset address =0XC255 * 0X10 +0X1341 =0XC2550 + 0X1341 =0XC3891 Since both huge pointers p and q are pointing same physical address so content of q will also same as content of q. What is huge pointer? Pointer tutorial. (57) Write c program which display mouse pointer and position of pointer.(In x coordinate, y coordinate)? Answer: #include”dos.h” #include”stdio.h” void main() { union REGS i,o; int x,y,k; //show mouse pointer i.x.ax=1; int86(0x33,&i,&o); while(!kbhit()) //its value will false when we hit key in the key board { i.x.ax=3; //get mouse position x=o.x.cx; y=o.x.dx; clrscr(); printf("(%d , %d)",x,y); delay(250); int86(0x33,&i,&o); } getch(); } What is int86? Advance c tutorial. (58) Write a c program to create dos command: dir. Answer: Step 1: Write following code. #include “stdio.h” #include “dos.h” void main(int count,char *argv[]) { struct find_t q ; int a; if(count==1) argv[1]="*.*"; a = _dos_findfirst(argv[1],1,&q); if(a==0) { while (!a) { printf(" %s\n", q.name); a = _dos_findnext(&q); } } else { printf("File not found"); } } Step 2: Save the as list.c (You can give any name) Step 3: Compile and execute the file. Step 4: Write click on My computer of Window XP operating system and select properties. Step 5: Select Advanced -> Environment Variables

Step 6: You will find following window:

Click on new button (Button inside the red box)









Step 7: Write following:

Variable name: path

Variable value: c:\tc\bin\list.c (Path where you have saved)









Step 8: Open command prompt and write list and press enter.



Command line argument tutorial.



(59) What will be output if you will compile and execute the following c code?



void main(){

int i=10;

static int x=i;

if(x==i)

printf("Equal");

else if(x>i)

printf("Greater than");

else

printf("Less than");



}



(a) Equal

(b) Greater than

(c) Less than

(d) Compiler error

(e) None of above













Answer: (d)

Explanation:

static variables are load time entity while auto variables are run time entity. We can not initialize any load time variable by the run time variable.

In this example i is run time variable while x is load time variable.



Properties of static variables.



Properties of auto variables.



(60) What will be output if you will compile and execute the following c code?

void main(){

int i;

float a=5.2;

char *ptr;

ptr=(char *)&a;

for(i=0;i<=3;i++) printf("%d ",*ptr++); } (a)0 0 0 0 (b)Garbage Garbage Garbage Garbage (c)102 56 -80 32 (d)102 102 -90 64 (e)Compiler error Answer: (d) Explanation: In c float data type is four byte data type while char pointer ptr can point one byte of memory at a time. Memory representation of float a=5.2 ptr pointer will point first fourth byte then third byte then second byte then first byte. Content of fourth byte: Binary value=01100110 Decimal value= 64+32+4+2=102 Content of third byte: Binary value=01100110 Decimal value=64+32+4+2=102 Content of second byte: Binary value=10100110 Decimal value=-128+32+4+2=-90 Content of first byte: Binary value=01000000 Decimal value=64 Note: Character pointer treats MSB bit of each byte i.e. left most bit of above figure as sign bit. How to represent float data type in memory? (61) What will be output if you will compile and execute the following c code? void main(){ int i; double a=5.2; char *ptr; ptr=(char *)&a; for(i=0;i<=7;i++) printf("%d ",*ptr++); } (a) -51 -52 -52 -52 -52 -52 20 64 (b) 51 52 52 52 52 52 20 64 (c) Eight garbage values. (d) Compiler error (e) None of these Answer: (a) Explanation: In c double data type is eight byte data type while char pointer ptr can point one byte of memory at a time. Memory representation of double a=5.2 ptr pointer will point first eighth byte then seventh byte then sixth byte then fifth byte then fourth byte then third byte then second byte then first byte as shown in above figure. Content of eighth byte: Binary value=11001101 Decimal value= -128+64+8+4+1=-51 Content of seventh byte: Binary value=11001100 Decimal value= -128+64+8+4=-52 Content of sixth byte: Binary value=11001100 Decimal value= -128+64+8+4=-52 Content of fifth byte: Binary value=11001100 Decimal value= -128+64+8+4=-52 Content of fourth byte: Binary value=11001100 Decimal value= -128+64+8+4=-52 Content of third byte: Binary value=11001100 Decimal value= -128+64+8+4=-52 Content of second byte: Binary value=000010100 Decimal value=16+4=20 Content of first byte: Binary value=01000000 Decimal value=64 Note: Character pointer treats MSB bit of each byte i.e. left most bit of above figure as sign bit. How to represent double data type in memory? (62) What will be output if you will compile and execute the following c code? void main(){ printf("%s","c" "question" "bank"); } (a) c question bank (b) c (c) bank (d) cquestionbank (e) Compiler error Answer: (d) Explanation: In c string constant “xy” is same as “x” “y” String tutorial. (63) What will be output if you will compile and execute the following c code? void main(){ printf("%s",__DATE__); } (a) Current system date (b) Current system date with time (c) null (d) Compiler error (e) None of these Answer: (a) Explanation: __DATE__ is global identifier which returns current system date. What is global identifier? (64) What will be output if you will compile and execute the following c code? void main(){ char *str="c-pointer"; printf("%*.*s",10,7,str); } (a) c-pointer (b) c-pointer (c) c-point (d) cpointer null null (e) c-point Answer: (e) Explanation: Meaning of %*.*s in the printf function: First * indicates the width i.e. how many spaces will take to print the string and second * indicates how many characters will print of any string. Following figure illustrates output of above code: Properties of printf function. (65) What will be output if you will compile and execute the following c code? void start(); void end(); #pragma startup start #pragma exit end int static i; void main(){ printf("\nmain function: %d",++i); } void start(){ clrscr(); printf("\nstart function: %d",++i); } void end(){ printf("\nend function: %d",++i); getch(); } (a) main function: 2 start function: 1 end function:3 (b) start function: 1 main function: 2 end function:3 (c) main function: 2 end function:3 start function: 1 (d) Compiler error (e) None of these Answer: (b) Explanation: Every c program start with main function and terminate with null statement. But #pragma startup can call function just before main function and #pragma exit What is pragma directive? Preprocessor tutorial. (66) What will be output if you will compile and execute the following c code? void main(){ int a=-12; a=a>>3;

printf("%d",a);

}



(a) -4

(b) -3

(c) -2

(d) -96

(e) Compiler error













Answer :( c)

Explanation:



Binary value of 12 is: 00000000 00001100

Binary value of -12 wills 2’s complement of 12 i.e.









So binary value of -12 is: 11111111 11110100









Right shifting rule:

Rule 1: If number is positive the fill vacant spaces in the left side by 0.

Rule 2: If number is negative the fill vacant spaces in the left side by 1.

In this case number is negative. So right shift all the binary digits by three space and fill vacant space by 1 as shown following figure:









Since it is negative number so output will also a negative number but its 2’s complement.









Hence final out put will be:









And its decimal value is: 2

Hence output will be:-2



More questions on shifting operator.



Operator tutorial.



(67) What will be output if you will compile and execute the following c code?



#include "string.h"

void main(){

clrscr();

printf("%d %d",sizeof("string"),strlen("string"));

getch();

}



(a) 6 6

(b) 7 7

(c) 6 7

(d) 7 6

(e) None of these













Answer: (d)

Explanation:

Sizeof operator returns the size of string including null character while strlen function returns length of a string excluding null character.



String tutorial.



Library functions of string.



(68) What will be output if you will compile and execute the following c code?



void main(){

static main;

int x;

x=call(main);

clrscr();

printf("%d ",x);

getch();

}

int call(int address){

address++;

return address;

}



(a) 0

(b) 1

(c) Garbage value

(d) Compiler error

(e) None of these













Answer: (b)

Explanation:

As we know main is not keyword of c but is special type of function. Word main can be name variable in the main and other functions.



What is main function in c?



(69) What will be output if you will compile and execute the following c code?



void main(){

int a,b;

a=1,3,15;

b=(2,4,6);

clrscr();

printf("%d ",a+b);

getch();

}



(a) 3

(b) 21

(c) 17

(d) 7

(e) Compiler error













Answer: (d)

Explanation:

In c comma behaves as separator as well as operator.

a=1, 3, 15;

b= (2, 4, 6);

In the above two statements comma is working as operator. Comma enjoys least precedence and associative is left to right.



Assigning the priority of each operator in the first statement:









Hence 1 will assign to a.

Assigning the priority of each operator in the second statement:
























Operator tutorial.



(70) What will be output if you will compile and execute the following c code?



int dynamic(int,...);

void main(){

int x,y;

x=dynamic(2,4,6,8,10,12,14);

y=dynamic(3,6,9,12);

clrscr();

printf("%d %d ",x,y);

getch();

}

int dynamic(int s,...){

void *ptr;

ptr=...;

(int *)ptr+=2;

s=*(int *)ptr;

return s;



}



(a) 8 12

(b) 14 12

(c) 2 3

(d) Compiler error

(e) None of these













Answer: (a)

Explanation:

In c three continuous dots is known as ellipsis which is variable number of arguments of function. In this example ptr is generic pointer which is pointing to first element of variable number of argument. After incrementing it will point third element.



What is variable number of argument?



(71) What will be output if you will compile and execute the following c code?



int extern x;

void main()

printf("%d",x);

x=2;

getch();

}

int x=23;



(a) 0

(b) 2

(c) 23

(d) Compiler error

(e) None of these













Answer: (c)

Explanation:

extern variables can search the declaration of variable any where in the program.



Properties of extern storage class.



(72) What will be output if you will compile and execute the following c code?



void main(){

int i=0;

if(i==0){

i=((5,(i=3)),i=1);

printf("%d",i);

}

else

printf("equal");

}



(a) 5

(b) 3

(c) 1

(d) equal

(e) None of above











Answer: (c)

Explanation:



Comma operator.



Operator tutorial.



(73) What will be output if you will compile and execute the following c code?



void main(){

int a=25;

clrscr();

printf("%o %x",a,a);

getch();

}



(a) 25 25

(b) 025 0x25

(c) 12 42

(d) 31 19

(e) None of these













Answer: (d)

Explanation:

%o is used to print the number in octal number format.

%x is used to print the number in hexadecimal number format.



Note: In c octal number starts with 0 and hexadecimal number starts with 0x.



What is octal number?



What is hexadecimal number?



(74) What will be output if you will compile and execute the following c code?





#define message "union is\

power of c"

void main(){

clrscr();

printf("%s",message);

getch();

}



(a) union is power of c

(b) union ispower of c

(c) union is

Power of c

(d) Compiler error

(e) None of these













Answer: (b)

Explanation:

If you want to write macro constant in new line the end with the character \.



Preprocessor tutorial.



(75) What will be output if you will compile and execute the following c code?





#define call(x) #x

void main(){

printf("%s",call(c/c++));

}



(a)c

(b)c++

(c)#c/c++

(d)c/c++

(e)Compiler error













Answer: (d)

Explanation:

# is string operator. It converts the macro function call argument in the string. First see the intermediate file:



test.c 1:

test.c 2: void main(){

test.c 3: printf("%s","c/c++");

test.c 4: }

test.c 5:



It is clear macro call is replaced by its argument in the string format.



What is # and ##?



Preprocessor tutorial?



(75) What will be output if you will compile and execute the following c code?



void main(){

if(printf("cquestionbank"))

printf("I know c");

else

printf("I know c++");

}



(a) I know c

(b) I know c++

(c) cquestionbankI know c

(d) cquestionbankI know c++

(e) Compiler error











Answer: (c)

Explanation:

Return type of printf function is integer which returns number of character it prints including blank spaces. So printf function inside if condition will return 13. In if condition any non- zero number means true so else part will not execute.



Prototype of printf function.

c interview questions

COMMONLY ASKED QUESTIONS IN INTERVIEW



1. In the following declaration statement

char c=’A’;

Variable c stores one byte of memory space while character constants ‘A’ stores one byte memory space. How one byte variables can stores two byte character constant?


2. What is automatic type promotion in c?

3. Swap two variables without using third variable.

4. Write a c program without using any semicolon which output is: Hello word.

5. How will you modify a const variable in c?

6. Write a c program to find out factorial of given number using function recursion.

7. Give an example in which comma is behaving as separator as well as operator in c.

8. What is variable number of arguments in c?

9. What is command line argument?

10. What will be output of following c code?

void main()
{

int i;

for(i=0;i<=5;i++); printf("%d",i); getch(); } 11. Subtract two integer numbers without using subtraction operator. 12. What are differences between sizeof operator and strlen function? 13. Declared a variable in c without defining it. 14. int x=sizeof(!5.856); 15 What will value of variable x? 15. What is difference between initialization and assignment of a variable? 16. Tell me something about auto storage class. 17. What will be output of following c code? extern int a; void main(){ int a=5; { int a=10; printf("%d",a++); } printf(" %d",a); getch(); } int a=20; 18. Declare a pointer which can point printf function in c. 19. What are merits and demerits of array in c? 20. Tell me a all sorting algorithm which you know. Answer: ----------------------------- 1. Character constant reserve two byte of memory space to represent octal or hexadecimal character constant but char variable stores only its one byte ASCII value. ------------- 2. In c if two operands are of different data type in a binary operation then before performing any operation compiler will automatically convert the operand of lower data type to higher data type .This phenomenon is known as automatic type conversion. For example: int a=10,c; float b=5.5f; c=a+b; Here a int variable while b is float variable. So before performing addition operation value of the variable a (Lower data type) will automatically convert into float constant (higher data type) then it will perform addition operation. ----------------------- 3. Solution: 1 void main(){ int x,y; scanf("%d%d",&x,&y); //swapping x=x+y; y=x-y; x=x-y; printf("%d %d",x,y); } Solution: 2 void main(){ int x,y; scanf("%d%d",&x,&y); //swapping x=x^y; y=y^x; x=x^y; printf("%d %d",x,y); } ------------------ 4. Solution: 1 void main(){ if(printf("Hello world")){ } } Solution: 2 void main(){ while(!printf("Hello world")){ } } Solution: 3 void main(){ switch(printf("Hello world")){ } } ----------------------- 5. We can modify the const variable with the help of pointer. void main(){ const int a=10; int *ptr=(int *)&a; *ptr=20; clrscr(); printf("%d",a); getch(); } Output: 20 --------------------- 6. void main() { long num,f; clrscr(); printf("Input a number: "); scanf("%ld",&num); f=fact(num); printf("\nFactorial is %ld",f); getch(); } int fact(long n) { if(n==0) return 1; else return(n*fact(n-1)); } ---------------- 7. void main(){ int x=5,y=10,z=15,val; val=sum(x,(y=0,z=0,y),z); clrscr(); printf("%d",val); getch(); } sum(int x,int y,int z){ return x+y+z; } Output: 20 Note: In the above program comma in red color are behaving as operator. -------------- 8. A function in which we can pass variables numbers of argument in function call statement such functions are known as function with variable number of arguments. For example: void display(int,...); void main(){ display(1,2); display(1,2,3,4); display(1,2,3,4,5,6,7,8,9); getch(); } void display(int x,...){ } Note: There consecutive dot is known as ellipsis in c. ------------- 9. Getting the arguments from command prompt in c is known as command line arguments. In c main function has three arguments. They are: (a)Argument counter (b)Argument vector (c)Environment vector -------------- 10. 6 -------------------- 11. void main(){ int a,b,d; scanf("%d%d",&a,&b); d=a+~b+1; printf("%d",d); getch(); } ------------------- 12. sizeof is keyword of c which can find size of a string constant including null character but strlen is function which has been defined string.h and can find number of characters in a string excluding null character. #include

void main(){

int a,b;

a=strlen("cquestionbank");

b=sizeof("cquestionbank");

printf("%d %d",a,b);

getch();

}


----------------
13. extern int a;

Note: Uninitialized extern variables are example of declaration of variables.
---------------
14. 2

17. 10 5

18. int (*ptr)(char const,…);

19.

(a) We can easily access each element of array.

(b) Not necessity to declare two many variables.

(c) Array elements are stored in continuous memory location.



Demerit:

(a) Wastage of memory space. We cannot change size of array at the run time.

(b) It can store only similar type of data.

----------------

20.

(a)Bubble sort

(b)Selection sort

(c)Insertion sort

(d)Quick sort

(e)Merge sort

(f)Heap sort

c objective questions and answer

(1) What will be output if you will compile and execute the following c code?



void main(){

int i=320;

char *ptr=(char *)&i;

printf("%d",*ptr);

}



(a)320

(b)1

(c)64

(d)Compiler error

(e)None of above











Output: (c)

Explanation:

As we know size of int data type is two byte while char pointer can pointer one byte at time.

Memory representation of int i=320









So char pointer ptr is pointing to only first byte as shown above figure.

*ptr i.e. content of first byte is 01000000 and its decimal value is 64.



How to represent char, int and float data in memory?

Data type tutorial.



(2) What will be output if you will compile and execute the following c code?



#define x 5+2

void main(){

int i;

i=x*x*x;

printf("%d",i);

}



(a)343

(b)27

(c)133

(d)Compiler error

(e)None of above









Output: (b)

Explanation:

As we know #define is token pasting preprocessor it only paste the value of micro constant in the program before the actual compilation start. If you will see intermediate file you will find:



test.c 1:

test.c 2: void main(){

test.c 3: int i;

test.c 4: i=5+2*5+2*5+2;

test.c 5: printf("%d",i);

test.c 6: }

test.c 7:





You can absorb #define only pastes the 5+2 in place of x in program. So,

i=5+2*5+2*5+2

=5+10+10+2

=27



What is intermediate file and how to see intermediate file?

Preprocessor tutorial.



(3) What will be output if you will compile and execute the following c code?



void main(){

char c=125;

c=c+10;

printf("%d",c);

}



(a)135

(b)+INF

(c)-121

(d)-8

(e)Compiler error











Output: (c)

Explanation:

As we know char data type shows cyclic properties i.e. if you will increase or decrease the char variables beyond its maximum or minimum value respectively it will repeat same value according to following cyclic order:









So,

125+1= 126

125+2= 127

125+3=-128

125+4=-127

125+5=-126

125+6=-125

125+7=-124

125+8=-123

125+9=-122

125+10=-121



What is cyclic nature of data type?



Data type tutorial.





(4) What will be output if you will compile and execute the following c code?



void main(){

float a=5.2;



if(a==5.2)

printf("Equal");

else if(a<5.2) printf("Less than"); else printf("Greater than"); } (a)Equal (b)Less than (c)Greater than (d)Compiler error (e)None of above Output: (b) Explanation: 5.2 is double constant in c. In c size of double data is 8 byte while a is float variable. Size of float variable is 4 byte. So double constant 5.2 is stored in memory as: 101.00 11001100 11001100 11001100 11001100 11001100 11001101 Content of variable a will store in the memory as: 101.00110 01100110 01100110 It is clear variable a is less than double constant 5.2 Since 5.2 is recurring float number so it different for float and double. Number likes 4.5, 3.25, 5.0 will store same values in float and double data type. Note: In memory float and double data is stored in completely different way. If you want to see actual memory representation goes to question number (60) and (61). Data type tutorial. (5) What will be output if you will compile and execute the following c code? void main(){ int i=4,x; x=++i + ++i + ++i; printf("%d",x); } (a)21 (b)18 (c)12 (d)Compiler error (e)None of above Output: (a) Explanation: In ++a, ++ is pre increment operator. In any mathematical expression pre increment operator first increment the variable up to break point then starts assigning the final value to all variable. Step 1: Increment the variable I up to break point. Step 2: Start assigning final value 7 to all variable i in the expression. So, i=7+7+7=21 What is break point? Operator tutorial. (6) What will be output if you will compile and execute the following c code? void main(){ int a=2; if(a==2){ a=~a+2<<1; printf("%d",a); } else { break; } } (a)It will print nothing. (b)-3 (c)-2 (d)1 (e)Compiler error Output: (e) Explanation: Keyword break is not part of if-else statement. Hence it will show compiler error: Misplaced break Where we can use break keyword? Control statement tutorial (7) What will be output if you will compile and execute the following c code? void main(){ int a=10; printf("%d %d %d",a,a++,++a); } (a)12 11 11 (b)12 10 10 (c)11 11 12 (d)10 10 12 (e)Compiler error Output: (a) Explanation: In c printf function follows cdecl parameter passing scheme. In this scheme parameter is passed from right to left direction. So first ++a will pass and value of variable will be a=10 then a++ will pass now value variable will be a=10 and at the end a will pass and value of a will be a=12. What is cedecl and pascal parameter passing convention? Function tutorial. (8) What will be output if you will compile and execute the following c code? void main(){ char *str="Hello world"; printf("%d",printf("%s",str)); } (a) 11Hello world (b) 10Hello world (c) Hello world10 (d) Hello world11 (e) Compiler error Output: (d) Explanation: Return type of printf function is integer and value of this integer is exactly equal to number of character including white space printf function prints. So, printf(“Hello world”) will return 13. What is prototype of printf function? Formatted I/O tutorial. (9) What will be output if you will compile and execute the following c code? #include "stdio.h" #include "string.h" void main(){ char *str=NULL; strcpy(str,"cquestionbank"); printf("%s",str); } (a)cquestionbank (b)cquestionbank\0 (c)(null) (d)It will print nothing (e)Compiler error Output: (c) Explanation: We cannot copy any thing using strcpy function to the character pointer pointing to NULL. String tutorial. More questions of string. (10) What will be output if you will compile and execute the following c code? #include "stdio.h" #include "string.h" void main(){ int i=0; for(;i<=2;) printf(" %d",++i); } (a)0 1 2 (b)0 1 2 3 (c)1 2 3 (d)Compiler error (e)Infinite loop Output: (c) Explanation: In for loop each part is optional. Complete tutorial of looping in C. (11) What will be output if you will compile and execute the following c code? void main(){ int x; for(x=1;x<=5;x++); printf("%d",x); } (a)4 (b)5 (c)6 (d)Compiler error (e)None of above Output: (c) Explanation: Body of for loop is optional. In this question for loop will execute until value of variable x became six and condition became false. Looping tutorial. (12) What will be output if you will compile and execute the following c code? void main(){ printf("%d",sizeof(5.2)); } (a)2 (b)4 (c)8 (d)10 (e)Compiler error Output: (c) Explanation: Default type of floating point constant is double. So 5.2 is double constant and its size is 8 byte. Detail explanation of all types of constant in C. (13) What will be output if you will compile and execute the following c code? #include "stdio.h" #include "string.h" void main(){ char c='\08'; printf("%d",c); } (a)8 (b)’8’ (c)9 (d)null (e)Compiler error Output: (e) Explanation: In c any character is starting with character ‘\’ represents octal number in character. As we know octal digits are: 0, 1, 2, 3, 4, 5, 6, and 7. So 8 is not an octal digit. Hence ‘\08’ is invalid octal character constant. Octal character constantan. Hexadecimal character constant. (14) What will be output if you will compile and execute the following c code? #define call(x,y) x##y void main(){ int x=5,y=10,xy=20; printf("%d",xy+call(x,y)); } (a)35 (b)510 (c)15 (d)40 (e)None of above Output: (d) Explanation: ## is concatenation c preprocessor operator. It only concatenates the operands i.e. a##b=ab If you will see intermediate file then you will find code has converted into following intermediate code before the start of actual compilation. Intermediate file: test.c 1: test.c 2: void main(){ test.c 3: int x=5,y=10,xy=20; test.c 4: printf("%d",xy+xy); test.c 5: } test.c 6: It is clear call(x, y) has replaced by xy. What is macro call? Preprocessor tutorial. (15) What will be output if you will compile and execute the following c code? int * call(); void main(){ int *ptr; ptr=call(); clrscr(); printf("%d",*ptr); } int * call(){ int a=25; a++; return &a; } (a)25 (b)26 (c)Any address (d)Garbage value (e)Compiler error Output: (d) Explanation: In this question variable a is a local variable and its scope and visibility is within the function call. After returning the address of a by function call variable a became dead while pointer ptr is still pointing to address of variable a. This problem is known as dangling pointer problem. Complete pointer tutorial. (16) What is error in following declaration? struct outer{ int a; struct inner{ char c; }; }; (a)Nesting of structure is not allowed in c. (b)It is necessary to initialize the member variable. (c)Inner structure must have name. (d)Outer structure must have name. (e)There is not any error. Output: (c) Explanation: It is necessary to assign name of inner structure at the time of declaration other wise we cannot access the member of inner structure. So correct declaration is: struct outer{ int a; struct inner{ char c; }name; }; Structure tutorial. Union tutorial. (17) What will be output if you will compile and execute the following c code? void main(){ int array[]={10,20,30,40}; printf("%d",-2[array]); } (a)-60 (b)-30 (c)60 (d)Garbage value (e)Compiler error Output: (b) Explanation: In c, array[2]=*(array+2)=*(2+array)=2[array]=30 Array tutorial. Array of pointer. How to read complex pointers. (18) What will be output if you will compile and execute the following c code? void main(){ int i=10; static int x=i; if(x==i) printf("Equal"); else if(x>i)

printf("Greater than");

else

printf("Less than");



}



(a)Equal

(b)Greater than

(c)Less than

(d)Compiler error

(e)None of above











Output: (d)

Explanation:

static variables are load time entity while auto variables are run time entity. We can not initialize any load time variable by the run time variable.

In this example i is run time variable while x is load time variable.



What is storage class?



(18) What will be output if you will compile and execute the following c code?



void main(){

int i=5,j=2;

if(++i>j++||i++>j++)

printf("%d",i+j);

}



(a)7

(b)11

(c)8

(d)9

(e)Compiler error













Output: (d)

Explanation:

|| is logical OR operator. In C logical OR operator doesn’t check second operand if first operand is true.

++i>j++ || i++>j++

First operand: ++i>j++

Second operand: i++>j++

First operand

++i > j++

=> 6 > 2

Since first operand is true so it will not check second operand.

Hence i= 6 and j=3



Properties of && operator.



Operator tutorial with examples.





(19) What will be output if you will compile and execute the following c code?



#define max 5;

void main(){

int i=0;

i=max++;

printf("%d",i++);

}



(a)5

(b)6

(c)7

(d)0

(e)Compiler error

Output: (e)













Explanation:

#define is token pasting preprocessor. If you will see intermediate file: test.i



test.c 1:

test.c 2: void main(){

test.c 3: int i=0;

test.c 4: i=5++;

test.c 5: printf("%d",i++);

test.c 6: }

test.c 7:



It is clear macro constant max has replaced by 5. It is illegal to increment the constant number. Hence compiler will show Lvalue required.





What is Lvalue and Rvalue?



How to see intermediate file?



Preprocessor questions and answer.



(20) What will be output if you will compile and execute the following c code?



void main(){

double far* p,q;

printf("%d",sizeof(p)+sizeof q);

}



(a)12

(b)8

(c)4

(d)1

(e)Compiler error









Output: (a)

Explanation:

It is clear p is far pointer and size of far pointer is 4 byte while q is double variable and size of double variable is 8 byte.



What is near pointer?



What is far pointer?



What is huge pointer?



Complete pointer tutorial.



(21) What will be output if you will compile and execute the following c code?



void main(){

int a=5;

float b;

printf("%d",sizeof(++a+b));

printf(" %d",a);

}



(a)2 6

(b)4 6

(c)2 5

(d)4 5

(e)Compiler error











Output: (d)

Explanation:

++a +b

=6 + Garbage floating point number

=Garbage floating point number

//From the rule of automatic type conversion

Hence sizeof operator will return 4 because size of float data type in c is 4 byte.

Value of any variable doesn’t modify inside sizeof operator. Hence value of variable a will remain 5.



Properties of sizeof operator.



Operators tutorial



(22) What will be output if you will compile and execute the following c code?



void main(){

char huge *p=(char *)0XC0563331;

char huge *q=(char *)0XC2551341;

if(p==q)

printf("Equal");

else if(p>q)

printf("Greater than");

else

printf("Less than");

}



(a)Equal

(b)Greater than

(c)Less than

(d)Compiler error

(e)None of above











Output: (a)

Explanation:

As we know huge pointers compare its physical address.

Physical address of huge pointer p



Huge address: 0XC0563331

Offset address: 0x3331

Segment address: 0XC056



Physical address= Segment address * 0X10 + Offset address

=0XC056 * 0X10 +0X3331

=0XC0560 + 0X3331

=0XC3891



Physical address of huge pointer q



Huge address: 0XC2551341

Offset address: 0x1341

Segment address: 0XC255



Physical address= Segment address * 0X10 + Offset address

=0XC255 * 0X10 +0X1341

=0XC2550 + 0X1341

=0XC3891



Since both huge pointers p and q are pointing same physical address so if condition will true.



What is huge pointer?

What is normalization?

Pointer tutorial.





(23) What will be output if you will compile and execute the following c code?



void main(){

char *str;

scanf("%[^\n]",str);

printf("%s",str);

}

(a)It will accept a word as a string from user.

(b)It will accept a sentence as a string from user.

(c)It will accept a paragraph as a string from user.

(d)Compiler error

(e)None of above













Output: (b)

Explanation:

Task of % [^\t] is to take the stream of characters until it doesn’t receive new line character ‘\t’ i.e. enter button of your keyboard.



General meaning of %[^ p]



String tutorial.



(24) What will be output if you will compile and execute the following c code?



void main(){

int a=5,b=10,c=15;

int *arr[]={&a,&b,&c};

printf("%d",*arr[1]);

}



(a)5

(b)10

(c)15

(d)Compiler error

(e)None of above













Output: (d)

Explanation:

Array element cannot be address of auto variable. It can be address of static or extern variables.



What is auto variable?



What is extern variable?



What is static variable?



Array tutorial.



(25) What will be output if you will compile and execute the following c code?



void main(){

int array[3]={5};

int i;

for(i=0;i<=2;i++) printf("%d ",array[i]); } (a)5 garbage garbage (b)5 0 0 (c)5 null null (d)Compiler error (e)None of above Output: (b) Explanation: Storage class of an array which initializes the element of the array at the time of declaration is static. Default initial value of static integer is zero. Properties of static storage class. How to read complex array. (26) What will be output if you will compile and execute the following c code? void main(){ int array[2][2][3]={0,1,2,3,4,5,6,7,8,9,10,11}; printf("%d",array[1][0][2]); } (a)4 (b)5 (c)6 (d)7 (e)8 Output: 8 Explanation: array[1][0][2] means 1*(2*3)+0*(3)+3=9th element of array starting from zero i.e. 8. Questions on two dimension array. Complete tutorial of array. (27) What will be output if you will compile and execute the following c code? void main(){ int a[2][4]={3,6,9,12,15,18,21,24}; printf("%d %d %d",*(a[1]+2),*(*(a+1)+2),2[1[a]]); } (a)15 18 21 (b)21 21 21 (c)24 24 24 (d)Compiler error (e)None of above Output: (b) Explanation: In c, a [1][2] =*(a [1] +2) =*(*(a+1) +2) =2[a [1]] =2[1[a]] Now, a [1] [2] means 1*(4) +2=6th element of an array staring from zero i.e. 21. Concept of complex array. Concept of complex pointer. Concept of complex function. (28) What will be output if you will compile and execute the following c code? void call(int,int,int); void main(){ int a=10; call(a,a++,++a); } void call(int x,int y,int z){ printf("%d %d %d",x,y,z); } (a)10 10 12 (b)12 11 11 (c)12 12 12 (d)10 11 12 (e)Compiler error Output: (e) Explanation: Default parameter passing scheme of c is cdecl i.e. argument of function will pass from right to left direction. First ++a will pass and a=11 Then a++ will pass and a=11 Then a will pass and a=12 What is pascal and cedecl parameter passing scheme? Concept of variable numbers of argument. (29) What will be output if you will compile and execute the following c code? void main(){ int x=5,y=10,z=15; printf("%d %d %d"); } (a)Garbage Garbage Garbage (b)5 10 15 (c)15 10 5 (d)Compiler error (e)Run time error Output: (c) Explanation: Auto variables are stored in stack as shown in following figure. Stack follow LIFO data structure i.e. last come and first out. First %d will print then content of two continuous bytes from the top of the stack and so on. Memory map tutorial. More questions based on memory map. (30) What will be output if you will compile and execute the following c code? void main(){ register int i,x; scanf("%d",&i); x=++i + ++i + ++i; printf("%d",x); } (a)17 (b)18 (c)21 (d)22 (e)Compiler error Output: (e) Explanation: In c register variable stores in CPU it doesn’t store in RAM. So register variable have not any memory address. So it is illegal to write &a. Complete tutorial of storage class with examples. Properties of register storage class. (31) What will be output if you will compile and execute the following c code? void main(){ int a=5; int b=10; { int a=2; a++; b++; } printf("%d %d",a,b); } (a)5 10 (b)6 11 (c)5 11 (d)6 10 (e)Compiler error Output: (c) Explanation: Default storage class of local variable is auto. Scope and visibility of auto variable is within the block in which it has declared. In c, if there are two variables of the same name then we can access only local variable. Hence inside the inner block variable a is local variable which has declared and defined inside that block. When control comes out of the inner block local variable a became dead. Complete tutorial of storage class with examples. What is auto storage class? (32) What will be output if you will compile and execute the following c code? void main(){ float f=3.4e39; printf("%f",f); } (a)3.4e39 (b)3.40000… (c)+INF (d)Compiler error (e)Run time error Output: (c) Explanation: If you will assign value beyond the range of float data type to the float variable it will not show any compiler error. It will store infinity. Data type tutorial with examples. Concept of float data type. (33) What will be output if you will compile and execute the following c code? void main(){ enum color{ RED,GREEN=-20,BLUE,YELLOW }; enum color x; x=YELLOW; printf("%d",x); } (a)-22 (b)-18 (c)1 (d)Compiler error (e)None of above Output: (b) Explanation: Default value of enum constant = value of previous enum constant +1 Default value of first enum constant=0 Hence: BLUE=GREEN+1=-20+1=-19 YELLOW=BLUE+1=-19+1=-18 Complete tutorial of enum data type with examples. (34) What will be output if you will compile and execute the following c code? void main(){ asm{ mov bx,8; mov cx,10 add bx,cx; } printf("%d",_BX); } (a)18 (b)8 (c)0 (d)Compiler error (e)None of above Output: (a) Explanation: asm keyword is used to write assembly language program in c. mov command stores the constants in the register bx, cx etc. add command stores the content of register and stores in first register i.e. in bx. How to write assembly language program by c? Advance c tutorial. (35) What will be output if you will compile and execute the following c code? void main(){ enum xxx{ a,b,c=32767,d,e }; printf("%d",b); } (a)0 (b)1 (c)32766 (d)Compiler error (e)None of above Output: (d) Explanation: Size of enum constant is size of sign int. Since value of c=32767. Hence value of d will be 32767+1=32768 which is beyond the range of enum constant. Tutorial of data type with examples. (36) What will be output if you will compile and execute the following c code? void main(){ signed int a=-1; unsigned int b=-1; if(a==b) printf("%d %d",a,b); else printf("Not equal"); } (a)-1 -1 (b)-1 32767 (c)-1 -32768 (d)Not equal (e)Compiler error Output: (a) Explanation: What is automatic type conversion? (37) What will be output if you will compile and execute the following c code? void main(){ float f=5.5f; float x; x=f%2; printf("%f",x); } (a)1.500000 (b)1.000000 (c)5.500000 (d)Compiler error (e)None of above Output: (d) Explanation: Modular division is not allowed with floating number. Properties of modular division. Operators tutorial with examples. (38) What will be output if you will compile and execute the following c code? void main(){ int a=-20; int b=-3; printf("%d",a%b); } (a)2 (b)-2 (c)18 (d)-18 (e)Compiler error Output: (b) Explanation: Sign of resultant of modular division depends upon only the sign of first operand. Properties of modular division. Operator’s tutorial with examples. (39) What will be output if you will compile and execute the following c code? void main(){ char c='0'; printf("%d %d",sizeof(c),sizeof('0')); } (a)1 1 (b)2 2 (c)1 2 (d)2 1 (e)None of above Output: (c) Explanation: Size of char data type is one byte while size of character constant is two byte. Why character constant is of two byte in c? (40) What will be output if you will compile and execute the following c code? void main(){ char *url="c:\tc\bin\rw.c"; printf("%s",url); } (a)c:\tc\bin\rw.c (b)c:/tc/bin/rw.c (c)c: c inw.c (d)c:cinw.c (e)w.c in Output: (e) Explanation: 1. \t is tab character which moves the cursor 8 space right. 2. \b is back space character which moves the cursor one space back. 3. \r is carriage return character which moves the cursor beginning of the line. Complete string tutorial with examples. Properties of escape characters. (41) What will be output if you will compile and execute the following c code? void main(){ clrscr(); goto abc; printf("main"); getch(); } void dispaly(){ abc: printf("display"); } (a)main (b)display (c)maindisplay (d)displaymain (e)Compiler error Output: (e) Explanation: Label of goto cannot be in other function because control cannot move from one function to another function directly otherwise it will show compiler error: unreachable label What is goto keyword. Complete function tutorial with examples. (42) What will be output if you will compile and execute the following c code? void main(){ int i=3; if(3==i) printf("%d",i<<2<<1); else printf("Not equal"); } (a)1 (b)48 (c)24 (d)Not equal (e)Compiler error Output: (c) Explanation: Associative of bitwise left shifting operator is left to right. In the following expression: i<<2<<1 There are two bitwise operators. From rule of associative leftmost operator will execute first. i <<><<>

After execution of leftmost bitwise left shifting operator:

so i=i*pow(2,2)

=3*



What is associative?



What is precedence?



Tutorial of bitwise operators.



(43) What will be output if you will compile and execute the following c code?



void main(){

int x=2,y=3;

if(x+y<=5) printf("True"); else printf("False"); } (a)True (b)False (c)Compiler error: Lvalued required (d)Compiler error: Invalid expression (e)None of above Output: (a) Explanation: Expression x+y<=5 => 2+3 <=5 => 5<=5 is true because 5 is either greater than 5 or equal to 5. Operator tutorial with examples. (44) What will be output if you will compile and execute the following c code? void main(){ const int i=5; i++; printf("%d",i); } (a)5 (b)6 (c)0 (d)Compiler error (e)None of above Output: (d) Explanation: We cannot modify the const variable by using increment operator. Properties of const keyword. Properties of volatile keyword. Data type tutorial with examples. (45) What will be output if you will compile and execute the following c code? void main(){ const int x=25; int * const p=&x; *p=2*x; printf("%d",x); } (a)25 (b)50 (c)0 (d)Compiler error (e)None of above Output: (b) Explanation: const keyword in c doesn’t make any variable as constant but it only makes the variable as read only. With the help of pointer we can modify the const variable. In this example pointer p is pointing to address of variable x. In the following line: int * const p=&x; p is constant pointer while content of p i.e. *p is not constant. *p=2*x put the value 50 at the memory location of variable x. Properties of const keyword. What is constant pointer? Data type tutorial with examples. (46) What will be output if you will compile and execute the following c code? void main(){ int i=11; int const * p=&i; p++; printf("%d",*p); } (a)11 (b) 12 (c)Garbage value (d)Compiler error (e)None of above Output: (c) Explanation: In the following line: int const * p=&i; *p i.e. content of p is constant pointer p is not constant pointer. So we can modify the pointer p. After incrementing the pointer it will point next memory location and its content will any garbage value. Note: We have assumed arbitrary memory address. To make pointer p as constant pointer write: int const * const p=&i; Properties of const keyword. Properties of volatile keyword. (47) What will be output if you will compile and execute the following c code? void main(){ int a=15,b=10,c=5; if(a>b>c )

printf("Trre");

else

printf("False");

}



(a)True

(b)False

(c)Run time error

(d)Compiler error

(e)None of above













Output: (b)

Explanation:

Relation operator in c always returns 1 when condition is true and 0 when condition is false. So in the following expression

a > b > c

Associative of relational operators are left to right order of execution will be following manner:









Hence in this expression first solve bolded condition: a > b > c

Since condition a>b is true so result will be 1. Now expression became:



1 > c

Since this condition is false so result will be 0. Thus else part will execute.



What is associative?



What is precedence?



(48) What will be output if you will compile and execute the following c code?



void main(){

float f;

f=3/2;

printf("%f",f);

}



(a)1.5

(b)1.500000

(c)1.000000

(d)Compiler error

(e)None of above













Output: (b)

Explanation:

In the following expression:

f=3/2 both 3 and 2 are integer constant hence its result will also be an integer constant i.e. 1.



Properties of floating type numbers.



(49) What will be output if you will compile and execute the following c code?



void main(){

int a=sizeof(a);

a=modify(a);

printf("%d",a);

}

int modify(int x){

int y=3;

_AX=x+y;

return;

}







(a)2

(b)3

(c)5

(d)Garbage value

(e)None of above











Output: (c)

Explanation:

_AX is register pseudo variable. It stores return type of function.



What is register pseudo variable?



What is global identifier?



(50) What will be output if you will compile and execute the following c code?



#define PRINT printf("c");printf("c++");

void main(){

float a=5.5;

if(a==5.5)

PRINT

else

printf("Not equal");

}



(a)c c++

(b)Not equal

(c)c

c++

(d)Compiler error

(e)None of above











Output: (d)

Explanation:



First see intermediate file:



try.c 1:

try.c 2: void main(){

try.c 3: float a=5.5;

try.c 4: if(a==5.5)

try.c 5: printf("c");printf("c++");

try.c 6: else

try.c 7: printf("Not equal");

try.c 8: }

try.c 9:

try.c 10:



If there are more than one statement in if block then it is necessary to write inside the { } otherwise it will show compiler error: misplaced else





More questions on preprocessors.



Preprocessor tutorial with examples.





Links to this post
5 comments
C questions and answer












(51) What will be output if you will compile and execute the following c code?



struct marks{

int p:3;

int c:3;

int m:2;

};

void main(){

struct marks s={2,-6,5};

printf("%d %d %d",s.p,s.c,s.m);

}



(a) 2 -6 5

(b) 2 -6 1

(c) 2 2 1

(d) Compiler error

(e) None of these













Answer: (c)

Explanation:

Binary value of 2: 00000010 (Select three two bit)

Binary value of 6: 00000110

Binary value of -6: 11111001+1=11111010

(Select last three bit)

Binary value of 5: 00000101 (Select last two bit)



Complete memory representation:











Structure tutorial



More questions



(52) What will be output if you will compile and execute the following c code?



void main(){

static char *s[3]={"math","phy","che"};

typedef char *( *ppp)[3];

static ppp p1=&s,p2=&s,p3=&s;

char * (*(*array[3]))[3]={&p1,&p2,&p3};

char * (*(*(*ptr)[3]))[3]=&array;

p2+=1;

p3+=2;

printf("%s",(***ptr[0])[2]);



}



(a) math

(b) phy

(c) che

(d) Compiler error

(e) None of these













Answer: (c)

Explanation:

Here

ptr: is pointer to array of pointer to string.

P1, p2, p3: are pointers to array of string.

array[3]: is array which contain pointer to array of string.

Pictorial representation:









Note: In the above figure upper part of box represent content and lower part represent memory address. We have assumed arbitrary address.



As we know p[i]=*(p+i)

(***ptr[0])[2]=(*(***ptr+0))[2]=(***ptr)[2]

=(***(&array))[2] //ptr=&array

=(**array)[2] //From rule *&p=p

=(**(&p1))[2] //array=&p1

=(*p1)[2]

=(*&s)[2] //p1=&s

=s[2]=”che”



How to read complex pointer?



Pointer tutorial.



(53) What will be output if you will compile and execute the following c code?



#include"conio.h"

int display();

int(*array[3])();

int(*(*ptr)[3])();

void main(){

array[0]=display;

array[1]=getch;

ptr=&array;

printf("%d",(**ptr)());

(*(*ptr+1))();

}

int display(){

int x=5;

return x++;

}



(a)5

(b)6

(c)0

(d)Compiler error

(e)None of these











Answer: (a)

Explanation:

In this example:

array []: It is array of pointer to such function which parameter is void and return type is int data type.



ptr: It is pointer to array which contents are pointer to such function which parameter is void and return type is int type data.



(**ptr)() = (** (&array)) () //ptr=&array

= (*array) () // from rule *&p=p

=array [0] () //from rule *(p+i)=p[i]

=display () //array[0]=display



(*(*ptr+1))() =(*(*&array+1))() //ptr=&array

=*(array+1) () // from rule *&p=p

=array [1] () //from rule *(p+i)=p[i]

=getch () //array[1]=getch



How to read complex array?



Array tutorial.



(54) What will be output if you will compile and execute the following c code?



void main(){

int i;

char far *ptr=(char *)0XB8000000;

*ptr='A';

*(ptr+1)=1;

*(ptr+2)='B';

*(ptr+3)=2;

*(ptr+4)='C';

*(ptr+5)=4;

}













Answer:

It output will be A, B and C in blue, green and red color respectively. As shown in following figure:









What is far pointer?



Advance c tutorial?



Working with text video memory.



(55) What will be output if you will compile and execute the following c code?



#include "dos.h"

void main(){

int j;

union REGS i,o;

char far *ptr=(char *)0XA0000000;

i.h.ah=0;

i.h.al=0x13;

int86(0x10,&i,&o);

for(j=1;j<=100;j++){ *(ptr+j)=4; } } Answer: One red color line in the graphics console as shown in the following figure What is union REGS? Advance c tutorial. Working with graphics video memory. (56) What will be output if you will compile and execute the following c code? void main(){ int huge*p=(int huge*)0XC0563331; int huge*q=(int huge*)0xC2551341; *p=200; printf("%d",*q); } (a)0 (b)Garbage value (c)null (d) 200 (e)Compiler error Answer: (d) Explanation: Physical address of huge pointer p Huge address: 0XC0563331 Offset address: 0x3331 Segment address: 0XC056 Physical address= Segment address * 0X10 + Offset address =0XC056 * 0X10 +0X3331 =0XC0560 + 0X3331 =0XC3891 Physical address of huge pointer q Huge address: 0XC2551341 Offset address: 0x1341 Segment address: 0XC255 Physical address= Segment address * 0X10 + Offset address =0XC255 * 0X10 +0X1341 =0XC2550 + 0X1341 =0XC3891 Since both huge pointers p and q are pointing same physical address so content of q will also same as content of q. What is huge pointer? Pointer tutorial. (57) Write c program which display mouse pointer and position of pointer.(In x coordinate, y coordinate)? Answer: #include”dos.h” #include”stdio.h” void main() { union REGS i,o; int x,y,k; //show mouse pointer i.x.ax=1; int86(0x33,&i,&o); while(!kbhit()) //its value will false when we hit key in the key board { i.x.ax=3; //get mouse position x=o.x.cx; y=o.x.dx; clrscr(); printf("(%d , %d)",x,y); delay(250); int86(0x33,&i,&o); } getch(); } What is int86? Advance c tutorial. (58) Write a c program to create dos command: dir. Answer: Step 1: Write following code. #include “stdio.h” #include “dos.h” void main(int count,char *argv[]) { struct find_t q ; int a; if(count==1) argv[1]="*.*"; a = _dos_findfirst(argv[1],1,&q); if(a==0) { while (!a) { printf(" %s\n", q.name); a = _dos_findnext(&q); } } else { printf("File not found"); } } Step 2: Save the as list.c (You can give any name) Step 3: Compile and execute the file. Step 4: Write click on My computer of Window XP operating system and select properties. Step 5: Select Advanced -> Environment Variables

Step 6: You will find following window:

Click on new button (Button inside the red box)









Step 7: Write following:

Variable name: path

Variable value: c:\tc\bin\list.c (Path where you have saved)









Step 8: Open command prompt and write list and press enter.



Command line argument tutorial.



(59) What will be output if you will compile and execute the following c code?



void main(){

int i=10;

static int x=i;

if(x==i)

printf("Equal");

else if(x>i)

printf("Greater than");

else

printf("Less than");



}



(a) Equal

(b) Greater than

(c) Less than

(d) Compiler error

(e) None of above













Answer: (d)

Explanation:

static variables are load time entity while auto variables are run time entity. We can not initialize any load time variable by the run time variable.

In this example i is run time variable while x is load time variable.



Properties of static variables.



Properties of auto variables.



(60) What will be output if you will compile and execute the following c code?

void main(){

int i;

float a=5.2;

char *ptr;

ptr=(char *)&a;

for(i=0;i<=3;i++) printf("%d ",*ptr++); } (a)0 0 0 0 (b)Garbage Garbage Garbage Garbage (c)102 56 -80 32 (d)102 102 -90 64 (e)Compiler error Answer: (d) Explanation: In c float data type is four byte data type while char pointer ptr can point one byte of memory at a time. Memory representation of float a=5.2 ptr pointer will point first fourth byte then third byte then second byte then first byte. Content of fourth byte: Binary value=01100110 Decimal value= 64+32+4+2=102 Content of third byte: Binary value=01100110 Decimal value=64+32+4+2=102 Content of second byte: Binary value=10100110 Decimal value=-128+32+4+2=-90 Content of first byte: Binary value=01000000 Decimal value=64 Note: Character pointer treats MSB bit of each byte i.e. left most bit of above figure as sign bit. How to represent float data type in memory? (61) What will be output if you will compile and execute the following c code? void main(){ int i; double a=5.2; char *ptr; ptr=(char *)&a; for(i=0;i<=7;i++) printf("%d ",*ptr++); } (a) -51 -52 -52 -52 -52 -52 20 64 (b) 51 52 52 52 52 52 20 64 (c) Eight garbage values. (d) Compiler error (e) None of these Answer: (a) Explanation: In c double data type is eight byte data type while char pointer ptr can point one byte of memory at a time. Memory representation of double a=5.2 ptr pointer will point first eighth byte then seventh byte then sixth byte then fifth byte then fourth byte then third byte then second byte then first byte as shown in above figure. Content of eighth byte: Binary value=11001101 Decimal value= -128+64+8+4+1=-51 Content of seventh byte: Binary value=11001100 Decimal value= -128+64+8+4=-52 Content of sixth byte: Binary value=11001100 Decimal value= -128+64+8+4=-52 Content of fifth byte: Binary value=11001100 Decimal value= -128+64+8+4=-52 Content of fourth byte: Binary value=11001100 Decimal value= -128+64+8+4=-52 Content of third byte: Binary value=11001100 Decimal value= -128+64+8+4=-52 Content of second byte: Binary value=000010100 Decimal value=16+4=20 Content of first byte: Binary value=01000000 Decimal value=64 Note: Character pointer treats MSB bit of each byte i.e. left most bit of above figure as sign bit. How to represent double data type in memory? (62) What will be output if you will compile and execute the following c code? void main(){ printf("%s","c" "question" "bank"); } (a) c question bank (b) c (c) bank (d) cquestionbank (e) Compiler error Answer: (d) Explanation: In c string constant “xy” is same as “x” “y” String tutorial. (63) What will be output if you will compile and execute the following c code? void main(){ printf("%s",__DATE__); } (a) Current system date (b) Current system date with time (c) null (d) Compiler error (e) None of these Answer: (a) Explanation: __DATE__ is global identifier which returns current system date. What is global identifier? (64) What will be output if you will compile and execute the following c code? void main(){ char *str="c-pointer"; printf("%*.*s",10,7,str); } (a) c-pointer (b) c-pointer (c) c-point (d) cpointer null null (e) c-point Answer: (e) Explanation: Meaning of %*.*s in the printf function: First * indicates the width i.e. how many spaces will take to print the string and second * indicates how many characters will print of any string. Following figure illustrates output of above code: Properties of printf function. (65) What will be output if you will compile and execute the following c code? void start(); void end(); #pragma startup start #pragma exit end int static i; void main(){ printf("\nmain function: %d",++i); } void start(){ clrscr(); printf("\nstart function: %d",++i); } void end(){ printf("\nend function: %d",++i); getch(); } (a) main function: 2 start function: 1 end function:3 (b) start function: 1 main function: 2 end function:3 (c) main function: 2 end function:3 start function: 1 (d) Compiler error (e) None of these Answer: (b) Explanation: Every c program start with main function and terminate with null statement. But #pragma startup can call function just before main function and #pragma exit What is pragma directive? Preprocessor tutorial. (66) What will be output if you will compile and execute the following c code? void main(){ int a=-12; a=a>>3;

printf("%d",a);

}



(a) -4

(b) -3

(c) -2

(d) -96

(e) Compiler error













Answer :( c)

Explanation:



Binary value of 12 is: 00000000 00001100

Binary value of -12 wills 2’s complement of 12 i.e.









So binary value of -12 is: 11111111 11110100









Right shifting rule:

Rule 1: If number is positive the fill vacant spaces in the left side by 0.

Rule 2: If number is negative the fill vacant spaces in the left side by 1.

In this case number is negative. So right shift all the binary digits by three space and fill vacant space by 1 as shown following figure:









Since it is negative number so output will also a negative number but its 2’s complement.









Hence final out put will be:









And its decimal value is: 2

Hence output will be:-2



More questions on shifting operator.



Operator tutorial.



(67) What will be output if you will compile and execute the following c code?



#include "string.h"

void main(){

clrscr();

printf("%d %d",sizeof("string"),strlen("string"));

getch();

}



(a) 6 6

(b) 7 7

(c) 6 7

(d) 7 6

(e) None of these













Answer: (d)

Explanation:

Sizeof operator returns the size of string including null character while strlen function returns length of a string excluding null character.



String tutorial.



Library functions of string.



(68) What will be output if you will compile and execute the following c code?



void main(){

static main;

int x;

x=call(main);

clrscr();

printf("%d ",x);

getch();

}

int call(int address){

address++;

return address;

}



(a) 0

(b) 1

(c) Garbage value

(d) Compiler error

(e) None of these













Answer: (b)

Explanation:

As we know main is not keyword of c but is special type of function. Word main can be name variable in the main and other functions.



What is main function in c?



(69) What will be output if you will compile and execute the following c code?



void main(){

int a,b;

a=1,3,15;

b=(2,4,6);

clrscr();

printf("%d ",a+b);

getch();

}



(a) 3

(b) 21

(c) 17

(d) 7

(e) Compiler error













Answer: (d)

Explanation:

In c comma behaves as separator as well as operator.

a=1, 3, 15;

b= (2, 4, 6);

In the above two statements comma is working as operator. Comma enjoys least precedence and associative is left to right.



Assigning the priority of each operator in the first statement:









Hence 1 will assign to a.

Assigning the priority of each operator in the second statement:
























Operator tutorial.



(70) What will be output if you will compile and execute the following c code?



int dynamic(int,...);

void main(){

int x,y;

x=dynamic(2,4,6,8,10,12,14);

y=dynamic(3,6,9,12);

clrscr();

printf("%d %d ",x,y);

getch();

}

int dynamic(int s,...){

void *ptr;

ptr=...;

(int *)ptr+=2;

s=*(int *)ptr;

return s;



}



(a) 8 12

(b) 14 12

(c) 2 3

(d) Compiler error

(e) None of these













Answer: (a)

Explanation:

In c three continuous dots is known as ellipsis which is variable number of arguments of function. In this example ptr is generic pointer which is pointing to first element of variable number of argument. After incrementing it will point third element.



What is variable number of argument?



(71) What will be output if you will compile and execute the following c code?



int extern x;

void main()

printf("%d",x);

x=2;

getch();

}

int x=23;



(a) 0

(b) 2

(c) 23

(d) Compiler error

(e) None of these













Answer: (c)

Explanation:

extern variables can search the declaration of variable any where in the program.



Properties of extern storage class.



(72) What will be output if you will compile and execute the following c code?



void main(){

int i=0;

if(i==0){

i=((5,(i=3)),i=1);

printf("%d",i);

}

else

printf("equal");

}



(a) 5

(b) 3

(c) 1

(d) equal

(e) None of above











Answer: (c)

Explanation:



Comma operator.



Operator tutorial.



(73) What will be output if you will compile and execute the following c code?



void main(){

int a=25;

clrscr();

printf("%o %x",a,a);

getch();

}



(a) 25 25

(b) 025 0x25

(c) 12 42

(d) 31 19

(e) None of these













Answer: (d)

Explanation:

%o is used to print the number in octal number format.

%x is used to print the number in hexadecimal number format.



Note: In c octal number starts with 0 and hexadecimal number starts with 0x.



What is octal number?



What is hexadecimal number?



(74) What will be output if you will compile and execute the following c code?





#define message "union is\

power of c"

void main(){

clrscr();

printf("%s",message);

getch();

}



(a) union is power of c

(b) union ispower of c

(c) union is

Power of c

(d) Compiler error

(e) None of these













Answer: (b)

Explanation:

If you want to write macro constant in new line the end with the character \.



Preprocessor tutorial.



(75) What will be output if you will compile and execute the following c code?





#define call(x) #x

void main(){

printf("%s",call(c/c++));

}



(a)c

(b)c++

(c)#c/c++

(d)c/c++

(e)Compiler error













Answer: (d)

Explanation:

# is string operator. It converts the macro function call argument in the string. First see the intermediate file:



test.c 1:

test.c 2: void main(){

test.c 3: printf("%s","c/c++");

test.c 4: }

test.c 5:



It is clear macro call is replaced by its argument in the string format.



What is # and ##?



Preprocessor tutorial?



(75) What will be output if you will compile and execute the following c code?



void main(){

if(printf("cquestionbank"))

printf("I know c");

else

printf("I know c++");

}



(a) I know c

(b) I know c++

(c) cquestionbankI know c

(d) cquestionbankI know c++

(e) Compiler error











Answer: (c)

Explanation:

Return type of printf function is integer which returns number of character it prints including blank spaces. So printf function inside if condition will return 13. In if condition any non- zero number means true so else part will not execute.



Prototype of printf function.









Links to this post
0 comments
for loop questions in c
(1)



void main(){

char i;

clrscr();

for(i=120;i<=128;i++){

printf("%d ",i);



}

getch();

}



Output: Infinite loop



(2)



void main(){

int i;

clrscr();

for(i=0,i++,i<=5;i++,i<=2;i=0,i<=5,i+=3){

printf("%d ",i);

}

getch();

}



Output: 2

(3)



void main(){

int i=1;

clrscr();

for(;;){

printf("%d ",i);

}

getch();

}



Output: Infinite loop

(4)



void main(){

int i=2;

clrscr();

for(i=0;i<=3;i++){

static int i;

i=i+8;

}

printf("%d",i);

getch();

}



Output: 4

(5)



extern int j;

void main(){

int i=0;

clrscr();

for(i=0;i<=2;i+=1){

int j=5;

printf("%d ",j);

j++;

}

getch();

}

int j=25;



Output: 5 5 5

(6)



extern int j;

void main(){

int i=0;

clrscr();

for(i=0;i<=2;i+=1){

int j=5;

printf("%d ",j);

j++;

}

getch();

}

int j=25;



Output: 5 5 5