C Programming Interview Questions And Answers (2022)

1.
What do you mean by the scope of a variable?
  The scope of a variable indicates the region over which the declaration has an effect or it refers to the part of the program, where it is accessible.
2.
What is the global scope?
  Global variables are defined outside a function or any specific block. These variables hold their values all through the end of the program and are accessible within any of the functions defined in your program.




3.
What do you mean by definition of a variable?
  In the definition of a variable, space is reserved for the variable and some initial value is given to it.
4.
When we mention the prototype of a function, are we defining the function or declaring it?
  When we mention the prototype of a function, we are declaring it.
5.
What is C programming language?
  C is a general-purpose, high level, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, with a static type system which is developed by Dennis M. Ritchie.
6.
Why is C known as a mother language?
  C is known as a mother language because most of the compilers and JVMs are written in C language. The development of various languages has been influenced by C language. These languages are C++, C#, Python, Java, JavaScript, Perl, PHP, Verilog, D, Limbo and C shell of Unix etc.
7.
Define C Compiler?
  A compiler is a program that translates a source program written in some high-level programming language into machine code for some computer architecture.
8.
What is main() function in c?
  Program execution begins from the main() function. Every C program must have a main() function. When we run a program, the operating system first looks at the main() function.
9.
What are identifiers in C?
  Identifiers are the name given to the variables, functions and constants. An identifier can start with A to Z, a to z, underscore (_) and followed by letters, numbers (0 to 9) or underscore(_).
10.
Define keywords in C with examples?




  Keywords are part of the syntax or reserved words and they cannot be used as an identifier. Examples - case, break, for , goto, void, do etc. Each of the keyword is associated with specific features. These words help us to use the functionality of C language. They have special meaning to the compilers.
11.
When was C language developed?
  C language was developed in 1972 at bell laboratories of AT&T.
12.
What is void data type in c?
  void is an empty datatype which is used in place where no value is returned. Like-

1. used with a function
void welcome(char *name)
{
printf("Welcome, %s.", name);
}

2. Used as function parameter - A function having no parameter can use void as parameter.
int rand(void);
13.
Define enumerated data type in c?
  An enumerated data type is an arithmetic data type that contains integral constants. Each constant is given a name and a value. The enum keyword is used to define enumerated data type.

enum location  {  east = 1, west = 2, north = 3, south = 4 };
14.
What are the storage size of float and double?
  Size of single precision float data type is 4 bytes and double precision float data type is 8 bytes.
15.
Define constants in C?
  Constants are fixed values that define only once and does not alter later. Constants can be any data types like an integer constants, floating constants etc.
16.
What is a register storage class?
  The register storage class is used to store variable values in CPU register. The value stored in a CPU register can be accessed faster than the one that is stored in memory.
17.
How to include a file in a another file in c?
  By using the include statement, we can include one file into another.
Example - #include "filename"
18.
What are the different types of linkages?




  There are three different types of linkages - external, internal and none.
External linkage means global, non-static variables and functions.
Internal linkage means static variables and functions with file scope.
No linkage means local variables.
19.
Define size_t?
  The size_t is used to express the size of something. It is the type of the result of the sizeof operator or it is the type returned by the strlen() to indicate the number of characters in a string.
20.
Can we use a switch statement to switch on strings?
  No, the cases in a switch must either have integer constants or constant expressions.
21.
How floats are stored in binary form?
  Floating point numbers are represented in IEEE format. The IEEE format for floating point storage uses a sign bit, a mantissa and an exponent for representing the power of 2. The sign bit denotes the sign of the number - a 0 represents a positive value and a 1 denotes a negative value. The mantissa is represented in binary after converting it to its normalised form.
22.
Is it necessary that the header files should have a .h extensions?
  No. It is not necessary. They has given a .h extensions only to identify them as something different than the .c program files.
23.
What do the header files usually contain?
  Header files contain pre-processor directives like #define, structure, union and enum declarations, typedef declarations, global variable declarations and external function declarations.




24.
Will it result into an error if a header file is included twice?
  Yes, unless the header file has taken care to ensure that if already included doesn't get included again.
25.
On doing #include where are the header files searched?
  If #include using < >, the files get searched in the predefined include path. It is possible to change the predefined include path.
