100 Best C Programming Interview Questions And Answers For Freshers

C Programming Interview Questions And Answers
Welcome to Interview Questions World.100 Best C Programming Interview Questions And Answers for freshers. If you are in final year of your graduation and student of computers, information technology, electronics and communication or science academics or just passed out and looking for IT Job as an fresher, you must and should know the C Programming language. Most of the MNC companies conducting different technical and aptitude tests including C Programming as mandatory. We hope that you know basics of programming and able to write programs using C language then this is the right place for you to know the 100 best C programming interview questions for freshers and experienced. Let's explore and enjoy the reading.




100 C Programming Interview Questions


1) What is a pointer on pointer?

It’s a pointer variable which can hold the address of another pointer variable. It de-refers twice to point to the data held by the designated pointer variable.

Eg: int x = 5, *p=&x, **q=&p;

Therefore ‘x’ can be accessed by **q.


2) Distinguish between malloc() & calloc() memory allocation.

Both allocates memory from heap area/dynamic memory. By default calloc fills the allocated memory with 0’s. 


3) What is keyword auto for?

By default every local variable of the function is automatic (auto). In the below function both the variables ‘i’ and ‘j’ are automatic variables.void f() { int i; auto int j; }

NOTE − A global variable can’t be an automatic variable. 


4) What are the valid places for the keyword break to appear.

Break can appear only with in the looping control and switch statement. The purpose of the break is to bring the control out from the said blocks. 

5) Explain the syntax for for loop.
for(expression-1;expression-2;expression-3) { //set of statements }

When control reaches for expression-1 is executed first. Then following expression-2, and if expression-2 evaluates to non-zero ‘set of statements’ and expression-3 is executed, follows expression-2. 

6) What is difference between including the header file with-in angular braces < > and double quotes “ “

If a header file is included with in < > then the compiler searches for the particular header file only with in the built in include path. If a header file is included with in “ “, then the compiler searches for the particular header file first in the current working directory, if not found then in the built in include path. 


7) How a negative integer is stored.

Get the two’s compliment of the same positive integer. Eg: 1101 (-5)

Step-1 − One’s compliment of 5 : 1010

Step-2 − Add 1 to above, giving 1011, which is -5 


8) What is a static variable?

A static local variables retains its value between the function call and the default value is 0. The following function will print 1 2 3 if called thrice.void f() { static int i; ++i; printf(“%d “,i); }

If a global variable is static then its visibility is limited to the same source code. 


9) What is a NULL pointer?

A pointer pointing to nothing is called so. Eg: char *p=NULL; 


10) What is the purpose of extern storage specifier?

Used to resolve the scope of global symbol.
Eg: main() { extern int i; Printf(“%d”,i); } int i = 20;

11) Explain the purpose of the function sprintf().

Prints the formatted output onto the character array. 


12) What is the meaning of base address of the array?

The starting address of the array is called as the base address of the array. 


13) When should we use the register storage specifier?

If a variable is used most frequently then it should be declared using register storage specifier, then possibly the compiler gives CPU register for its storage to speed up the look up of the variable. 


14) S++ or S = S+1, which can be recommended to increment the value by 1 and why?

S++, as it is single machine instruction (INC) internally. 


15) What is a dangling pointer?

A pointer initially holding valid address, but later the held address is released or freed. Then such a pointer is called as dangling pointer. 


16) What is the purpose of the keyword typedef?

It is used to alias the existing type. Also used to simplify the complex declaration of the type. 


17) What is lvalue and rvalue?
The expression appearing on right side of the assignment operator is called as rvalue. Rvalue is assigned to lvalue, which appears on left side of the assignment operator. The lvalue should designate to a variable not a constant. 


18) What is the difference between actual and formal parameters?

The parameters sent to the function at calling end are called as actual parameters while at the receiving of the function definition called as formal parameters. 


19) Can a program be compiled without main() function?

Yes, it can be but cannot be executed, as the execution requires main() function definition. 


20) What is the advantage of declaring void pointers?

When we do not know what type of the memory address the pointer variable is going to hold, then we declare a void pointer for such. 


21) Where an automatic variable is stored?

Every local variable by default being an auto variable is stored in stack memory. 


22) What is a nested structure?

A structure containing an element of another structure as its member is referred so. 


23) What is the difference between variable declaration and variable definition?

Declaration associates type to the variable whereas definition gives the value to the variable. 


24) What is a self-referential structure?

A structure containing the same structure pointer variable as its element is called as self-referential structure. 


25) Does a built-in header file contains built-in function definition?

No, the header file only declares function. The definition is in library which is linked by the linker. 


26) Explain modular programming.

Dividing the program in to sub programs (modules/function) to achieve the given task is modular approach. More generic functions definition gives the ability to re-use the functions, such as built-in library functions. 


27) What is a token?

A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol. 


28) What is a preprocessor?

Preprocessor is a directive to the compiler to perform certain things before the actual compilation process begins. 


29) Explain the use of %i format specifier w.r.t scanf().

