Wednesday, February 22, 2012

pointers


1. 

What will be output of following program?
#include<stdio.h>
void main(){
   int a = 320;
   char *ptr;
   ptr =( char *)&a;
   printf("%d ",*ptr);
   getch();
}

(A) 2
(B) 320
(C) 64
(D) Compilation error
(E) None of above

Explanation: c

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. 

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) Compilation error
(E) None of above

Explanation: b
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. 

What will be output of following program?

#include<stdio.h>
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) Compilation error
(E) None of above

Explanation: a
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. 

What will be output of following program?

#include<stdio.h>
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) Compilation error
(E) None of above

Explanation:b
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. 

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) Compilation error
(E) None of above

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

6. 

What will be output of following program?

#include<stdio.h>
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 c power of c
(D) Compilation error
(E) None of above

Explanation: a
Here we are performing relational operation between two huge addresses. So first of all both a and b will normalize as:
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. 

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) Compilation error
(E) None of above

Explanation: d
Register data type stores in CPU. So it has not any memory address. Hence we cannot write &a.

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

Explanation: c
p is far pointer which size is 4 byte.
By default q is near pointer which size is 2 byte.

9. 

What will be output of following program?

#include<stdio.h>
void main(){
int a = 10;
void *p = &a;
int *ptr = p;
printf("%u",*ptr);
getch();
}

(A) 10
(B) Address
(C) 2
(D) Compilation error
(E) None of above

Explanation: a
Void pointer can hold address of any data type without type casting. Any pointer can hold void pointer without type casting.

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) Compilation error
(E) None of above

Explanation: d
Register data type stores in CPU. So it has not any memory address. Hence we cannot write &a.

11. 

What will be output of following program?

#include<stdio.h>
void main(){
char arr[10];
arr = "world";
printf("%s",arr);
getch();
}

(A) world
(B) w
(C) Null
(D) Compilation error
(E) None of above

Explanation: d
Compilation error Lvalue required
Array name is constant pointer and we cannot assign any value in constant data type after declaration.

12.

What will be output of following program?

#include<stdio.h>
#include<string.h>
void main(){
int a,b,c,d;
char *p = ( char *)0;
int *q = ( int *q)0;
float *r = ( float *)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) Compilation error
(E) None of above

Explanation: b
Address = next address
Since initial address of all data type is zero. So its
next address will be size of data type.

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) Compilation error
(E) None of above

Explanation: a
Difference of two same type of pointer is always one.

14. 

What will be output of following program?

#include<stdio.h>
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) Compilation error
(E) None of above

Explanation:c

15. 

What will be output of following program?

#include<stdio.h>
void main(){
int * p , b;
b = sizeof(p);
printf(“%d” , b);
}

(A) 2
(B) 4
(C) 8
(D) Compilation error
(E) None of above

Explanation: e

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.


16. 

What will be output of following program?

#include<stdio.h>
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) Compilation error
(E) None of above

Explanation: a

17. 

What will be output of following program?

#include<stdio.h>
void main(){
int i = 5;
int *p;
p = &i;
printf(" %u %u", *&p , &*p);
getch();
}

(A) 5 Address
(B) Address Address
(C) Address 5
(D) Compilation error
(E) None of above

Explanation: b
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.

What will be output of following program?

#include<stdio.h>
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) Compilation error
(E) None of above

Explanation: a
Within the scope of any variable, value of variable may change but its address will never change in any modification of variable.

19. 

What will be output of following program?

#include<stdio.h>
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) Compilation error
(E)None of above

Explanation: e
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 to increase the content by one at memory the location 0x5555 so now content of memory location at 0x55555 is 26
*q also means content at memory location 0x55555 which is 26

20.

What will be output of following program?

#include<stdio.h>
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) Compilation error
(E) None of above

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


C question set 6


1=

main()
{
int i = 3;
for (;i++=0;) printf("%d",i);
}
Answer:

Compiler Error: Lvalue required.
Explanation:

