Top 45 C programming interview questions and answers

Introduction

In this article, you will find basic to most puzzled interview queries questions. This article is very useful for those who are preparing for an interview in IT company. Whether you are experienced or fresher, this article will cover all C programming interview questions from basic to advanced level.

1) Describe some features of C language.

C has the features of both high and low-level programming languages.
Supports machine independence, the code you compile in one computer can be executed in another machine without any error.
C is a procedural language and supports modular programming.
C is a well-structured programming language which is used to write efficient programs.

2) What is the basic syntax of a C program?

#include <stdio.h>
void main(void)
{
   printf("Hello World"); //printing a string
}

3) What is preprocessor directive?

A preprocessor directive starts from # in C and is used to modify C program. The preprocessor directive is not a part of the compiler but is an instruction to compiler before the program is executed to include header files. In short, pre-processing is done when # is used.

4) What are the different types of comments in C programming?

In C programming, we have:

Single line comments
Multi-line comments
Single line comment in C can be added using double slash //.

Example:
// This is Single line comment

Multi-Line comments in C can be added using /* at beginning and */ and ending of the comment.

Example:
/* This is
Multi line comment */

5) What is the difference between keyword and Identifier?

Keywords are the reserved words that are predefined and have special meaning and purpose whereas, identifiers are the names of user-defined functions, variables, and constants, etc.

6) What is the difference between variable and constant?

A variable is the one whose value can be changed as the name says during the execution of the program. The value of a constant cannot be changed once it is defined.

7) List and describe the use of some of the keywords in C programing language.

The following are some of the keywords in C:

while: is used to use the while loop in C.
default: is used in switch statement. When all the cases are false, the default part is executed.
int: is used to declare a variable of integer type.
void: is a data type which means nothing.

8) What are the data types in C programming? Give example.

When declaring variables data types are used to specify the type of the variable and to reserve space in memory according to type of variable. The following are some of the data types in C:

Basic data types which include: int, long, double, float, char, etc.
Void type which means nothing
Derived types for example arrays, and structures, etc.

9) What is variable declaration?.

To specify the data type and name of variable is called variable declaration. A variable can be declared in C as follows:

int blue;

Here int is data type and blue is the name of variable.

10) What is variable definition?

Declaring and initializing a variable is called definition of variable.

11) Can you declare variables of same name?

Variables in same scope cannot have same name but variables in different scope can have same names.

12) How you can display data on output screen in C?

In C programming, data can be displayed on output screen using printf() statement. The syntax of printf() is as follows:

printf(“format string”, variableName);

format string can have a message that you want to display and format specifiers and escape sequences. Format specifiers are for value of variable.

format string can have a message that you want to display and format specifiers and escape sequences. Format specifiers are for value of variable.

13) How you can read something from the output screen?

You can get user input by using the scanf() statement. The syntax of scanf() is as follows:

scanf(“format string”, &variableName);

14) How you can read strings from the output screen and display strings on output string?

Strings can be read from output screen by using the gets() function and you can display strings on output screen by using puts() statement.

15) Why & operator is used?

The & operator is an address operator. The & operator ensures to store the entered value into the corresponding memory location.

16) What are local variables in C Programming?

Local variables are the ones declared inside a function or a block (a block can be any number of statements inside if, for, switch or function). Local variables are not known outside where they are declared.

17) What are global variables in C Programming?

The global variables are declared before the main function and they are accessible from anywhere in the program. In other words, global variables are known to every function.

18) Describe different operators in C programming

In C programming, we have:

Arithmetic operators to perform arithmetic or mathematical operations which may include addition, subtraction, etc.
Logical operators to compare two conditions. The result is always of Boolean type.
Relational operators to compare values of operands.
Assignment operator to assign values to variables (identifiers).
Compound assignment operators to combine assignment and arithmetic operator to perform some arithmetic operations.

19) What is the use of logical operators in C programming?

In C programming, we have:

Logical And operator (&&)
Logical OR operator (||)
Logical not operator
Logical operators are used to combine conditions and result is of Boolean type either true or false.

20) What is difference between for and while loops?

for loop is used when you know that how many times the loop has to be executed. while loop is used when the number of iterations are not known.

21) What is the main difference between while and do while loop?

In while loop if the condition is false the control is not entered into the body of while loop. In do while loop the condition comes after the body of the loop so the body will be executed at least once.

22) What are unary operators in C programming?

Unary operators are the one that operates on one operand only for example increment and decrement operators to make an increment and decrement of one in the value of variable respectively.

23) What is the syntax of switch statement?

