C PROGRAMMING INTERVIEW QUESTION AND ANSWER

C-Programming-Interview-Questions

What is debugging?

Debugging is the process of identifying errors within a program. During program compilation, errors that are found will stop the program from executing completely. At this state, the programmer would look into the possible portions where the error occurred. Debugging ensures the removal of errors, and plays an important role in ensuring that the expected program output is met.

When is the “void” keyword used in a function?

When declaring functions, you will decide whether that function would be returning a value or not. If that function will not return a value, such as when the purpose of a function is to display some outputs on the screen, then “void” is to be placed at the leftmost part of the function header.

When a return value is expected after the function execution, the data type of the return value is placed instead of “void”.

How do you access the values within an array?

Arrays contain a number of elements, depending on the size you gave it during variable declaration. Each element is assigned a number from 0 to number of elements-1. To assign or retrieve the value of a particular element, refer to the element number. For example: if you have a declaration that says “intscores[5];”, then you have 5 accessible elements, namely: scores[0], scores[1], scores[2], scores[3] and scores[4].

What are variables and it what way is it different from constants?

Variables and constants may at first look similar in a sense that both are identifiers made up of one character or more characters (letters, numbers and a few allowable symbols). Both will also hold a particular value.  Values held by a variable can be altered throughout the program, and can be used in most operations and computations. Constants are given values at one time only, placed at the beginning of a program. This value is not altered in the program. For example, you can assigned a constant named PI and give it a value 3.1415  .  You can then use it as PI in the program, instead of having to write 3.1415 each time you need it. 

What is syntax error?

Syntax errors are associated with mistakes in the use of a programming language. It maybe a command that was misspelled or a command that must was entered in lowercase mode but was instead entered with an upper case character. A misplaced symbol, or lack of symbol, somewhere within a line of code can also lead to syntax error.

What are header files and what are its uses in C programming?

Header files are also known as library files. They contain two essential things: the definitions and prototypes of functions being used in a program.

Simply put, commands that you use in C programming are actually functions that are defined from within each header files. Each header file contains a set of functions.

For example:

stdio.h is a header file that contains definition and prototypes of commands like printf and scanf. 

Can the curly brackets { } be used to enclose a single line of code?

While curly brackets are mainly used to group several lines of codes, it will still work without error if you used it for a single line. Some programmers prefer this method as a way of organizing codes to make it look clearer, especially in conditional statements.

Difference between compilers and interpreters ?

Compilers and interpreters often deal with how program codes are executed.

Interpreters execute program codes one line at a time, while compilers take the program as a whole and convert it into object code, before executing it.

The key difference here is that in the case of interpreters, a program may encounter syntax errors in the middle of execution, and will stop from there. On the other hand, compilers check the syntax of the entire program and will only proceed to execution when no syntax errors are found.

What is the default value of local and global variables?

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

What are enumerations?

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

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.

What is a NULL pointer?

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

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.

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

How a negative integer is stored.

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

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

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

Which of the following operators is incorrect and why? ( >=, <=, <>, ==)

<> is incorrect. While this operator is correctly interpreted as “not  equal to” in writing conditional statements, it is not the proper operator to be used in C programming. Instead, the operator  !=  must be used to indicate “not equal to” condition.

What is a nested loop?

A nested loop is a loop that runs within another loop. Put it in another sense, you have an inner loop that is inside an outer loop. In this scenario, the inner loop is performed a number of times as specified by the outer loop. For each turn on the outer loop, the inner loop is first performed.

What is the modulus operator?

The modulus operator outputs the remainder of a division. It makes use of the percentage (%) symbol. For example: 10 % 3 = 1, meaning when you divide 10 by 3, the remainder is 1.

What is the difference between the = symbol and == symbol?

The = symbol is often used in mathematical operations. It is used to assign a value to a given variable. On the other hand, the == symbol, also known as “equal to” or “equivalent to”, is a relational operator that is used to compare two values.

What is the use of a ‘\0’ character?

It is referred to as a terminating null character, and is used primarily to show the end of a string value.

In C programming, how do you insert quote characters (‘ and “) into the output screen?

This is a common problem for beginners because quotes are normally part of a printf statement. To insert the quote character as part of the output, use the format specifiers \’ (for single quote), and \” (for double quote).

Differentiate Source Codes from Object Codes

Source codes are codes that were written by the programmer. It is made up of the commands and other English-like keywords that are supposed to instruct the computer what to do. However, computers would not be able to understand source codes. Therefore, source codes are compiled using a compiler. The resulting outputs are object codes, which are in a format that can be understood by the computer processor. In C programming, source codes are saved with the file extension .C, while object codes are saved with the file extension .OBJ

What is variable initialization and why is it important?

This refers to the process wherein a variable is assigned an initial value before it is used in the program. Without initialization, a variable would have an unknown value, which can lead to unpredictable outputs when used in computations or other operations.

What is difference between uninitialized pointer and null pointer?

An uninitialized pointer is a pointer which points unknown memory location while null pointer is pointer which points a null value or base address of segment. For example:

int *p;   //Uninitialized pointer

int *q= (int *)0;  //Null pointer

#include<stdio.h>

int *r=NULL;   //Null pointer

What will be output of following c program?

#include<string.h>

#include<stdio.h>

int main(){

char *p;  //Uninitialized pointer

char *q=NULL;   //Null pointer;

strcpy(p,”cquestionbank”);

strcpy(q,”cquestionbank”);

 

printf(“%s  %s”,p,q);

return 0;

}

