C interview questions

Top most important C interview questions and answers by Experts:

Here is a list of Top most important C interview questions and answers by Experts.If you want to download C interview questions pdf free ,you can register with RVH techguru. Our experts prepared these C interview questions to accommodate freshers level to most experienced level technical interviews.

If you want to become an expert in C ,Register for C online training here.

 

1) 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?.

2) 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.

3) What is the equivalent code of the following statement in WHILE LOOP format?
[c]
for (a=1; a<=100; a++)
printf (“%d\n”, a * a);
[/c]
[c]
a=1;
while (a<=100) {
printf (“%d\n”, a * a);
a++;
}
[/c]

4) What is spaghetti programming?
Spaghetti programming refers to codes that tend to get tangled and overlapped throughout the program. This unstructured approach to coding is usually attributed to lack of experience on the part of the programmer. Spaghetti programing makes a program complex and analyzing the codes difficult, and so must be avoided as much as possible.

5) 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).

6) 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.

7) 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.

8) 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.

9) 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.

10) 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.

11) Can I use “int” data type to store the value 32768? Why?
No. “int” data type is capable of storing values from -32768 to 32767. To store 32768, you can use “long int” instead. You can also use “unsigned int”, assuming you don’t intend to store negative values.

12) Can two or more operators such as \n and \t be combined in a single line of program code
Yes, it’s perfectly valid to combine operators, especially if the need arises. For example: you can have a code like ” printf (“Hello\n\n\’World\’”) ” to output the text “Hello” on the first line and “World” enclosed in single quotes to appear on the next two lines.

13) Why is it that not all header files are declared in every C program?
The choice of declaring a header file at the top of each C program would depend on what commands/functions you will be using in that program. Since each header file contains different function definitions and prototype, you would be using only those header files that would contain the functions you will need. Declaring all header files in every program would only increase the overall file size and load of the program, and is not considered a good programming style.

14) 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”.

15) What are compound statements?
Compound statements are made up of two or more program statements that are executed together. This usually occurs while handling conditions wherein a series of statements are executed when a TRUE or FALSE is evaluated. Compound statements can also be executed within a loop. Curly brackets { } are placed before and after compound statements.

16) Write a loop statement that will show the following output:
1
12
123
1234
12345
[c]
for (a=1; a<=5; i++) {
for (b=1; b<=a; b++)
printf(“%d”,b);
printf(“\n”);
}
[/c]

17) What is wrong in this statement? scanf(“%d”,whatnumber);
An ampersand & symbol must be placed before the variable name whatnumber. Placing & means whatever integer value is entered by the user is stored at the “address” of the variable name. This is a common mistake for programmers, often leading to logical errors.

18) How do you generate random numbers in C?
Random numbers are generated in C using the rand() command. For example: anyNum = rand() will generate any integer number beginning from 0, assuming that anyNum is a variable of type integer.

19) What could possibly be the problem if a valid function name such as tolower() is being reported by the C compiler as undefined?
The most probable reason behind this error is that the header file for that function was not indicated at the top of the program. Header files contain the definition and prototype for functions and commands used in a C program. In the case of “tolower()”, the code “#include ” must be present at the beginning of the program.

20) What does the format %10.2 mean when included in a printf statement?
This format is used for two things: to set the number of spaces allotted for the output number and to set the number of decimal places. The number before the decimal point is for the allotted space, in this case it would allot 10 spaces for the output number. If the number of space occupied by the output number is less than 10, addition space characters will be inserted before the actual output number. The number after the decimal point sets the number of decimal places, in this case, it’s 2 decimal spaces.

21) What is wrong with this statement? myName = “Robin”;
You cannot use the = sign to assign values to a string variable. Instead, use the strcpy function. The correct statement would be: strcpy(myName, “Robin”);

22) How do you determine the length of a string value that was stored in a variable?
To get the length of a string value, use the function strlen(). For example, if you have a variable named FullName, you can get the length of the stored string value by using this statement: I = strlen(FullName); the variable I will now have the character length of the string value.

23) Is it possible to initialize a variable at the time it was declared?
Yes, you don’t have to write a separate assignment statement after the variable declaration, unless you plan to change it later on. For example: char planet[15] = “Earth”; does two things: it declares a string variable named planet, then initializes it with the value “Earth”.

24) What are the different file extensions involved when programming in C?
Source codes in C are saved with .C file extension. Header files or library files have the .H file extension. Every time a program source code is successfully compiled, it creates an .OBJ object file, and an executable .EXE file.

25) What are reserved words?
Reserved words are words that are part of the standard C language library. This means that reserved words have special meaning and therefore cannot be used for purposes other than what it is originally intended for. Examples of reserved words are int, void, and return.

26) What are linked list?
A linked list is composed of nodes that are connected with another. In C programming, linked lists are created using pointers. Using linked lists is one efficient way of utilizing memory for storage.