Can be used to input integer in all the supported format. 


30) How can you print a \ (backslash) using any of the printf() family of functions.

Escape it using \ (backslash). 


31) Does a break is required by default case in switch statement?

Yes, if it is not appearing as the last case and if we do not want the control to flow to the following case after default if any. 


32) When to user -> (arrow) operator.

If the structure/union variable is a pointer variable, to access structure/union elements the arrow operator is used. 


33) What are bit fields?

We can create integer structure members of differing size apart from non-standard size using bit fields. Such structure size is automatically adjusted with the multiple of integer size of the machine. 


34) What are command line arguments?

The arguments which we pass to the main() function while executing the program are called as command line arguments. The parameters are always strings held in the second argument (below in args) of the function which is array of character pointers. 

First argument represents the count of arguments (below in count) and updated automatically by operating system.main( int count, char *args[]) { } 


35) What are the different ways of passing parameters to the functions? Which to use when?


Call by value − We send only values to the function as parameters. We choose this if we do not want the actual parameters to be modified with formal parameters but just used. 

Call by reference − We send address of the actual parameters instead of values. We choose this if we do want the actual parameters to be modified with formal parameters. 


36) What is the purpose of built-in stricmp() function.

It compares two strings by ignoring the case. 


37) Describe the file opening mode “w+”.

Opens a file both for reading and writing. If a file is not existing it creates one, else if the file is existing it will be over written. 


38) Where the address of operator (&) cannot be used?

It cannot be used on constants.

It cannot be used on variable which are declared using register storage class. 


39) Is FILE a built-in data type?

No, it is a structure defined in stdio.h. 


40) What is reminder for 5.0 % 2?

Error, It is invalid that either of the operands for the modulus operator (%) is a real number. 


41) How many operators are there under the category of ternary operators?

There is only one operator and is conditional operator (? : ). 


42) Which key word is used to perform unconditional branching?

goto 


43) What is a pointer to a function? Give the general syntax for the same.

A pointer holding the reference of the function is called pointer to a function. In general it is declared as follows.

T (*fun_ptr) (T1,T2…); 

Where T is any date type.

Once fun_ptr refers a function the same can be invoked using the pointer as follows.

fun_ptr(); [Or] (*fun_ptr)();

44) Explain the use of comma operator (,).

Comma operator can be used to separate two or more expressions.Eg: printf(“hi”) , printf(“Hello”);

45) What is a NULL statement?

A null statement is no executable statements such as ; (semicolon).

Eg: int count = 0; while( ++count<=10 ) ;

Above does nothing 10 times. 


46) What is a static function?

A function’s definition prefixed with static keyword is called as a static function. You would make a function static if it should be called only within the same source code. 


47) Which compiler switch to be used for compiling the programs using math library with gcc compiler?

Opiton –lm to be used as > gcc –lm <file.c> 


48) Which operator is used to continue the definition of macro in the next line?

Backward slash (\) is used.

E.g. #define MESSAGE "Hi, \ Welcome to C"

49) Which operator is used to receive the variable number of arguments for a function?

Ellipses (…) is used for the same. 

A general function definition looks as followsvoid f(int k,…) { } 


C Interview Questions And Answers



50) What is the problem with the following coding snippet? 

char *s1 = "hello",*s2 = "welcome"; strcat(s1,s2);

s1 points to a string constant and cannot be altered. 


51) Which built-in library function can be used to re-size the allocated dynamic memory?

realloc(). 


52) Define an array.

Array is collection of similar data items under a common name. 


53) What are enumerations?

Enumerations are list of integer constants with name. Enumerators are defined with the keyword enum. 


54) Which built-in function can be used to move the file pointer internally?

fseek() 


55) What is a variable?

A variable is the name storage. 


56) Who designed C programming language?

Dennis M Ritchie. 


57) C is successor of which programming language?

B Programming


57) What is the full form of ANSI?

American National Standards Institute. 


58) Which operator can be used to determine the size of a data type or variable?

sizeof 


59) Can we assign a float variable to a long integer variable?

Yes, with loss of fractional part. 


60) Is 068 a valid octal number?

No, it contains invalid octal digits. 


61) What it the return value of a relational operator if it returns any?

Return a value 1 if the relation between the expressions is true, else 0. 


62) How does bitwise operator XOR works.

If both the corresponding bits are same it gives 0 else 1. 


63) What is an infinite loop?

A loop executing repeatedly as the loop-expression always evaluates to true such aswhile(0 == 0) { }

64) Can variables belonging to different scope have same name? If so show an example.

Variables belonging to different scope can have same name as in the following code snippet.

int var; 
 void f() { int var; } 
 main() { int var; } 


65) What is the default value of local and global variables?


Local variables get garbage value and global variables get a value 0 by default. 


66) Can a pointer access the array?

Pointer by holding array’s base address can access the array. 


67) What are valid operations on pointers?

The only two permitted operations on pointers are 

