Interview Questions C

C


161. Difference between syntax vs logical error?
Syntax Error
These involves validation of syntax of language.
compiler prints diagnostic message.
Logical Error
logical error are caused by an incorrect algorithm or by a statement mistyped in such a way that it doesn't violet syntax of language.
difficult to find.

162. What are the facilities provided by preprocessor?
file inclusion
substitution facility
conditional compilation

163. What do the functions atoi(), itoa() and gcvt() do?
atoi() is a macro that converts integer to character.
itoa() It converts an integer to string
gcvt() It converts a floating point number to string

164. What is FILE?
FILE is a predefined data type. It is defined in stdio.h file.

165. gets() function
Question: There is a hidden problem with the following code. Can you detect it?
#include<stdio.h>

int main(void)
{
char buff[10];
memset(buff,0,sizeof(buff));

gets(buff);

printf("\n The buffer entered is [%s]\n",buff);

return 0;
}
The hidden problem with the code above is the use of the function gets(). This function accepts a string from stdin without checking the capacity of buffer in which it copies the value. This may well result in buffer overflow. The standard function fgets() is advisable to use in these cases.

166. strcpy() function
Question: Following is the code for very basic password protection. Can you break it without knowing the password?
#include<stdio.h>

int main(intargc, char *argv[])
{
int flag = 0;
charpasswd[10];

memset(passwd,0,sizeof(passwd));

strcpy(passwd, argv[1]);

if(0 == strcmp("LinuxGeek", passwd))
{
flag = 1;
}

if(flag)
{
printf("\n Password cracked \n");
}
else
{
printf("\n Incorrect passwd \n");

}
return 0;
}

	

167. Return type of main()
Question: Will the following code compile? If yes, then is there any other problem with this code?
#include<stdio.h>

void main(void)
{
char *ptr = (char*)malloc(10);

if(NULL == ptr)
{
printf("\n Malloc failed \n");
return;
}
else
{
// Do some processing

free(ptr);
}

return;
}
The code will compile error free but with a warning (by most compilers) regarding the return type of main()function. Return type of main() should be 'int' rather than 'void'. This is because the 'int' return type lets the program to return a status value. This becomes important especially when the program is being run as a part of a script which relies on the success of the program execution.

168. Memory Leak
Question: Will the following code result in memory leak?
#include<stdio.h>

void main(void)
{
char *ptr = (char*)malloc(10);

if(NULL == ptr)
{
printf("\n Malloc failed \n");
return;
}
else
{
// Do some processing
}

return;
}
Well, Though the above code is not freeing up the memory allocated to 'ptr' but still this would not cause a memory leak as after the processing is done the program exits. Since the program terminates so all the memory allocated by the program is automatically freed as part of cleanup. But if the above code was all inside a while loop then this would have caused serious memory leaks.
Note : If you want to know more on memory leaks and the tool that can detect memory leaks, read our article on Valgrind.

169. atexit with _exit
Question: In the code below, the atexit() function is not being called. Can you tell why?
#include<stdio.h>

voidfunc(void)
{
printf("\n Cleanup function called \n");
return;
}

int main(void)
{
int i = 0;

atexit(func);

for(;i<0xffffff;i++);

_exit(0);
}
This behavior is due to the use of function _exit(). This function does not call the clean-up functions like atexit() etc. If atexit() is required to be called then exit() or 'return' should be used.

170. void* and C structures
Question: Can you design a function that can accept any type of argument and returns an integer? Also, is there a way in which more than one arguments can be passed to it?
A function that can accept any type of argument looks like :
intfunc(void *ptr)
if more than one argument needs to be passed to this function then this function could be called with a structure object where-in the structure members can be populated with the arguments that need to be passed.