Output: cquestionbank (null)

What is size of void pointer?

Size of any type of pointer in c is independent of data type which is pointer is pointing i.e. size of all type of pointer (near) in c is two byte either it is char pointer, double pointer, function pointer or null pointer.  Void pointer is not exception of this rule and size of void pointer is also two byte.

What is the meaning of prototype of a function?

Prototype of a function

Declaration of function is known as prototype of a function. Prototype of a function means

(1) What is return type of function?

(2) What parameters are we passing?

(3) For example prototype of printf function is:

int printf(const char *, …);

I.e. its return type is int data type, its first parameter constant character pointer and second parameter is ellipsis i.e. variable number of arguments.

Why we use do-while loop in c?

It is also called as post tested loop. It is used when it is necessary to execute the loop at least one time. Syntax:

do {

Loop body

} while (Expression);

Example:

int main(){

int num,i=0;

do{

printf(“To enter press 1\n”);

printf(“To exit press  2”);

scanf(“%d”,&num);

++i;

switch(num){

case 1:printf(“You are welcome\n”);break;

default : exit(0);

}

}

while(i<=10);

return 0;

}

Output: 3 3 4 4

Explain the syntax for for loop.

Syntax:

Do you know memory representation of int a = 7 ?

Memory representation of:

signed int a=7;         (In Turbo c compiler)

signed short int a=7 (Both turbo c and Linux gcc compiler)

Binary equivalent of data 7 in 16 bit:  00000000 00000111

Data bit: 0000000 00000111 (Take first 15 bit form right side)

Sign bit: 0 (Take leftmost one bit)

First eight bit of data bit from right side i.e. 00000111 will store in the leftmost byte from right to left side and rest seven bit of data bit i.e. 0000000 will store in rightmost byte from right to left side as shown in the following figure:

What are merits and demerits of array in c?

Merits:

(a) We can easily access each element of array.

(b) Not necessity to declare too many variables.

(c) Array elements are stored in continuous memory location.

Demerit:

(a) Wastage of memory space. We cannot change size of array at the run time.

(b) It can store only similar type of data.

What is a sequential access file?

When writing programs that will store and retrieve data in a file, it is possible to designate that file into different forms. A sequential access file is such that data are saved in sequential order: one data is placed into the file after another. To access a particular data within the sequential access file, data has to be read one data at a time, until the right one is reached.

What is a stack?

A stack is one form of a data structure. Data is stored in stacks using the FILO (First In Last Out) approach. At any particular instance, only the top of the stack is accessible, which means that in order to retrieve data that is stored inside the stack, those on the upper part should be extracted first. Storing data in a stack is also referred to as a PUSH, while data retrieval is referred to as a POP.

Some coders debug their programs by placing comment symbols on some codes instead of deleting it. How does this aid in debugging?

Placing comment symbols /* */ around a code, also referred to as “commenting out”, is a way of isolating some codes that you think maybe causing errors in the program, without deleting the code. The idea is that if the code is in fact correct, you simply remove the comment symbols and continue on. It also saves you time and effort on having to retype the codes if you have deleted it in the first place.

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.

Swap two variables without using third variable.

#include<stdio.h>

int main(){

int a=5,b=10;

//process one

a=b+a;

b=a-b;

a=a-b;

printf(“a= %d  b=  %d”,a,b);

//process two

a=5;

b=10;

a=a+b-(b=a);

printf(“\na= %d  b=  %d”,a,b);

//process three

a=5;

b=10;

a=a^b;

b=a^b;

a=b^a;

printf(“\na= %d  b=  %d”,a,b);

 

//process four

a=5;

b=10;

a=b-~a-1;

b=a+~b+1;

a=a+~b+1;

printf(“\na= %d  b=  %d”,a,b);

 

//process five

a=5,

b=10;

a=b+a,b=a-b,a=a-b;

printf(“\na= %d  b=  %d”,a,b);

return 0;

}

Write a c program to print Hello world without using any semicolon

void main(){

if(printf(“Hello world”)){

}

}

Solution: 2

void main(){

while(!printf(“Hello world”)){

}

}

Solution: 3

void main(){

switch(printf(“Hello world”)){

}

}

What is dangling pointer in c?

Dangling pointer:

If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.

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

Distinguish between malloc() & calloc() memory allocation

There are two major differences between malloc and calloc in C programming language: first, in the number of arguments. The malloc() takes a single argument, while calloc() takess two. Second, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.

Both malloc and calloc are used in C language for dynamic memory allocation they obtain blocks of memory dynamically. Dynamic memory allocation is a unique feature of C language that enables us to create data types and structures of any size and length suitable to our programs.

What is the difference between Call by Value and Call by Reference?

When using Call by Value, you are sending the value of a variable as parameter to a function, whereas Call by Reference sends the address of the variable. Also, under Call by Value, the value in the parameter is not affected by whatever operation that takes place, while in the case of Call by Reference, values can be affected by the process within the function.

How do you construct an increment statement or decrement statement in C?

There are actually two ways you can do this. One is to use the increment operator ++ and decrement operator –. For example, the statement “x++” means to increment the value of x by 1. Likewise, the statement “x –” means to decrement the value of x by 1. Another way of writing increment statements is to use the conventional + plus sign or – minus sign. In the case of “x++”, another way to write it is “x = x +1”.

What is a preprocessor?

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

Related posts

Leave a Comment