As we know that increment operators return rvalues and hence it cannot appear on the left hand side of an
assignment operation.

2=

main()
{
int i=10,j=20;
j = i, j?(i,j)?i:j:j;
printf("%d %d",i,j);
}
Answer:

10 10
Explanation:

The Ternary operator ( ? : ) is equivalent for if-then-else statement. So the question can be written as:
if(i,j)
      {
if(i,j)
    j = i;
else
   j = j;
}
  else
j = j;

3=

const char *a;
char* const a;
char const *a;

-Differentiate the above declarations.
Answer:

'const' applies to char * rather than 'a' ( pointer to a constant char ) 
*a='F' : illegal 
a="Hi" : legal
'const' applies to 'a' rather than to the value of a (constant pointer to char ) 
*a='F' : legal 
a="Hi" : illegal
Same as 1.

4=

main()
{
int i=4,j=7;
j = j || i++ && printf("YOU CAN");
printf("%d %d", i, j);
}
Answer:  4 1


Explanation:

The boolean expression needs to be evaluated only till the truth value of the expression is not known.
 j is not equal to zero itself means that the expression's truth value is 1. Because it is followed by || and
true || (anything) => true where (anything) will not be evaluated. So the remaining expression is not
evaluated and so the value of i remains the same. Similarly when && operator is involved in an expression,
 when any of the operands become false, the whole expression's truth value becomes false and hence the
 remaining expression will not be evaluated.
false && (anything) => false where (anything) will not be evaluated.

5=

main()
{
register int a=2;
printf("Address of a = %d",&a);
printf("Value of a   = %d",a);
}
Answer:

Compier Error: '&' on register variable 
Rule to Remember:
& (address of ) operator cannot be applied on register variables.

C question set 5


1===========

Is the following statement a declaration/definition. Find what does it mean?
int (*x)[10];
Answer:

Definition.
x is a pointer to array of(size 10) integers.
Apply clock-wise rule to find the meaning of this definition.


2===========

What is the output for the program given below
typedef enum errorType{warning, error, exception,}error;
     main()
    {
        error g1;
        g1=1;
        printf("%d",g1);
     }
Answer:

Compiler error: Multiple declaration for error
Explanation:

The name error is used in the two meanings. One means that it is a enumerator constant with value 1.
 The another use is that it is a type name (due to typedef) for enum errorType. Given a situation the
 compiler cannot distinguish the meaning of error to know in what sense the error is used:
error g1;
g1=error;
// which error it refers in each case? When the compiler can distinguish between usages then it will not
 issue error (in pure technical terms, names can only be overloaded in different namespaces).
Note:

the extra comma in the declaration,
enum errorType{warning, error, exception,}
is not an error. An extra comma is valid and is provided just for programmer's convenience.



3============


#ifdef something
int some=0;
#endif

main()
{
int thing = 0;
printf("%d %d\n", some ,thing);
}
Answer:

Compiler error : undefined symbol some
Explanation:

This is a very simple example for conditional compilation. The name something is not already known to the
compiler making the declaration int some = 0; effectively removed from the source code.


4============


#if something == 0
int some=0;
#endif

main()
{
int thing = 0;
printf("%d %d\n", some ,thing);
}
Answer:

0 0
Explanation:

This code is to show that preprocessor expressions are not the same as the ordinary expressions. If a name
 is not known the preprocessor treats it to be equal to zero.

5============

main()
{
char *p = "ayqm";
printf("%c",++*(p++));
}

Answer: z

6============

main()
{
char *p = "ayqm";
char c;
c = ++*p++;
printf("%c",c);
}
Answer:

b
Explanation:

There is no difference between the expression ++*(p++) and ++*p++. Parenthesis just works as a visual
 clue for the reader to see which expression is first evaluated.

7============

#include<stdio.h>




main()
{
char *p = "ayqm";
printf("%c...%c....%c",*p,++*(p++),*p);
}

Answer:y...z....a

8============