26.
Are the expressions *ptr++ and ++*p same?
  No, *ptr++ increments the pointer and not the value pointed by it, whereas ++*ptr increments the value being pointer to by ptr.
27.
For which purpose, we can use pointers?
  We can use pointer in lot of places, like -
Accessing array or string elements
Dynamic memory allocation
Call by reference
Implementing linked lists, trees, graphs and many other data structures.
28.
What is null pointer?
  A Null Pointer is a pointer that does not point to any memory location. For each pointer type, C defines a special pointer value. If a pointer is initialized with a NULL value, is considered as NULL Pointer.




29.
What is NULL macro?
  A NULL macro is used to represent the null pointer in source code. It has a value 0 associated with it. Null macro is defined in stdio.h and stddef.h.
30.
What is ASCII NUL character?
  The ASCII NUL character has all its bits as 0 but doesn't have any relationship with the null pointer.
31.
How many bytes are occupied by near, far and huge pointers?
  A near pointer is 2 bytes long whereas a far and a huge pointer are 4 bytes long. Note the near, far and huge pointers exist only under DOS. Under Windows and Linux every pointer is 4 bytes long.




32.
Is the NULL pointer same as an uninitialised pointer?
  No
33.
In which header file is the NULL macro defind?
  In "stdio.h" and "stddef.h" files.
34.
How many array elements can be designate in expression num[45]?
  46
35.
Which library function is used to reverse a string?
  The strrev() function is used to reverse a string.
36.
Which library function is used to find the last occurrence of a character in a string?
  The strchr() function is used to find the last occurrence of a character in a string.
37.
How are structure passing and returning by the compiler?
  When structures are passed as arguments to functions, the entire structure is pushed on the stack. To return structures a hidden argument generated by the compiler is passed to the function. This argument points to a location where the returned structure is copied.
38.
What is the difference between a structure and a union?
  In a union, complier allocates memory for the largest of all the members whereas in structure all the members get allocated memory.
39.
What is the difference between an enumeration and a set of preprocessor #defines?
  The #define has a global effect, whereas an enumeration can have an effect local to the block, if desired.




40.
Which function is more safe to use, fgets() and gets()?
  The fgets() function is more safe, because unlike fgets(), gets() cannot be told the size of the buffer into which the string supplied will be stored. As a result, there is always a possiblility of overflow of buffer.
41.
Which bitwise operator is suitable for turning off a particular bit in a number?
  The & operator.
42.
How will you use calloc() functions?
  The calloc() function needs two arguments, the number of elements to be allocated and the size of each element.

Example

p = (int*) calloc(10, sizeof(int));

The above example allocate space for a 10-integer array.

43.
Which function should be used to free the memory allocated by calloc()?
  The free() function should be used to free the memory allocated by calloc().
44.
What is Include directive?
  The 'include' directive is used to make the inclusion of any header file available to the C program. It causes the preprocessor to add the contents of the corresponding header file to the program where you have written include statement.
45.
Define Keywords?
  Keywords are the reserve words whose meaning has been already explained to the compiler. The keywords can not be used as a variable names. Example - float, case, return, void, while.
46.
What is constants?
  A constant is an entity that does not changed the value at run time.
47.
What is variables?
  Variable is an identifier, which is used to store a data values for processing. A variable can have different data values at different times during program execution.
48.
What is local variable?
  Local variable are declared inside a function, and can be used only inside that function. It is possible to have local variables with the same name in different functions.
49.
What is a pointer in C?
  A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. There are various types of pointers such as a null pointer, wild pointer, void pointer and other types of pointers.
50.
What is stdio.h?
  The header files named 'stdio.h' has the collection of input/output functions. These functions are associated with keyword input/file input and screen output/file output.
51.
What is scanf() function?
  The scanf() is a library functions. It is used to receive values of variables from keyboard.
52.
What is conio.h?
  The conio.h header file contains functions for console input/output in C programming. Functioning of conio.h can be used to clear screen, change color of text and background.




53.
What is Goto Statement?
  This is an unconditional control statement that transfers the flow of control to another part of the program.
54.
What is getch()?
  This function gets a character from console but does not echo it to the screen. When getch() occurs in a program, it indicates the program waits for user input and when user presses any key, the execution of program continue.
55.
Define clrsrc()?
  This is a library function to clear the output screen. It is pre-defined function in header file conio.h.