27) What are binary trees?
Binary trees are actually an extension of the concept of linked lists. A binary tree has two pointers, a left one and a right one. Each side can further branch to form additional nodes, which each node having two pointers as well.

28) Not all reserved words are written in lowercase. TRUE or FALSE?
FALSE. All reserved words must be written in lowercase; otherwise the C compiler would interpret this as unidentified and invalid.

29) What is wrong with this program statement? void = 10;
The word void is a reserved word in C language. You cannot use reserved words as a user-defined variable.

30) Is this program statement valid? INT = 10.50;
Assuming that INT is a variable of type float, this statement is valid. One may think that INT is a reserved word and must not be used for other purposes. However, recall that reserved words are express in lowercase, so the C compiler will not interpret this as a reserved word.

31) What is a newline escape sequence?
A newline escape sequence is represented by the \n character. This is used to insert a new line when displaying data in the output screen. More spaces can be added by inserting more \n characters. For example, \n\n would insert two spaces. A newline escape sequence can be placed before the actual output expression or after.

32) What is output redirection?
It is the process of transferring data to an alternative output source other than the display screen. Output redirection allows a program to have its output saved to a file. For example, if you have a program named COMPUTE, typing this on the command line as COMPUTE >DATA can accept input from the user, perform certain computations, then have the output redirected to a file named DATA, instead of showing it on the screen.

33) What is the difference between functions abs() and fabs()?
These 2 functions basically perform the same action, which is to get the absolute value of the given value. Abs() is used for integer values, while fabs() is used for floating type numbers. Also, the prototype for abs() is under , while fabs() is under .

34) Write a simple code fragment that will check if a number is positive or negative.
[c]
If (num>=0)
printf(“number is positive”);
else
printf (“number is negative”);
[/c]

35) What does the function toupper() do?
It is used to convert any letter to its upper case mode. Toupper() function prototype is declared in . Note that this function will only convert a single character, and not an entire string.

36) Which function in C can be used to append a string to another string?
The strcat function. It takes two parameters, the source string and the string value to be appended to the source string.

37) Dothese two program statements perform the same output? 1) scanf(“%c”, &letter); 2) letter=getchar()
Yes, they both do the exact same thing, which is to accept the next key pressed by the user and assign it to variable named letter.

38) What is the difference between text files and binary files?
Text files contain data that can easily be understood by humans. It includes letters, numbers and other characters. On the other hand, binary files contain 1s and 0s that only computers can interpret.

39) is it possible to create your own header files?
Yes, it is possible to create a customized header file. Just include in it the function prototypes that you want to use in your program, and use the #include directive followed by the name of your header file.

40) What is dynamic data structure?
Dynamic data structure provides a means for storing data more efficiently into memory. Using dynamic memory allocation, your program will access memory spaces as needed. This is in contrast to static data structure, wherein the programmer has to indicate a fix number of memory space to be used in the program.

41) The % symbol has a special use in a printf statement. How would you place this character as part of the output on the screen?
You can do this by using %% in the printf statement. For example, you can write printf(“10%%”) to have the output appear as 10% on the screen.