i) Comparision 
ii) Addition/Substraction (excluding void pointers) 


68) What is a string length?

It is the count of character excluding the ‘\0’ character. 


69) What is the built-in function to append one string to another?


strcat() form the header string.h 


70) Which operator can be used to access union elements if union variable is a pointer variable?

Arrow (->) operator. 


71) Explain about ‘stdin’.
stdin in a pointer variable which is by default opened for standard input device. 


72) Name a function which can be used to close the file stream.

fclose(). 


73) What is the purpose of #undef preprocessor?

It be used to undefine an existing macro definition. 


74) Define a structure.

A structure can be defined of collection of heterogeneous data items. 


75) Name the predefined macro which be used to determine whether your compiler is ANSI standard or not?

__STDC__ 


76) What is typecasting?

Typecasting is a way to convert a variable/constant from one type to another type. 


77) What is recursion?

Function calling itself is called as recursion. 


78) Which function can be used to release the dynamic allocated memory?

free(). 


79) What is the first string in the argument vector w.r.t command line arguments?

Program name. 


80) How can we determine whether a file is successfully opened or not using fopen() function?

On failure fopen() returns NULL, otherwise opened successfully. 


81) What is the output file generated by the linker.

Linker generates the executable file. 


82) What is the maximum length of an identifier?

Ideally it is 32 characters and also implementation dependent. 


83) What is the default function call method?

By default the functions are called by value. Functions must and should be declared. 

Comment on this.

Function declaration is optional if the same is invoked after its definition. 


84) When the macros gets expanded?

At the time of preprocessing. 


85) Can a function return multiple values to the caller using return reserved word?

No, only one value can be returned to the caller. 


86) What is a constant pointer?

A pointer which is not allowed to be altered to hold another address after it is holding one. 


87) To make pointer generic for which date type it need to be declared?

Void 


88) Can the structure variable be initialized as soon as it is declared?

Yes, w.r.t the order of structure elements only. 


89) Is there a way to compare two structure variables?

There is no such. We need to compare element by element of the structure variables. 


90) Which built-in library function can be used to match a patter from the string?
Strstr() 


91) What is difference between far and near pointers?

In first place they are non-standard keywords. A near pointer can access only 2^15 memory space and far pointer can access 2^32 memory space. 

Both the keywords are implementation specific and are non-standard. 


92) Can we nest comments in a C code? 

No, we cannot. 


93) Which control loop is recommended if you have to execute set of statements for fixed number of times?

for – Loop. 


94) What is a constant?

A value which cannot be modified is called so. Such variables are qualified with the keyword const. 


95) Can we use just the tag name of structures to declare the variables for the same?

No, we need to use both the keyword ‘struct’ and the tag name. 


96) Can the main() function left empty?

Yes, possibly the program doing nothing. 


97) Can one function call another?


Yes, any user defined function can call any function. 


98) Apart from Dennis Ritchie who the other person who contributed in design of C language.

Brain Kernighan


99) What is the difference between a string and an array?

An array is an array of anything. A string is a specific kind of an array with a well-known convention to determine its length. There are two kinds of programming languages: those in which a string is just an array of characters, and those in which it’s a special type.

In C, a string is just an array of characters (type char), with one wrinkle: a C string always ends with a NUL character.

The “value” of an array is the same as the address of (or a pointer to) the first element; so, frequently, a C string and a pointer to char are used to mean the same thing.

An array can be any length. If it’s passed to a function, there’s no way the function can tell how long the array is supposed to be, unless some convention is used.

The convention for strings is NUL termination; the last character is an ASCII NUL (‘’) character. 


100) What is the difference between goto and longjmp() and setjmp()? 

A goto statement implements a local jump of program execution, and the longjmp() and setjmp() functions implement a nonlocal, or far, jump of program execution. Generally, a jump in execution of any kind should be avoided because it is not considered good programming practice to use such statements as goto and longjmp in your program.

A goto statement simply bypasses code in your program and jumps to a predefined position. To use the goto statement, you give it a labeled position to jump to. This predefined position must be within the same function. You cannot implement gotos between functions.

When your program calls setjmp(), the current state of your program is saved in a structure of type jmp_buf. Later, your program can call the longjmp() function to restore the program’s state as it was when you called setjmp().Unlike the goto statement, the longjmp() and setjmp() functions do not need to be implemented in the same function.

However, there is a major drawback to using these functions: your program, when restored to its previously saved state, will lose its references to any dynamically allocated memory between the longjmp() and the setjmp(). This means you will waste memory for every malloc() or calloc() you have implemented between your longjmp() and setjmp(), and your program will be horribly inefficient.

It is highly recommended that you avoid using functions such as longjmp() and setjmp() because they, like the goto statement, are quite often an indication of poor programming practice.


Be confident. All the best for your future interviews..!

Comments

  1. Really nice blog post.provided a helpful information.I hope that you will post more updates like thisBig Data Hadoop Online Training Hyderabad

    ReplyDelete

Post a Comment

Popular posts from this blog