In switch statement we have switch key word and a condition the result of which is compared with a number of cases. At the end we have default part which is executed when all cases are false:

switch(condition)
{
case 1:
//statements
break;
case 2:
//statements
break;
canse N:
//N statements
break;
default:
//statements
}

24) What difference is between continue and break statements?

continue statement skips the current iteration of the loop on the basis of a condition and break statement breaks the control making the control move outside the body of the loop on the basis of condition.

25) What is the main difference between switch and if statement?

Both switch and if statements have multiple choices of which only one is executed. But in switch you cannot check range of values.

26) What is the use of ternary operator?

Ternary operator works with three operands. Ternary operator is a decision making construct. The first operand in ternary is the condition. If the condition is true second operand is executed else third operand is executed. Instead of if else, ternary operator can also be used.

27) What is an auto variable?

An auto variable is a local variable. When you declare a variable as automatic it will be allocated memory at the beginning of the execution of the program and memory will be de allocated when the execution is done.

28) What are the storage classes in C?

Storage classes specify how the variables will be stored in memory and where the variables are known in the program (scope of variables). Storage classes in C are:

Local variables
Global variables
Register variables
Static variables
Automatic variables

29) What is the difference between an auto and a register variable?

An auto variable is a regular variable which is allocated memory when it is created and memory is de allocated when execution of program ends so this variable is destroyed. On the other hand a register keyword is used to create a register variable which will be stored in register in RAM. Register variables are fast and are accessed directly by the compiler.

30) What is the use of static keyword?

When you create a static variable, the value of this variable will remain constant throughout the program. These variables are defined at the beginning of the program. Static variables cannot be destroyed.

31 What are different ways to declare constants in C?

Constants can be declared using the keyword const and using the #define preprocessor.

32) What is type casting?

Type casting is to convert one data type into another. In C we have two categories:

Implicit type casting done by compiler
Explicit type casting done by user

33) What is meant by modular programming?

By modular programming we mean to divide the whole program (problem) into small modules by defining functions. The program becomes reusable by dividing it into modules.

34) How can you declare functions in C?

A function can be declared by specifying the return type of the function and then the name of function with function arguments within parenthesis. The following is how you can declare function in C:

returnTypefunctionName(argument1, argumentN);

For example:

int add(int a, int b); //This is the declaration of function ended with semi colon.

35) How can you call your function? Describe some different ways to call a function?

A function can be called using its name and passing values for arguments (if any). In C we can call a function in assignment statement (if it returns a value), in printf statement (if it returns a value). If the function doesn’t return a value you can simple call the function using its name and passing arguments.

36) What is difference between function declaration and function definition? What is prototype of a function?

Function declaration also called function prototype is used to declare a function and is ended with a semi colon whereas, function definition is to define the body of the function or define the behavior or functionality of the function.

37) How a function can be called by reference?

When calling function by reference you are passing addresses of values to the formal parameters. This address is then used by function to perform specific task. Consider the example below:

int add (int *a, int *b)

Here *a, *b are the pointers holding addresses of the corresponding values or variables.

38) What is meant by recursive call?

A function calling itself is called a recursive function and this process is known as recursion or recursive call. A function can call itself inside its definition.

39) What is difference between pointer and a regular variable?

A pointer holds the address of the variable whereas a regular variable holds a value and is a named memory location to hold some value.

40)Explain dangling Pointer in C.

A dangling pointer is the one which points to the deleted memory location. Dangling pointer occurs when an objected is destroyed and the memory is de-allocated but the pointer is still pointing to the freed memory

41) What is the use of library functions in C?

Library functions are the built in or primitive functions defined at the time of manufacturing. These functions have specific tasks to perform for example printf() statement can be used to display output on the output screen. To use the library functions you have to include respective header files for example stdio.h for printf().

42) What is an auto variable?

An auto variable is a local variable. When you declare a variable as automatic it will be allocated memory at the beginning of the execution of the program and memory will be de allocated when the execution is done.

43) What is meaning of e in getche() function?

What is meaning of e in getche() function? The e in getche() stands for echo. When the user enters any character in output screen, it is displayed on the output screen or echoes on the output screen without waiting for the enter key to be pressed. The getche() function is included in the header file conio.h.

44) What are the different errors that can be occurred in your C program?

In C program, we can have syntax error because of wrong syntax, run time error also called exception that can be occurred for example multiplying any number by zero, and then we have logical errors that are the type of run time errors.

45) What do you mean by enumeration in C?

Enumeration is used to assign strings to integral constants. Enumeration is a user defined data type which can be created by using the enum keyword.

Leave a Comment