C Programming Interview Questions
This page consists of C Programming interview questions and answers.
We can pass value to a function in two different ways.
In call by value a copy of the original value is passed to the function. Any change made to the copy is not reflected back to the original value.
In call by reference a reference of the original value is passed to the function. And any change made to the reference inside the function is reflected back to the original value.
We can write the following C code to showcase Call by Value.
#include <stdio.h>
void foo(int);
int main() {
int x = 10;
printf("Inside main function.\n");
printf("Value of x %d\n", x);
// calling foo
foo(x);
printf("Value of x after function call %d\n", x);
return 0;
}
void foo(int x) {
printf("Inside foo function.\n");
x = x + 10;
printf("Value of x updated to %d\n", x);
}
The above code will give us the following output.
Inside main function.
Value of x 10
Inside foo function.
Value of x updated to 20
Value of x after function call 10
Click here to learn more about Call by Value.
We can write the following code to implement call by reference in C programming language.
#include <stdio.h>
void foo(int *);
int main(void) {
int x = 10;
printf("Value of x before function call: %d\n", x);
foo(&x);
printf("Value of x after function call: %d\n", x);
return 0;
}
void foo(int *n) {
*n = *n + 10;
printf("Inside foo function: Value updated to %d\n", *n);
}
The output of the code is given below.
Value of x before function call: 10
Inside foo function: Value updated to 20
Value of x after function call: 20
Click here to learn more about Call by Reference.
C program:
#include <stdio.h>
int main(void) {
int arr[][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
int r = 3, c = 3;
int i, j;
int sum = 0;
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
if (i == j && i % 2) {
sum += arr[i][j];
}
}
}
printf("Sum: %d\n", sum);
return 0;
}
The answer of the above C program is 5.
The code is only adding odd row-column value to variable sum.
A loop without any exit condition runs indefinitely and is called an infinite loop.
Following are some of the examples of infinite loop.
Infinite for loop.
for(i = 1; i > 0; i++) {
// some code goes here...
}
Infinite while loop.
i = 1;
while (i > 0) {
// some code goes here...
i++;
}
Code #1:
for (;;) {}
Code #2:
while (1) {}
Code #3:
do {} while (1);
All of them are infinite loop and there is no exit condition to jump out of the loop body.
C code snippet:
int x = 5;
int y = 2;
float z = x/y;
printf("z = %f", z);
The above C code snippet will print 2.000000
.
Even though we know that 5/2
equals 2.5
but since they are stored in integer variable so the final return is also in integer without the decimal part.
C code snippet:
int x = 5;
int y = 2;
float z = x/y;
printf("z = %f", z);
We have to use type casting in order to get the correct result. We can type cast x to float data type and then it will work.
Modified C code snippet:
int x = 5;
int y = 2;
float z = (float) x/y;
printf("z = %f", z);
getch
and getche
functions different for each other?Both the functions are used to read a single character input from the keyboard.
But getch()
function does not displays the input character whereas, getche()
function displays the data on the output screen.
We can write the following C code snippet to allocate 10 blocks of memory of type int
to store integer values.
// int pointer
int *iptr;
// allocate memory
iptr = (int *) malloc (10 * sizeof(int));
Click here to read more about malloc
function.
ADVERTISEMENT