This post has few of the frequently asked C questions in an interview for the entry level or the experienced. Some of the Questions below are related to the C standard library. Question.22(argc, argv) is a good way to test the programming knowledge of a candidate in a C language. Question.23(malloc vs calloc) is an all time favorite for the interviewers. This question would be asked to test the candidate deeply once he is able to answer the simple questions. Question.24(automatic variables) would test an important concept. If the candidate is able to answer that automatic variables are the local variable by default, one can assume to a fair extent that the candidate has basics of C language. Question.25 is a difficult question if the candidate is not aware of the gets and fgets functions in C language. It is easy for the candidate to guess the answer if he/she knew about usage of gets, fgets. Question.26 tests whether the candidate is aware of realloc function. See all C interview questions in this blog in one page.
- What do the ‘c’ and ‘ v ‘ in argc and argv stand for? Explain their purpose?
- What are the differences between malloc() and calloc()?
- Where are the auto variables stored?
- Out of fgets( ) and gets( ) which function is safer to use and why?
- How can you increase the size of a dynamically allocated array?
- What do the ‘c’ and ‘ v ‘ in argc and argv stand for? Explain their purpose? In C, we can supply arguments to ‘main’ function. The arguments that we pass to main ( ) at command prompt are called command line arguments. These arguments are supplied at the time of invoking the program.
- What are the differences between malloc() and calloc()?
- Where are the auto variables stored?
- Out of fgets( ) and gets( ) which function is safer to use and why?
- How can you increase the size of a dynamically allocated array?
The main ( ) function can take arguments as: main(int argc, char *argv[]) { }
The first argument argc is known as ‘argument counter’. It represents the number of arguments in the command line. The second argument argv is known as ‘argument vector’. It is an array of char type pointers that points to the command line arguments. Size of this array will be equal to the value of argc.
Example: at the command prompt if we give:
C:\> fruit.exe apple mango
then
argc would contain value 3
argv [0] would contain base address of string ” fruit.exe” which is the command name that invokes the program.
argv [1] would contain base address of string “apple”
argv [2] would contain base address of string “mango”
here apple and mango are the arguments passed to the program fruit.exe
Program:
#include <stdio.h>
int main(int argc, char *argv[]) {
int n;
printf("Following are the arguments entered in the command line");
for (n = 0; n < argc; n++) {
printf("\n %s", argv[n]);
}
printf("\n Number of arguments entered are\n %d\n", argc);
return 0;
}
Output:
Following are the arguments entered in
the command line
C:\testproject.exe
apple
mango
Number of arguments entered are
3
Allocation of memory at the time of execution is called dynamic memory allocation. It is done using the standard library functions malloc() and calloc().
It is defined in “stdlib.h”.
malloc(): used to allocate required number of bytes in memory at runtime. It takes one argument, viz.
size in bytes to be allocated.
Syntax:
void * malloc(size_t size);
Example:
a = (int*) malloc(4);
4 is the size (in bytes) of memory to be allocated.
calloc(): used to allocate required number of bytes in memory at runtime. It needs two arguments viz.,
1. total number of data and
2. size of each data.
Syntax:
void * calloc(size_t nmemb, size_t size);
Example:
a = (int*) calloc(8, sizeof(int));
Here sizeof indicates the size of the data type and 8 indicates that we want to reserve space for storing 8 integers.
Differences between malloc() and calloc() are:
1. Number of arguments differ.
2. By default, memory allocated by malloc() contains garbage values. Whereas memory allocated by calloc() contains all zeros.
Back to top
Main memory and CPU registers are the two memory locations where auto variables are stored. Auto variables are defined under automatic storage class. They are stored in main memory. Memory is allocated to an automatic variable when the block which contains it is called and it is de-allocated at the completion of its block execution.
Auto variables:
Storage : main memory.
Default value : garbage value.
Scope : local to the block in which the variable is defined.
Lifetime : till the control remains within the block in which the variable is defined.
Back to top
Out of functions fgets( ) and gets( ), fgets( ) is safer to use. gets( ) receives a string from the keyboard and it is terminated only when the enter key is hit. There is no limit for the input string. The string can be too long and may lead to buffer overflow.
Example:
gets(s) /* s is the input string */
Whereas fgets( ) reads string with a specified limit, from a file and displays it on screen.The function fgets() takes three arguments.
First argument : address where the string is stored.
Second argument : maximum length of the string.
Third argument : pointer to a FILE.
Example:
fgets(s,20,fp); /* s: address of the string, 20: maximum length of string, fp: pointer to a file */
The second argument limits the length of string to be read. Thereby it avoids overflow of input buffer. Thus fgets( ) is preferable to gets( ).
Back to top
realloc(): This function is used to increase or decrease the size of any dynamic memory which is
allocated using malloc() or calloc() functions.
Syntax: void *realloc(void *ptr, size_t newsize);
The first argument ‘ptr’ is a pointer to the memory previously allocated by the malloc or calloc functions. The second argument ‘newsize’ is the size in bytes, of a new memory region to be allocated by realloc. This value can be larger or smaller than the previously allocated memory.
The realloc function adjusts the old memory region if newsize is smaller than the size of old memory.
If the newsize is larger than the existing memory size, it increases the size by copying the contents of old memory region to new memory region. The function then deallocates the old memory region. realloc function is helpful in managing a dynamic array whose size may change during execution.
Example: a program that reads input from standard input may not know the size of data in advance. In this case, dynamically allocated array can be used so that it is possible allocate the exact amount of memory using realloc function.
Back to top
| Previous | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | Next |
void * malloc(size_t size);