int aaa() {printf("Hi");}
int bbb(){printf("hello");}
iny ccc(){printf("bye");}

main()
{
int ( * ptr[3]) ();
ptr[0] = aaa;
ptr[1] = bbb;
ptr[2] =ccc;
ptr[2]();
}
Answer:

bye
Explanation:

int (* ptr[3])() says that ptr is an array of pointers to functions that takes no arguments and returns the
type int. By the assignment ptr[0] = aaa; it means that the first function pointer in the array is initialized
 with the address of the function aaa. Similarly, the other two array elements also get initialized with the
addresses of the functions bbb and ccc. Since ptr[2] contains the address of the function ccc, the call to
 the function ptr[2]() is same as calling ccc(). So it results in printing "bye".

9============
main()
{
while (strcmp("some","some\0"))
printf("Strings are not equal\n");
}
Answer:

No output
Explanation:

Ending the string constant with \0 explicitly makes no difference. So "some" and "some\0" are equivalent.
So, strcmp returns 0 (false) hence breaking out of the while loop.

10===========

main()
{
char str1[] = {'s','o','m','e'};
char str2[] = {'s','o','m','e','\0'};
while (strcmp(str1,str2))
printf("Strings are not equal\n");
}
Answer:

"Strings are not equal"
"Strings are not equal" ...
Explanation:

If a string constant is initialized explicitly with characters, '\0' is not appended automatically to the string.
 Since str1 doesn't have null termination, it treats whatever the values that are in the following positions as
 part of the string until it randomly reaches a '\0'. So str1 and str2 are not the same, hence the result.

C question set 4


1-----------------------------------------------------------------------------------------

What are the following notations of defining functions known as?
int abc(int a,float b)
{
/* some code */
}
int abc(a,b)
int a; float b;
{
/* some code*/
}
Answer:


i. ANSI C notation 
ii. Kernighan & Ritche notation (K&R Notation)


2-----------------------------------------------------------------------------------------


main()
{
char *p;
p="%d\n";
            p++;
            p++;
            printf(p-2,300);
}
Answer:

300
Explanation:
The pointer points to % since it is incremented twice and again decremented by 2, it points to '%d\n' and 300 is
 printed.


3-----------------------------------------------------------------------------------------

main()
{
 int i;
 i = abc();
 printf("%d",i);
}
abc()
{
 _AX = 1000;
}
Answer: 1000
Explanation:< blockquote>Normally the return value from the function is through the information from the
accumulator. Here _AH is the pseudo global variable denoting the accumulator. Hence, the value of the
accumulator is set 1000 so the function returns value 1000.

4-----------------------------------------------------------------------------------------

int i;
        main(){
int t;
for ( t=4;scanf("%d",&i)-t;printf("%d\n",i))
              printf("%d--",t--);
            }
// If the inputs are 0,1,2,3 find the o/p
Answer:

4--0 
3--1 
2--2
Explanation:

Let us assume some x= scanf("%d",&i)-t the values during execution will be,
t i x
4 0 -4
3 1 -2
2 2 0



5--------------------------------------------------------------------------------------------

main(){
  int a= 0;int b = 20;char x =1;char y =10;
  if(a,b,x,y)
        printf("hello");
 }
Answer:

hello
Explanation:

The comma operator has associativity from left to right. Only the rightmost value is returned and the
 other values are evaluated and ignored. Thus the value of last variable y is returned to check in if.
 Since it is a non zero value if becomes true so, "hello" will be printed.


6----------------------------------------------------------------------------------------------

main(){
 unsigned int i;
 for(i=1;i>-2;i--)
 printf("c aptitude");
}
Explanation:

i is an unsigned integer. It is compared with a signed value. Since the both types doesn't match, signed
is promoted to unsigned value. The unsigned equivalent of -2 is a huge value so condition becomes false and
control comes out of the loop.



7------------------------------------------------------------------------------------------------

