C Lang Interview Questions
C Lang Interview Questions
Ans: The Datatypes in C Language are broadly classified into 4 categories. They are as
follows:
Basic Datatypes
Derived Datatypes
Enumerated Datatypes
Void Datatypes
3. What do you mean by the Scope of the variable? What is the scope of the variables
in C?
Ans: Scope of the variable can be defined as the part of the code area where the
variables declared in the program can be accessed directly. In C, all identifiers are
lexically (or statically) scoped.
4. What are static variables and functions?
Ans: The variables and functions that are declared using the keyword Static are
considered as Static Variable and Static Functions. The variables declared using Static
keyword will have their scope restricted to the function in which they are declared.
6. What are the valid places where the programmer can apply Break Control
Statement?
Ans: Break Control statement is valid to be used inside a loop
7. How can we store a negative integer?
Ans: To store a negative integer, we need to follow the following steps. Calculate the
two’s complement of the same positive integer.
Eg: 1011 (-5)
Step-1 − One’s complement of 5: 1010
Step-2 − Add 1 to above, giving 1011, which is -5
Ans: printf() is used to print the values on the screen. To print certain values, and on
the other hand, scanf() is used to scan the values. We need an appropriate datatype
format specifier for both printing and scanning purposes. For example,
%d: It is a datatype format specifier used to print and scan an integer value.
%s: It is a datatype format specifier used to print and scan a string.
%c: It is a datatype format specifier used to display and scan
a character value.
%f: It is a datatype format specifier used to display and scan a float value.
Ans. The array is a simple data structure that stores multiple elements of the same
datatype in a reserved and sequential manner. There are three types of arrays,
namely,
18. What is the main difference between the Compiler and the Interpreter?
Ans: Compiler is used in C Language and it translates the complete code into the
Machine Code in one shot. On the other hand, Interpreter is used in python langauge
and other high-end programming languages. It is designed to interpret the code in
line by line fashion.
19. Can I use ‘int’ datatype to store 32768 value?
Ans: No, Integer datatype will support the range between -32768 and 32767. Any
value exceeding that will not be stored. We can either use float or long int.
20. How is a Function declared in C Language?
Ans: A function in C language is declared as follows,
Syntax:
Syntax:
Syntax:
Syntax:
1 free(ptr);
1 struct employee
2 {
3 char name[10];
4 int age;
5 }e1;
6 int main()
7 {
8 printf("Enter the name");
9 scanf("%s",e1.name);
10 printf("n");
11 printf("Enter the age");
12 scanf("%d",&e1.age);
13 printf("n");
14 printf("Name and age of the employee: %s,%d",e1.name,e1.age);
15 return 0;
16 }
1 #include<stdio.h>
2 void change(int*,int*);
3 int main()
4 {
5 int a=25,b=50;
6 change(&a,&b);
7 printf("The value assigned to a is: %d",a);
8 printf("n");
9 printf("The value assigned to b is: %d",b);
10 return 0;
11 }
12 void change(int *x,int *y)
13 {
14 *x=100;
15 *y=200;
16 }
//Output
1 #include<stdio.h>
2 #include<conio.h>
3 int main()
4 {
5 char ch;
6 printf("Please enter a character ");
7 ch=getch();
8 printf("nYour entered character is %c",ch);
9 printf("nPlease enter another character ");
10 ch=getche();
11 printf("nYour new character is %c",ch);
12 return 0;
13 }
//Output
Please enter a character
Your entered character is x
Please enter another character z
Your new character is z
1 #include<stdio.h>
2 #include<ctype.h>
3 int main()
4 {
5 char c;
6 c=a;
7 printf("%c after conversions %c", c, toupper(c));
8 c=B;
9 printf("%c after conversions %c", c, toupper(c));
//Output:
a after conversions A
B after conversions B
1 #include<stdio.h>
2 #include<stdlib.h>
3 int main()
4 {
5 int a,b;
6 for(a=1;a<=10;a++)
7 {
8 b=rand();
9 printf("%dn",b);
10 }
11 return 0;
12 }
//Output
1987384758
2057844389
3475398489
2247357398
1435983905
1 #include<stdio.h>
2 #include<stdlib.h>
3 int main()
4 {
5 int* ptr;
6 int n, i, sum = 0;
7 n = 5;
8 printf("Enter the number of elements: %dn", n);
9 ptr = (int*)malloc(n * sizeof(int));
10 if (ptr == NULL)
11 {
12 printf("Memory not allocated.n");
13 exit(0);
14 }
15 else
16 {
17 printf("Memory successfully allocated using malloc.n");
18 for (i = 0; i<= n; ++i)
19 {
20 ptr[i] = i + 1;
21 }
22 printf("The elements of the array are: ");
23 for (i = 0; i<=n; ++i)
24 {
25 printf("%d, ", ptr[i]);
26 }
27 }
28 return 0;
29 }
//Output
Enter the number of elements: 5
Memory successfully allocated using malloc.
The elements of the array are: 1, 2, 3, 4, 5
1 #include<stdio.h>
2 void fun()
3 {
4 static int x;
5 printf("%d ", x);
6 x = x + 1;
7 }
8 int main()
9 {
10 fun();
11 fun();
12 return 0;
13 }
//Output
01
32. What is the difference between declaring a header file with < > and ” “?
Ans: If the Header File is declared using < > then the compiler searches for the
header file within the Built-in Path. If the Header File is declared using ” ” then the
compiler will search for the Header File in the current working directory and if not
found then it searches for the file in other locations.
35. Can I declare the same variable name to the variables which have different scopes?
Ans: Yes, Same variable name can be declared to the variables with different
variable scopes as the following example.
1 int var;
2 void function()
3 {
4 int variable;
5 }
6 int main()
7 {
8 int variable;
9 }
36. Which variable can be used to access Union data members if the Union variable is
declared as a pointer variable?
Ans: Arrow Operator( -> ) can be used to access the data members of a Union if the
Union Variable is declared as a pointer variable.
37. Mention File operations in C Language.
Ans: Basic File Handling Techniques in C, provide the basic functionalities that user
can perform against files in the system.
Function Operation
fopen() To Open a File
fclose() To Close a File
fgets() To Read a File
fprint() To Write into a File
auto
register
static
extern
1 (type_name) expression;
42. How can you print a string with the symbol % in it?
Ans: There is no escape sequence provided for the symbol % in C. So, to print % we
should use ‘%%’ as shown below.
44. Which structure is used to link the program and the operating system?
45. What are the limitations of scanf() and how can it be avoided?
Ans: The differences between macros and functions can be explained as follows:
Macro call replaces the templates with the expansion in a literal way.
The Macro call makes the program run faster but also increases the program size.
Macro is simple and avoids errors related to the function calls.
In a function, call control is transferred to the function along with arguments.
It makes the functions small and compact.
Passing arguments and getting back the returned value takes time and makes the
program run at a slower rate.
47. Suppose a global variable and local variable have the same name. Is it is possible to
access a global variable from a block where local variables are defined?
Ans: No. It is not possible in C. It is always the most local variable that gets preference.
Ans: In Entry Controlled Loop, loop body is checked after checking the test condition
i.e. condition is checked first after that loop body will execute while in Exit
Controlled Loop, loop body will be executed first after that loop’s test condition is
checked.
49. Difference b/w sentinel control loop & counter control Loop?
Ans: In Counter Controlled (fixed control) loop, we know that exactly how many
times loop body will be executed while in Sentinel Controlled loop we don’t know
about the loop recurrence, Execution of loop is based on condition not counter.
Ans: In C Programming Language, switch case statement follows the fall down
property. It means when case block is executed and break statement is not used after
the block statements, then it will execute next case or default statements until break not
reached or switch not finished.
1 int i=2;
2 switch(i)
3 {
4 case 1:
5 printf("\none");
6 break;
7 case 2:
8 printf("\ntwo");
9 case 3:
10 printf("\nthree");
11 case 4:
12 printf("\nfour");
13 case 5:
14 printf("\nFive");
15 break;
16 default:
17 printf("\nWrong Choice");
18 }
51. Which loop statement is executed at least once even loop test condition if false?
Ans: “do while loop” executes once even loop test condition is false. Since do while is
an exit controlled loop and in this type of loop, loop body execute once than loop test
condition checks.
52. Can we use single statement in loop body without using curly braces "{...}"?
Ans: Yes, when loop body has single statement, than we can escape curly braces "{...}".
1 for(i=1;i<=10;i++)
printf("\n%d ",i);
Ans: A loop which is never finished is known as infinite loop, it means the looping
condition is always true, so that loop never terminate. Infinite loop is very useful in
embedded systems.
Ans: It depends on the programming need, we cannot say which is best to use. Where
condition checking is required before body execution, just use for or while. And when
condition is not required to check before body execution, just use do while.
Ans: No, continue statement can only be used within the loops only, it can be any
loop while, do while or for. If we use continue statement without using loop, there will
be a compiler error "misplaced continue".
56. Can goto statement transfer program control from one function to another
function?
Ans: No, As we know that goto statement can transfer the program's control to
specified label, But it has a limitation, as it can transfer program's control within a
function only. Outside the function, control can not be transferred by goto statement.
61. What is the return type of printf() function, and what it returns ?
62. In the case of character array what is the difference b/w strlen() and sizeof() ?
Ans: In character array strlen() returns the number of characters, while sizeof()
returns the number of occupied bytes by character array.
Example
#include <stdio.h>
#include <string.h>
int main()
{
char text[100]= "Sample test";
printf("%d,%d", strlen(text), sizeof(text));
return 0;
}
In this example strlen(text) will return 11 because total number of characters are 11
and sizeof(text) will return 100, because text variable will take 100 bytes in memory.
Ans: If you have more than one condition to check on single variable or a single
expression, then switch is better than if. In switch statement, program’s execution
jumps to the matching value if found. If you use if condition, it checks one by one
condition. So it is highly recommended to use switch, if you have to check a
variable/condition/expression with multiple values.
Example
if( errorCode == 0)
printf("Error code is 0: reading failed error.\n");
if( errorCode == 1)
printf ("Error code is 1: writing failed error.\n");
if( errorCode == 2)
printf("Error code is 2: opening failed error.\n");
if( errorCode == 3)
printf("Error code is 3: write protected.\n");
Here, errorCode variable is checking with values 0,1,2 and 3. In such case you can
use switch.
switch(errorCode)
{
case 0:
printf("Error code is 0: reading failed error.\n");
break;
case 1:
printf ("Error code is 1: writing failed error.\n");
break;
case 2:
printf("Error code is 2: opening failed error.\n");
break;
case 3:
printf("Error code is 3: write protected.\n");
break;
default:
printf("Undefined error.\n");
}
Ans: No, it’s not necessary to use the default. But using default is a good programming
technique, which must be used. If variable or expression does not match with any given
case values, default case executed.
65. Can we use default case anywhere in the switch statement block?
Ans: Yes, of course. You can use default case anywhere within the switch statement. It
is not necessary to write default case at the end of the cases.
a=10
b=20
b=++a(increment than assign)
a->11
b->11
++ is an increment operator, which increments the value of a variable. ++a is pre
increment, where value will be incremented first and then expression evaluate. a++ is
a post increment, where expression evaluates first and then value increments.
67. Can printf() function be used in if condition?
Ans: Yes, because printf() returns integer value (that is total number of printed
characters). And In if condition, a non-zero value evaluated as TRUE, and zero
evaluated as FALSE.
int num=113;
(num%2==0)?printf("EVEN"):printf("ODD");
Ans: In ternary operator, ? : (In case of TRUE or FALSE) only one C statement can
use, but in if statement, we can use more than one statement.
printf("%d,%d",sizeof(int),sizeof(1.23));
Ans: 10.05 is a double constant while 10.05f is a float constant, in c language 10.05
(any floating value without followed by ‘f’) is a double constant value.
Ans: These all functions are used to get a single character from keyboard, but there
are following differences.
getch() -It takes input, but not echo the character on console, but does not requires
enter key.
getche() -It takes input, but not echo character and does not requires enter key.
getchar() -It takes input, echo character and requires enter key.
72. Can a C variable/identifier name starts with a digits and what are the maximum
length of a variable/identifier name?
Ans: No, It will be an error, if you want to find remainder with floating point
number, you should use fmod()function.
int main()
{
int i = 97, *p = &i;
func(&i);
printf("%d ", *p);
}
void func(int *p)
{
int j = 2;
p = &j;
printf("%d ", *p);
}
A. 2 97
B. 2 2
C. Compile time error
D. Segmentation fault/code crash
75. What is the output of this C code?
void func(int*);
int main()
{
int i = 10, *p = &i;
func(p++);
}
void func(int *p)
{
printf("%d\n", *p);
}
A. 10
B. Some garbage value
C. Compile time error
D. Segmentation fault
void func(int*);
int main()
{
int i = 10;
func((&i)++);
}
void func(int *p)
{
printf("%d\n", *p);
}
A. 10
B. Some garbage value
C. Compile time error
D. Segmentation fault/code crash
77. What is the output of this C code?
void main()
{
int x = 0;
int *ptr = &x;
printf("%d\n", *ptr);
}
A. Address of x
B. Junk value
C. 0
D. Run time error
78. What is the output of this C code?
void main()
{
int x = 0;
int *ptr = &5;
printf("%p\n", ptr);
}
A. 5
B. Address of 5
C. Nothing
D. Compile time error
int x = 0;
void main()
{
int *const ptr = &x;
printf("%p\n", ptr);
ptr++;
printf("%p\n ", ptr);
}
A. 0 1
B. Compile time error
C. 0xbfd605e8 0xbfd605ec
D. 0xbfd605e8 0xbfd605e8
int x = 0;
void main()
{
Int x=10;
int *ptr = &x;
printf("%p\n", ptr);
x++;
printf("%p\n ", ptr);
}
A. Same address
B. Different address
C. Compile time error
D. Varies
int main()
{
char *p = NULL;
char *q = 0;
if (p)
printf(" p ");
else
printf("nullp");
if (q)
printf("q\n");
else
printf(" nullq\n");
}
A. nullp nullq
B. Depends on the compiler
C. x nullq where x can be p or nullp depending on the value of NULL
D. p q
84. What is the output of this C code?
int main()
{
int i = 10;
void *p = &i;
printf("%d\n", (int)*p);
return 0;
}
A. Compile time error
B. Segmentation fault/runtime crash
C. 10
D. Undefined behavior