Hello World!
Good morning C programmers. Here is a compilation of couple of C and C++ questions to test your C/C++ programming knowledge. Most of the time you will be facing similar questions in job interviews for Software engineer or programmer not only at the fresher level but also at a very senior level. Many companies expect you to be hands on even at senior level. C is a programming language which is easy and at the same time extremely difficult if asked about complex pointer related questions. So, one has to be extremely careful in answering such questions.
Q1. Find the o/p:
main()
{
char *ptr="hello";
char ary[]="world";
printf("PTR: &ptr=%x, ptr=%x, *ptr=%x, ptr[0]=%x\n", &ptr,ptr, *ptr, ptr[0]);
printf("ARY: &ary=%x,ary=%x, *ary=%x, ary[0]=%x\n", &ary, ary, *ary, ary[0]);
}
Ans:
PTR: &ptr=bfb86990, ptr=8048524, *ptr=68, ptr[0]=68
ARY: &ary=bfb8698a,ary=bfb8698a, *ary=77, ary[0]=77
ARY: &ary=bfb8698a,ary=bfb8698a, *ary=77, ary[0]=77
Q2. Find the o/p:
main()
{
struct node {
char a;
int b;
short c;
struct node next;
}n1;
printf("Ans1:%d\n", sizeof(n1));
return 0;
}
Ans:
This program will not compiler.
Reason: The compiler can't figure out the space needed for the structure n1.
[kongkond@jorhat ~]$ gcc Q2.c
Q2.c: In function ‘main’:
Q2.c:7: error: field ‘next’ has incomplete type
Reason: The compiler can't figure out the space needed for the structure n1.
[kongkond@jorhat ~]$ gcc Q2.c
Q2.c: In function ‘main’:
Q2.c:7: error: field ‘next’ has incomplete type
Q3. Find the o/p:
main()
{
struct node {
char a;
int b;
short c;
struct node *next;
}n1;
printf("Ans1:\n\t node=%x\n \t node+2=%x\n", (char *)&n1, (char *)&n1+2);
printf("Ans2:%d\n", sizeof(n1));
return 0;
}
Ans:
Ans1:
node=bfcc5c94
node+2=bfcc5c96
Ans2:16
node=bfcc5c94
node+2=bfcc5c96
Ans2:16
Q4. What is the difference between fopen() and open()? When do you choose to use one over the other?
Ans:
FILE * fopen(const char *restrict filename, const char *restrict mode);
--Upon successful completion fopen() returns a FILE pointer. Otherwise, NULL is returned and the global variable errno is set to indicate the error.
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
--Given a pathname for a file, open() returns a file descriptor, a small, non-negative integer for use in subsequent system calls (read(2), write(2), lseek(2), fcntl(2), etc.). The file descriptor returned by a successful call will be the lowest-numbered file descriptor not currently open for the process.
--Upon successful completion fopen() returns a FILE pointer. Otherwise, NULL is returned and the global variable errno is set to indicate the error.
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
--Given a pathname for a file, open() returns a file descriptor, a small, non-negative integer for use in subsequent system calls (read(2), write(2), lseek(2), fcntl(2), etc.). The file descriptor returned by a successful call will be the lowest-numbered file descriptor not currently open for the process.
Q5. Find the o/p:
main()
{
if(!printf("Hello ")){}
else
printf("World!");
}
Ans:
Hello World!
Q6. Find the o/p:
main()
{
struct i{
int a; //4
double b; //8
char * c; //4
char d[7]; //8
short e; //4
int f; //4
};
printf("sizeof(i)= %d", sizeof(struct i));
}
Ans:
4+8+4+8+4+4=32
Q6.1. Find the o/p:
main()
{
struct i{
int a; //4
double b; //8
char * c; //4
char d[3]; //4
short e; //4
int f; //4
};
printf("sizeof(i)= %d", sizeof(struct i));
}
Ans:
4+8+4+4+4+4=28
Q7. Find the o/p:
# include < stdio.h >
void main()
{
printf(NULL) ;
return ;
}
Ans:
> ./a.out
>
Q8. Find the o/p:
# include < stdio.h >
void main()
{
int i = 0;
printf(i) ;
return ;
}
Ans:
In newer gcc compiler this will not compile. In older ones:
> ./a.out
>
But,
# include < stdio.h >
void main()
{
int i = 0;
printf("%f\n", i) ;
return ;
}
Ans: random number like:
-0.008430
And the result of the following will be like this:
# include < stdio.h >
void main()
{
int i = 0;
printf("%f %d\n", i) ;
return ;
}
Ans:random number like:
-0.008430 134513785
> ./a.out
>
But,
# include < stdio.h >
void main()
{
int i = 0;
printf("%f\n", i) ;
return ;
}
Ans: random number like:
-0.008430
And the result of the following will be like this:
# include < stdio.h >
void main()
{
int i = 0;
printf("%f %d\n", i) ;
return ;
}
Ans:random number like:
-0.008430 134513785
Q9. Find the o/p:
#include <stdio.h>
void main()
{
printf("%s",NULL) ;
return ;
}
Ans:
> ./a.out
(null)
But,
#include <stdio.h>
void main()
{
int i = -10;
printf("%s\n",i) ;
return ;
}
will result in "Segmentation fault" if the value of i is other than zero.
Q10. Find the o/p of this last one:
main()
{
printf("%%",7);
}
Ans:
Try yourself
Q11. How do I declare an array of N pointers to functions returning pointers to functions returning pointers to characters?
Ans:
Well, first you need to know how to declare an array of N items of type T - that's
T foo[N];
Now you need to look at how to declare a pointer to function returning something, say, an object of type S. That's like this:
S (*bar)();
Now assume that S is ``pointer to function returning pointer to char''. We get
(char *) (*)() (*bar)().
So, the whole thing turns out to be (with appropriate parentheses)
(((char)(*))((*)())(((*)((foo)))())([(N)]));
If your compiler complains, break this down into subexpressions.
To call it, just use
foo[i]();
This works because, in C, declaration reflects use, but it's one of those weird distorted mirrors.
T foo[N];
Now you need to look at how to declare a pointer to function returning something, say, an object of type S. That's like this:
S (*bar)();
Now assume that S is ``pointer to function returning pointer to char''. We get
(char *) (*)() (*bar)().
So, the whole thing turns out to be (with appropriate parentheses)
(((char)(*))((*)())(((*)((foo)))())([(N)]));
If your compiler complains, break this down into subexpressions.
To call it, just use
foo[i]();
This works because, in C, declaration reflects use, but it's one of those weird distorted mirrors.
Q12. What is asmlinkage and what is it's use?
Ans:
"asmlinkage" is defined, for example, in the header file
"include/asm-i386/linkage.h" as
__attribute__((regparm(0)))
that tells compiler put all function parameters in the stack (i.e.
disables call optimization).
In simple terms, a) you are using assembly code using asmlinkage, and b) all the function parameters are pushed into the stack and optimization is disabled for this purpose.
For more details check this link.
"include/asm-i386/linkage.h" as
__attribute__((regparm(0)))
that tells compiler put all function parameters in the stack (i.e.
disables call optimization).
In simple terms, a) you are using assembly code using asmlinkage, and b) all the function parameters are pushed into the stack and optimization is disabled for this purpose.
For more details check this link.
Q13. What is output?
main()
{
printf("%x",-1<<4); }
Ans:
[root@monday kongkon]# gcc 2.c
2.c: In function âmainâ:
2.c:3: warning: incompatible implicit declaration of built-in function âprintfâ
[root@monday kongkon]# ./a.out
fffffff0[root@monday kongkon]#
(1)10=(0000 0000 0000 0001)2
(-1)10=(2's complement of binary 1)=(1's complement of 1 added with 1) =(1111 1111 1111 1110 + 1 )2=(1111 1111 1111 1111)2
Now,
(-1)10 << 4 = (1111 1111 1111 0000)2=(F F F 0)16 Note: In Computer, Negative numbers are represented in 2's complement form, which is 1's complement +1.
Link to the earlier post: Test your programming skills.2.c: In function âmainâ:
2.c:3: warning: incompatible implicit declaration of built-in function âprintfâ
[root@monday kongkon]# ./a.out
fffffff0[root@monday kongkon]#
(1)10=(0000 0000 0000 0001)2
(-1)10=(2's complement of binary 1)=(1's complement of 1 added with 1) =(1111 1111 1111 1110 + 1 )2=(1111 1111 1111 1111)2
Now,
(-1)10 << 4 = (1111 1111 1111 0000)2=(F F F 0)16 Note: In Computer, Negative numbers are represented in 2's complement form, which is 1's complement +1.
Enjoy yourself.
Regards,
Kongkon