56.
Define switch statement?
  This is a multidirectional conditional control statement. Sometimes there is a need in program to make choice among number of alternatives. For making choice, we use the switch statement.
57.
What is Call by Value?
  In this method the value of each of the actual arguments in the calling function is copied into corresponding formal arguments of the called function.
58.
What is Call by Reference?
  In this method, the addresses of actual arguments in the calling function are copied into the formal arguments of the called function. It means, using these addresses, we would have an access to the actual arguments.
59.
What is recursion?
  A function is called recursive if a statement within the body of the function calls the same function. Recursion is the process defining something in terms of itself.
60.
What is the difference between the function rewind() and fsetpos()?
  The rewind() function repositions file pointer to streams beginning. The fsetpos() positions the file pointer of a stream at the desired position.
61.
Give some examples of software that is built using C language?
  UNIX/ Linux Operating system, Databases, Text editors, language compilers.




62.
What is the sizeof operator?
  The sizeof operator is used to get the exact size of the variable. It is a compile-time unary operator and can be applied to any data type, float type, pointer type variables.
63.
What is the union?
  The union is a user-defined data type that allows storing multiple types of data in a single unit. In this, we can access only one variable at a time as it allocates one common space for all the members of a union.
64.
What is the newline escape sequence?
  An escape sequence in C language is a sequence of characters that doesn't represent itself when used inside string literal or character. The new line escape sequence is represented by "\n". It inserts a new line on the output screen.
65.
What is typecasting?
  Typecasting is a method in C language of converting one data type to another. If we want to store the floating type value to an int type, then we need to convert the data type into another data type explicitly.
66.
How can a number be converted to a string in c?
  The sprintf() function gives an easy way to convert an integer to a string. It works the same as the printf() function, but it does not print a value directly on the console but returns a formatted string.
#include <stdio.h>
int main(void)
{
int num;
char text[20]; 

printf("Enter a number: ");
scanf("%d", &num);

sprintf(text, "%d", num);

printf("\nYou have entered: %s", text);

return 0;
}
67.
What is a C Token?
  Keywords, Constants, Special Symbols, Strings, Operators, Identifiers used in C program are referred to as C Tokens.




68.
What is an infinite loop?
  An infinite loop is a looping construct that does not terminate the loop and executes the loop forever. It is also called an indefinite loop or an endless loop.
69.
What is a nested loop?
  If a loop exists inside the body of another loop, it's called a nested loop. The depth of nested loop depends on the complexity of a problem. We can have any number of nested loops as required.
70.
What is the default function call method?
  By default the functions are called by value.
71.
What is a constant pointer?
  A constant pointer is a pointer that holds the address of only one location. It cannot change the address it contains.
71.
What is a constant pointer?
  A constant pointer is a pointer that holds the address of only one location. It cannot change the address it contains.
72.
Write a program to subtract two numbers in C?
  In the below program, we have asked the user to enter two integers. Then, we have calculated the subtraction of these two integers using the - operator and displayed it on the screen.
#include <stdio.h>
int main() {

int num1, num2, sub;

printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);

// calculating subtraction
sub = num1 - num2;

printf("%d - %d = %d", num1, num2, sub);
return 0;
}
Output of the above code:
Enter two integers: 60 22
60 - 22 = 38




73.
Write a program to reverse a string in C?
  In the given example, we have created an user defined function strrev() to reverse a string.
#include <stdio.h>  
#include <string.h>  

// Defining function strrev()
void strrev(char *str1)  
{  
// variable declaration  
int i, len, temp;  
// getting length of string
len = strlen(str1); 

// using for loop to iterate the string   
for (i = 0; i < len/2; i++)  
{  
temp = str1[i];  
str1[i] = str1[len - i - 1];  
str1[len - i - 1] = temp;  
}  
}  

int main()  
{  
char str[50]; // size of char string  
printf("\n Enter a string to be reversed: ");  
scanf("%s", str); 

// call strrev() function   
strrev(str);  
printf (" String After Reverse : %s", str);  
}  
74.
Write a program to find square root of a number in C?
  We can use the standard library function sqrt() to find the square root of a number. It accepts a single argument of type double and returns the square root of that number.
/**
 * C program to get square root of a number
 */

#include <stdio.h>
#include <math.h>

