Interview Questions C

C


171. * and ++ operators
Question: What would be the output of the following code and why?
#include<stdio.h>

int main(void)
{
char *ptr = "Linux";
printf("\n [%c] \n",*ptr++);
printf("\n [%c] \n",*ptr);

return 0;
}
The output of the above would be :
[L]

[i]
Since the priority of both '++' and '*' are same so processing of '*ptr++' takes place from right to left. Going by this logic, ptr++ is evaluated first and then *ptr. So both these operations result in 'L'. Now since a post fix '++' was applied on ptr so the next printf() would print 'i'.

172. Making changes in Code(or read-only) segment
Question: The following code seg-faults (crashes). Can you tell the reason why?
#include<stdio.h>

int main(void)
{
char *ptr = "Linux";
*ptr = 'T';

printf("\n [%s] \n", ptr);

return 0;
}
This is because, through *ptr = 'T', the code is trying to change the first byte of the string 'Linux' kept in the code (or the read-only) segment in the memory. This operation is invalid and hence causes a seg-fault or a crash.

173. Process that changes its own name
Question: Can you write a program that changes its own name when run?
Following piece of code tries to do the required :
#include<stdio.h>

int main(intargc, char *argv[])
{
int i = 0;
char buff[100];

memset(buff,0,sizeof(buff));

strncpy(buff, argv[0], sizeof(buff));
memset(argv[0],0,strlen(buff));

strncpy(argv[0], "NewName", 7);

// Simulate a wait. Check the process
// name at this point.
for(;i<0xffffffff;i++);

return 0;
}

174. Returning address of local variable
Question: Is there any problem with the following code?If yes, then how it can be rectified?
#include<stdio.h>

int* inc(intval)
{
int a = val;
a++;
return&a;
}

int main(void)
{
int a = 10;

int *val = inc(a);

printf("\n Incremented value is equal to [%d] \n", *val);

return 0;
Though the above program may run perfectly fine at times but there is a serious loophole in the function 'inc()'. This function returns the address of a local variable. Since the life time of this local variable is that of the function 'inc()' so after inc() is done with its processing, using the address of its local variable can cause undesired results. This can be avoided by passing the address of variable 'a' from main() and then inside changes can be made to the value kept at this address.

175. Processing printf() arguments
Question: What would be the output of the following code?
#include<stdio.h>

int main(void)
{
int a = 10, b = 20, c = 30;

printf("\n %d..%d..%d \n", a+b+c, (b = b*2), (c = c*2));

return 0;
}
The output of the above code would be :
110..40..60
This is because the arguments to the function are processed from right to left but are printed from left to right.

176. What is scope of a variable? How are variables scoped in C?
Scope of a variable is the part of the program where the variable may directly be accessible. In C, all identifiers are lexically (or statically) scoped. See this for more details.

177. How will you print "Hello World" without semicolon?
There can be many ways. Following are some of them.
#include<stdio.h>
int main()
{
if (printf("Hello World") ) { }
}
#include<stdio.h>
int main()
{
while(!printf("Geeks for Geeks\n")) { }
}

178. When should we use pointers in a C program?
1. To get address of a variable
2.For achieving pass by reference in C: Pointers allow different functions to share and modify their local variables.
3. To pass large structures so that complete copy of the structure can be avoided.
4. To implement "linked" data structures like linked lists and binary trees.

179. What is memory leak? Why it should be avoided
Memory leak occurs when programmers create a memory in heap and forget to delete it. Memory leaks are particularly serious issues for programs like daemons and servers which by definition never terminate.
/* Function with memory leak */
#include <stdlib.h>

void f()
{
int *ptr = (int *) malloc(sizeof(int));

/* Do some work */

return; /* Return without freeing ptr*/
}

180. What are local static variables? What is their use?