42) What are the advantages and disadvantages of a heap?
Storing data on the heap is slower than it would take when using the stack. However, the main advantage of using the heap is its flexibility. That’s because memory in this structure can be allocated and remove in any particular order. Slowness in the heap can be compensated if an algorithm was well designed and implemented.
43) What will be the output of printf(“%d”)?
The ideal form of printf is printf(“%d”,x); where x is an integer variable. Executing this statement will print the value of x. But here, there is no variable is provided after %d so compiler will show garbage value. The reason is a bit tricky.
When access specifiers are used in printf function, the compiler internally uses those specifiers to access the arguments in the argument stack. In ideal scenario compiler determines the variable offset based on the format specifiers provided. If we write printf(“%d”, x) then compiler first accesses the first specifier which is %d and depending on the that the offset of variable x in the memory is calculated. But the printf function takes variable arguments.
The first argument which contains strings to be printed or format specifiers is mandatory. Other than that, the arguments are optional.So, in case of only %d used without any variable in printf, the compiler may generate warning but will not cause any error. In this case, the correct offset based on %d is calculated by compiler but as the actual data variable is not present in that calculated location of memory, the printf will fetch integer size value and print whatever is there (which is garbage value to us).
44) What is the return values of printf and scanf?
The printf function upon successful return, returns the number of characters printed in output device. So, printf(“A”) will return 1. The scanf function returns the number of input items successfully matched and assigned, which can be fewer than the format specifiers provided. It can also return zero in case of early matching failure.
45) How to free a block of memory previously allocated without using free?
If the pointer holding that memory address is passed to realloc with size argument as zero (like realloc(ptr, 0)) the the memory will be released.
46) How can you print a string containing ‘%’ in printf?
There are no escape sequence provided for ‘%’ in C. To print ‘%’ one should use ‘%%’, like –
printf(“He got 90%% marks in math”);
47) What is use of %n in printf()?
According to man page “the number of characters written so far is stored into the integer. indicated by the int * (or variant) pointer argument.“. Meaning if we use it in printf, it will get the number of characters already written until %n is encountered and this number will stored in the variable provided. The variable must be an integer pointer.
#include<stdio.h>
main()
{
int c;
printf(“Hello%n world “,&c);
printf(“%d”, c);
}
Above program will print ‘Hello world 5 “ as Hello is 5 letter.
48) Swap two variables without using any control statement ?
We can swap variable using 2 methods. First method is as given below
#include <stdio.h>
main()
{
int a = 6;
int b = 10;
a = a + b;
b = a – b;
a = a – b;
printf(“a: %d, b: %d\n”, a, b);
}
Second method to swap variables is given below
#include <stdio.h>
main()
{
int a = 6;
int b = 10;
a ^= b;
b ^= a;
a ^= b;
printf(“a: %d, b: %d\n”, a, b);
}
49) How to call a function before main()?
To call a function pragma startup directive should be used. Pragma startup can be used like this –
#pragma startup fun
void fun()
{
printf(“In fun\n”);
}
main()
{
printf(“In main\n”);
}
The output of the above program will be –
In fun
In main
But this pragma directive is compiler dependent. Gcc does not support this. So, it will ignore the startup directive and will produce no error. But the output in that case will be –
In main
50) What is rvalue and lvalue?
You can think lvalue as a left side operant in an assignment and rvalue is the right. Also, you can remember lavlue as location. So, the lvalue means a location where you can store any value. Say, for statement i = 20, the value 20 is to be stored in the location or address of the variable i. 20 here is rvalue. Then the 20 = I, statement is not valid. It will result in compilation error “lvalue required” as 20 does not represent any location.
51) Which is better #define or enum?
• Enum values can be automatically generated by compiler if we let it. But all the define values are to be mentioned specifically.
• Macro is preprocessor, so unlike enum which is a compile time entity, source code has no idea about these macros. So, the enum is better if we use a debugger to debug the code.
• If we use enum values in a switch and the default case is missing, some compiler will give a warning.
• Enum always makes identifiers of type int. But the macro let us choose between different integral types.
• Macro does not specifically maintain scope restriction unlike enum. For example –
#include <stdio.h>
main()
{
{
#define A 10
printf(“first A: %d\n”, A);
}
{
printf(“second A: %d\n”, A);
}
}
Above program will print –
first A: 10
second A: 10
52) What is the difference between const char* p and char const* p?
In const char* p, the character pointed by pointer variable p is constant. This value can not be changed but we can initialize p with other memory location. It means the character pointed by p is constant but not p. In char const* p, the pointer p is constant not the character referenced by it. So we can’t assign p with other location but we can change the value of the character pointed by p.
53) What is the point of using malloc(0)?
According to C standard, “ If the size of the space requested is zero, the behavior is implementation defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object”. But there is a benefit of this. The pointer return after malloc(0) will be valid pointer and can be deallocated using free() and it will not crash the program.
54) What is function pointer?
Function pointer, as the name suggests, points to a function. We can declare a function pointer and point to a function. After that using that function pointer we can call that function. Let’s say, we have a function Hello which has definition like this –
void Hello(int)
The pointer to that function will look like –
void (*ptr)(int);
Here, ptr is a function pointer that can point to function with no return type and one integer argument. After declaration, we can point to function Hello like this –
void (*ptr)(int) = NULL;
ptr = Hello;
After that we can call that function like this –
(*ptr)(30);
55) Declare a function pointer that points to a function which returns a function pointer ?
To understand this concept, let us look at the following example.
#include<stdio.h>
void Hello();
typedef void (*FP)();

FP fun(int);

main()
{
FP (*ptr)(int) = NULL;
FP p;
ptr = fun;
p = (*fun)(30);
(*p)();
}

void Hello()
{
printf(“Hello\n”);
}

FP fun(int a)
{
FP p = Hello;
printf(“Number is : %d\n”, a);
return p;
}
In this program, we have a function Hello with no return type and no argument. The function fun takes an integer argument and returns a function pointer that can point to Hello(). First we have typdef the function pointer which can point to a function with no return type and argument with identifier FP. That way it will be easier to define the required function pointer. If we avoid typedef the required function pointer (ptr) will look like this –
void (*(*ptr)())(int)
56) What is indirection?
In C when we use variable name to access the value it is direct access. If we use pointer to get the variable value, it is indirection.
57) Can math operations be performed on a void pointer?
No. Pointer addition and subtraction means advancing the pointer by a number of elements. But in case of a void pointer, we don’t know fpr sure what it’s pointing to, so we don’t know the size of what it’s pointing to. That is why pointer arithmetic can not be used on void pointers.