int main()
{
    double num, root;

    /* Get number input from user */
    printf("Enter any number: ");
    scanf("%lf", &num);

    /* Calculate square root of number */
    root = sqrt(num);

    /* Print output */
    printf("Square root of %.2lf : %.2lf", num, root);

    return 0;
}




75.
Write a program to reverse a number in c?
  In the given C program, we have used the while loop to find the reverse of the given number. This program takes user input and loop until num != 0 is false (0).
#include <stdio.h>
int main() {
    int num, rev = 0, rem;
    printf("Please enter an integer : ");
    scanf("%d", &num);
    while (num != 0) {
        rem = num % 10;
        rev = rev * 10 + rem;
        num /= 10;
    }
    printf("Reverse of entered number %d", rev);
    return 0;
}
Output of the above code:
Please enter an integer: 52434
Reverse of entered number 43425
76.
Write a program for addition of two numbers in C?
  In the below program, we have asked the user to enter two integers. Then, we have calculated the sum of these two integers using the + operator and displayed it on the screen.
#include <stdio.h>
int main() {

    int num1, num2, sum;

    printf("Enter two integers: ");
    scanf("%d %d", &num1, &num2);

    // calculating sum
    sum = num1 + num2;

    printf("%d + %d = %d", num1, num2, sum);
    return 0;
}
Output of the above code:
Enter first number: 52
Enter second number: 245

52 + 245 = 297
77.
Write a program to check Armstrong number in C?
  A number is called an Armstrong number if the sum of cubes of digits of the number is equal to the number itself. Here is the C program to check armstrong number using for loop.
#include <stdio.h>

void main(){
    int num,r,sum=0,temp;

    printf("Enter a number:");
    scanf("%d",&num);

    for(temp=num;num!=0;num=num/10){
         r=num % 10;
         sum=sum+(r*r*r);
    }
    if(sum==temp)
         printf("%d is an Armstrong number.\n",temp);
    else
         printf("%d is not an Armstrong number.\n",temp);

}
Output of the above code:
Enter a number:22
22 is not an Armstrong number.

Enter a number:31
31 is not an Armstrong number.

Enter a number:370
370 is an Armstrong number.




78.
Write a program to convert Celsius to Fahrenheit?
  Here is a simple program to convert Celsius to Fahrenheit using C. The given program asks the user to enter the temperature in Celsius.
#include<stdio.h>
int main()
{
   float fahrenheit, celsius;

    /* Input temperature in Celsius */
    printf("Enter temperature in Celsius: ");
    scanf("%f", &celsius);

    fahrenheit =( (celsius*9)/5)+32;
    printf("\n\nTemperature in Fahrenheit is:  %f",fahrenheit);
    return (0);
}
79.
Write a program to find prime factors of a number using while loop?
  The given C program finds the prime factors of a number using a while loop.
/* C Program to Find Prime factors of a Number using While Loop */
 
#include <stdio.h>
 
int main()
{
  	int num, x = 1, y, count; 
 
  	printf("\n Please enter any positive number to find factors :  ");
  	scanf("%d", &num);
 
 	while (x <= num)
   	{
   		count = 0;
    	if(num % x == 0)
      	{
      		y = 1;
      		while(y <= x)
      		{
      			if(x % y == 0)
      			{
      				count++;
				}
				y++;
			}
			if(count == 2)
			{
				printf("\n %d is a Prime Factor ", x);
			} 
      	}
    	x++;
   	}
  	return 0;
}  	
}
Output of the above code:
Please enter any positive number to find factors :  121

11 is a Prime Factor
80.
Write a program to find area of rectangle?
  The following C program allows a user to enter the width and height of a rectangle. Next, we are calculating the area as per the formula: Area = width * height.
// C Program to find Area of a Rectangle

#include <stdio.h>

int main()
{
    int length, width, area;

    //Input length and width of rectangle

    printf("Please enter length of rectangle: ");
    scanf("%d", &length);
    printf("Please enter width of rectangle: ");
    scanf("%d", &width);

    /* Calculate area of rectangle */
    area = length * width;

    /* Print area of rectangle */
    printf("Area of rectangle = %d", area);

    return 0;
}
Output of the above code:
Please enter length of rectangle: 45
Please enter width of rectangle: 2
Area of rectangle = 90




Related Articles

Prime factors of a number in c
Armstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative