© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

1. How do you print an address?

Ans:
The safest way is to use printf() (or fprintf() or sprintf()) with the %P specification. That prints a void pointer (void*). Different compilers might print a pointer with different formats. Your compiler will pick a format that’s right for your environment.
If you have some other kind of pointer (not a void*) and you want to be very safe, cast the pointer to a void*:
printf( %Pn, (void*) buffer );

Ex - 1:  What is the output?
main()
{
 char *p;
 int *q;
 long *r;
 p=q=r=0;
 p++;
 q++;
 r++;
 printf("%p...%p...%p",p,q,r);
}

Ans:
0001...0002...0004

Explanation:
++ operator  when applied to pointers increments address according to their corresponding data-types.

Ex - 2 what is the output?:

main(int argc, char **argv)
{
 printf("enter the character");
 getchar();
 sum(argv[1],argv[2]);
}
sum(num1,num2)
int num1,num2;
{
 return num1+num2;
}

Ans:
Compiler error.

Explanation:
argv[1] & argv[2] are strings. They are passed to the function sum without converting it to integer values. 


SLogix Student Projects
bottom