func(a,b)
int a,b;
{
 return( a= (a==b) );
}
main()
{
int process(),func();
printf("The value of process is %d !\n ",process(func,3,6));
}
process(pf,val1,val2)
int (*pf) ();
int val1,val2;
{
return((*pf) (val1,val2));
 }


Answer: The value if process is 0 !
Explanation:
The function 'process' has 3 parameters - 1, a pointer to another function 2 and 3, integers. When this
function is invoked from main, the following substitutions for formal parameters take place: func for pf, 3 for
 val1 and 6 for val2. This function returns the result of the operation performed by the function 'func'.
 The function func has two integer parameters. The formal parameters are substituted as 3 for a and 6 for b.
since 3 is not equal to 6, a==b returns 0. therefore the function returns 0 which in turn is returned by the
function 'process'.


8-----------------------------------------------------------------------------------------------

void main()
{
void *v;
int integer=2;
int *i=&integer;
v=i;
printf("%d",(int*)*v);
}
Answer:

Compiler Error. We cannot apply indirection on type void*.
Explanation:

Void pointer is a generic pointer type. No pointer arithmetic can be done on it. Void pointers are normally used for,
Passing generic pointers to functions and returning such pointers.
As a intermediate pointer type.
Used when the exact pointer type will be known at a later point of time.

9--------------------------------------------------------------------------------------------------

void main()
{
int i=i++,j=j++,k=k++;
printf("%d%d%d",i,j,k);
}
Answer:

Garbage values.
Explanation:

An identifier is available to use in program code from the point of its declaration. So expressions such
 as i = i++ are valid statements. The i, j and k are automatic variables and so they contain some garbage
 value. Garbage in is garbage out (GIGO).


10---------------------------------------------------------------------------------------------------

void main()
{
static int i=i++, j=j++, k=k++;
printf("i = %d j = %d k = %d", i, j, k);
}
Answer:

i = 1 j = 1 k = 1
Explanation:

Since static variables are initialized to zero by default.

C question set 3


(Q-1)

main()
{
printf("%d", out);
}
int out=100;
Answer:

Compiler error: undefined symbol out in function main.
Explanation:

The rule is that a variable is available for use from the point of declaration. Even though a is a global
variable, it is not available for main. Hence an error.


(Q-2)

main()
{
 extern out;
 printf("%d", out);
}
 int out=100;
Answer:

100
Explanation:

This is the correct way of writing the previous program.

(Q-3)

main( )
{
  int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};
  printf("%u %u %u %d \n",a,*a,**a,***a);
 printf("%u %u %u %d \n",a+1,*a+1,**a+1,***a+1);
  }
Answer:
100, 100, 100, 2 
114, 104, 102, 3

Explanation:

The given array is a 3-D one. It can also be viewed as a 1-D array.
2 4 7 8 3 4 2 2 2 3 3 4
100 102 104 106 108 110 112 114 116 118 120 122
thus, for the first printf statement a, *a, **a give address of first element . since the indirection ***a gives
the value. Hence, the first line of the output.

for the second printf a+1 increases in the third dimension thus points to value at 114, *a+1 increments in second
dimension thus points to 104, **a +1 increments the first dimension thus points to 102 and ***a+1 first gets the
value at first location and then increments it by 1. Hence, the output.

(Q-4)

main( )
{
 static int  a[ ]   = {0,1,2,3,4};
 int  *p[ ] = {a,a+1,a+2,a+3,a+4};
 int  **ptr =  p;
 ptr++;
 printf("\n %d  %d  %d", ptr-p, *ptr-a, **ptr);
 *ptr++;
 printf("\n %d  %d  %d", ptr-p, *ptr-a, **ptr);
 *++ptr;
 printf("\n %d  %d  %d", ptr-p, *ptr-a, **ptr);
 ++*ptr;
printf("\n %d  %d  %d", ptr-p, *ptr-a, **ptr);
}



Answer:
111 
222 
333 
344
Explanation:

Let us consider the array and the two pointers with some address
a
0 1 2 3 4
100 102 104 106 108
p
100 102 104 106 108
1000 1002 1004 1006 1008
ptr
1000
2000
After execution of the instruction ptr++ value in ptr becomes 1002, if scaling factor for integer is 2 bytes.
 Now ptr - p is value in ptr - starting location of array p, (1002 - 1000) / (scaling factor) = 1,
 *ptr - a = value at address pointed by ptr - starting value of array a, 1002 has a value 102 so the value
is (102 - 100)/(scaling factor) = 1, **ptr is the value stored in the location pointed by the pointer of
ptr = value pointed by value pointed by 1002 = value pointed by 102 = 1. Hence the output of the firs printf is 1, 1, 1.

After execution of *ptr++ increments value of the value in ptr by scaling factor, so it becomes1004. Hence,
 the outputs for the second printf are ptr - p = 2, *ptr - a = 2, **ptr = 2.

After execution of *++ptr increments value of the value in ptr by scaling factor, so it becomes1004. Hence,
 the outputs for the third printf are ptr - p = 3, *ptr - a = 3, **ptr = 3.

After execution of ++*ptr value in ptr remains the same, the value pointed by the value is incremented by the
 scaling factor. So the value in array p at location 1006 changes from 106 10 108,. Hence, the outputs for the
 fourth printf are ptr - p = 1006 - 1000 = 3, *ptr - a = 108 - 100 = 4, **ptr = 4.

(Q-5)

main( )
{
 void *vp;
 char ch = 'g', *cp = "goofy";
 int j = 20;
 vp = &ch;
 printf("%c", *(char *)vp);
 vp = &j;
 printf("%d",*(int *)vp);
 vp = cp;
 printf("%s",(char *)vp + 3);
}
Answer:

g20fy
Explanation:

Since a void pointer is used it can be type casted to any other type pointer. vp = &ch stores address of char ch
 and the next statement prints the value stored in vp after type casting it to the proper data type pointer.
 the output is 'g'. Similarly the output from second printf is '20'. The third printf statement type casts it
 to print the string from the 4th value hence the output is 'fy'.


(Q-6)

main ( )
{
 static char *s[ ]  = {"black", "white", "yellow", "violet"};
 char **ptr[ ] = {s+3, s+2, s+1, s}, ***p;
 p = ptr;
 **++p;
 printf("%s",*--*++p + 3);
}
Answer:

ck
Explanation:

In this problem we have an array of char pointers pointing to start of 4 strings. Then we have ptr which is a pointer to a pointer of type char and
 a variable p which is a pointer to a pointer to a pointer of type char. p hold the initial value of ptr, i.e. p = s+3. The next statement increment
value in p by 1 , thus now value of p = s+2. In the printf statement the expression is evaluated *++p causes gets value s+1 then the pre decrement is
executed and we get s+1-1 = s . the indirection operator now gets the value from the array of s and adds 3 to the starting address. The string is
 printed starting from this position. Thus, the output is 'ck'.


(Q-7)

main()
{
 int  i, n;
 char *x = "girl";
 n = strlen(x);
 *x = x[n];
 for(i=0; i‹n; ++i)
   {
      printf("%s\n",x);
      x++;
   }
 }

Answer:

(blank space) 
irl 
rl 
l
Explanation:

Here a string (a pointer to char) is initialized with a value "girl". The strlen function returns the length
 of the string, thus n has a value 4. The next statement assigns value at the nth location ('\0') to the first
location. Now the string becomes "\0irl" . Now the printf statement prints the string after each iteration it
increments it starting position. Loop starts from 0 to 4. The first time x[0] = '\0' hence it prints nothing
 and pointer value is incremented. The second time it prints from x[1] i.e "irl" and the third time it
 prints "rl" and the last time it prints "l" and the loop terminates.

(Q-8)

main()
{
int i=-1;
+i;
printf("i = %d, +i = %d \n",i,+i);
}
Answer:

i = -1, +i = -1
Explanation:

Unary + is the only dummy operator in C. Where-ever it comes you can just ignore it just because it has no
 effect in the expressions (hence the name dummy operator).

(Q-9)

What are the files which are automatically opened when a C file is executed?
Answer: stdin, stdout, stderr (standard input,standard output,standard error).


(Q-10)

main(){
  int * j;
  void fun(int **);
  fun(&j);
 }
 void fun(int **k) {
  int a =0;
  /* add a stmt here*/
 }
Answer:   *k = &a
Explanation:

The argument of the function is a pointer to a pointer.

C question set 2

(Q-6)

main()
{
   static char names[5][20]={"pascal","ada","cobol","fortran","perl"};
    int i;
    char *t;
    t=names[3];
    names[3]=names[4];
    names[4]=t;
    for (i=0;i<=4;i++)
    printf("%s",names[i]);
}
Answer: Compiler error: Lvalue required in function main
Explanation:

Array names are pointer constants. So it cannot be modified.

(Q-7)

void main()
{
int i=5;
printf("%d",i++ + ++i);
}
Answer: Output Cannot be predicted exactly.
Explanation:

Side effects are involved in the evaluation of i

(Q-8)

main()
{
int i;
printf("%d",scanf("%d",&i));  // value 10 is given as input here
}

Answer:1

Explanation:  Scanf returns number of items successfully read and not 1/0. Here 10 is given as input which should
have been scanned successfully. So number of items read is 1

(Q-9)

#define f(g,g2) g##g2
main()
{
int var12=100;
printf("%d",f(var,12));
}
Answer: 100


(Q-10)

main()
{
int i=0;

for(;i++;printf("%d",i)) ;
printf("%d",i);
}
Answer:  1
Explanation:  before entering into the for loop the checking condition is "evaluated". Here it evaluates to 0
(false) and comes out of the loop, and i is incremented (note the semicolon after the for loop).




c question set 1


(Q-1)
#include<stdio.h>
main()
{
char *p="hai friends",*p1;
p1=p;
while(*p!='\0') ++*p++;
printf("%s   %s",p,p1);
}

Answer: ibj!gsjfoet

Explanation:

++*p++ will be parse in the given order
*p that is value at the location currently pointed by p will be taken
++*p the retrieved value will be incremented
when ; is encountered the location will be incremented that is p++ will be executed

Hence, in the while loop initial value pointed by p is 'h', which is changed to 'i' by executing ++*p and
pointer moves to point, 'a' which is similarly changed to 'b' and so on. Similarly blank space is converted
to '!'. Thus, we obtain value in p becomes “ibj!gsjfoet” and since p reaches '\0' and p1 points to p thus
p1doesnot print anything.




(Q-2)

#include‹stdio.h›
#define a 10
main()
{
#define a 50
printf("%d",a);
}

Answer:  50

Explanation:
The preprocessor directives can be redefined anywhere in the program. So the most recently assigned value will be taken.


(Q-3)
#define clrscr() 100
main()
{
clrscr();
printf("%d\n",clrscr());
}

Answer:  100

Explanation:
Preprocessor executes as a seperate pass before the execution of the compiler. So textual replacement of clrscr()
to 100 occurs.The input  program to compiler looks like this :
main()
{
    100;
    printf("%d\n",100);
}

Note:

100; is an executable statement but with no action. So it doesn't give any problem


(Q-4)
main()
{
 int i=400,j=300;
 printf("%d..%d");
}
Answer:  400..300
Explanation:
printf takes the values of the first two assignments of the program. Any number of printf's may be given.
 All of them take only the first two values. If more number of assignments given in the program,then printf 
will take garbage values.


(Q-5)
main()
{
    int i=1;
    while (i<=5)
    {
       printf("%d",i);
       if (i>2)
 goto here;
       i++;
    }
}
fun()
{
   here:
     printf("PP");
}

Answer: Compiler error: Undefined label 'here' in function main

Explanation:
Labels have functions scope, in other words The scope of the labels is limited to functions . The label 'here' is
available in function fun() Hence it is not visible in function main.