Although C is 5 decades old, it is still among the most in-demand skills. Even though modern languages like Python, Golang, Go and RustAMP are popular, they are still way behind in building a legacy like that of the C programming language.
Learning C is, although, not necessary to build a career in software development these days, it is an important skill to have to build a solid foundation in programming. So, here, we will be discussing some of the most-asked C interview questions.
Table of Contents
ToggleAMPThe following are the important features of the C language:
These two functions facilitate dynamic memory allocation in C. Following are the points that differentiate between these two functions:
calloc | malloc |
| It will load all the memory locations assigned with 0 value. | It will not load all the memory locations assigned with a 0 value. It contains garbage value. |
| You can assign multiple blocks of memory using calloc. | You can only create a single block of memory of the desired size. |
| It is more secure. | It is less secure. |
| The calloc() function returns the beginning address and sets it to zero before allocating the memory. | The malloc() function does not set the address to 0 but just returns the starting address. |
| It can do memory initialization. | It cannot do memory initialization. |
Making a new header file is both possible and simple in C. You can make a file containing function prototypes a header file and use it throughout C code. Simply, put the file’s name in the “#include” section.
Actual Parameters: These parameters are the arguments passed to a function call. The calling function determines these parameters. They are listed in the parameter list of a subprogram call. It is not required to specify the data type in the argument itself.
Example: add(x,y);
x and y are actual parameters here.
Formal Parameters: These are the variables or expressions that are listed in the parameter list of a subprogram definition. It is compulsory to mention the data type of the received value. The scope of formal arguments is constrained to the function declaration in which they are used.
Example: int diff(int x, int y)
{
//Code inside function
}
x and y are Formal Parameters here
A scope is a region within which a variable can have its usage and existence. A variable is not accessible if it’s accessed beyond its scope. The scope of a variable can be of two types:
1. Local Variables: These variables are declared within a function or block. Only statements included within that function or code block are permitted to use them.
Example:
#include <stdio.h>
int main () {
// local variable declaration
int x, y;
int sum = 0;
// actual initialization
x = 45;
y = 30;
sum = x + y;
printf (“x = %d, y = %d and sum is = %d\n”, x, y, sum);
return ;
}
2. Global Variables: These variables are declared outside of the function or block. The global variable can have its value altered by any function. It can be used for all functions but it has to be declared at the beginning of the block.
Example:
#include <stdio.h>
// global variable declaration
int globSum;
int main () {
// local variable declaration
int x, y;
// actual initialization
x = 18;
b = 67;
globSum = x + y;
printf (“x= %d, y = %d and Sum = %d\n”, x, y, globSum);
return ;
}
In the C programming language, everything is in the form of a function. Hence, C is a procedural programming language.
Most object-oriented languages, including Java, Python, and C++ support function overloading, however, C does not. Nonetheless, one may indirectly achieve function overloading in the C programming language. The first strategy goes like this:
You can pass a void * pointer as an argument to a function. Pass another argument in the same function that would tell the actual data type of args1 passed.
int foo(void * arg1, int arg2);
The process of allocating memory to a program and its variables during execution is known as dynamic memory allocation. There are three functions in the dynamic memory allocation process to allocate memory, and one function to release memory that has been used. These functions are present under the <stdlib.h> header.
You can think of dynamic allocation as a method of utilizing heap memory where we can change the size of a variable at any time while a program is running using library functions.
1. malloc() – Used to allocate memory
Syntax:
ptr = (cast-type*) malloc(byte-size);
2. calloc() – Used to allocate memory
Syntax:
ptr = (cast-type*)calloc(n, sizeOfElement);
3. realloc() – Used to allocate memory
Syntax:
ptr = realloc(ptr, eleNewSize);
4. free() – Used to deallocate the used memory
Syntax:
free(ptr);
Pointers are used to keep the address of a variable or memory location. The major goals of the pointer are to decrease execution time and boost memory efficiency. Following are some of the uses of pointers:
There are three separate memory pools in C:
Call by value:
Example:
#include<stdio.h>
void main() {
#include<stdio.h>
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
int main() {
int a = 70;
int b = 30;
printf(“The numbers before swapping a and b %d %d \n”,a, b);
swap(a, b);
printf(“The numbers after swapping a and b %d %d \n”,a, b);
return ;
}
Call by reference:
Example:
#include<stdio.h>
void swap(int *a ,int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int a = 80;
int b = 50;
printf(“The numbers before swapping a and b %d %d \n”,a, b);
swap(&a, &b);
printf(“The numbers after swapping a and b %d %d”,a, b);
return ;
}
The pointer const char* p points to a const char.
The pointer char const* p points to a char const.
Const char and char const are the same in C language.
When a header file is enclosed in double quotations (“), the compiler first looks for that specific header file in the working directory. If not, it looks like the inclusion path for the file. However, the compiler only looks in the working directory for the specific header file when the header file is enclosed in angular brackets (>).
You can easily generate random numbers using the rand function. Following is the code to generate a random number:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x,y;
for(x=1;x<=10;x++)
{
y = rand();
printf(“%dn”,y);
}
return ;
}
#include<stdio.h>
void main()
{
if(printf(“hello world”)){}
}
#include<stdio.h>
int main()
{
int n;
for(i = 5; i!=; i–)
printf(“i = %d”, i–);
getchar();
return ;
}
Output: Infinite loop as the value of ‘i’ is never 0 when the i!=0 condition is checked.
#include <stdio.h>
int main()
{
int num, rev = , remainder, number;
printf(“Enter an integer: “);
scanf(“%d”, &num);
number = num;
while( num!= )
{
remainder = num%10;
rev = rev*10 + remainder;
num /= 10;
}
if (number == rev)
printf(“\n%d is a palindrome\n”, number);
else
printf(“\n%d is not a palindrome\n”, number);
return ;
}
#include <stdio.h>
main() {
int num, i, count = ;
printf(“Enter any number:”);
scanf(“%d”, &num);
//prime numbers have only factors (1 and itself).
//count the factors of number from 1 to num.
//iterate from 1 to num
for (i = 1; i <= num; i++) {
if (num % i == ) {
count++;
}
}
//if count is 2 then its prime number
if (count == 2) {
printf(“It is a Prime number”);
}
else {
printf(“It is not a Prime number”);
}
return ;
}
The keyword typedef defines an alias for an existing type in C programming. No matter if the name is for a structure declaration, function parameter, or integer variable, typedef will abbreviate it.
Syntax:
typedef <existing-type> <alias-name>
Example:
typedef long long ll
A data value that is kept in memory at a specific location is referred to as an “r-value.” A value cannot be assigned to an expression that has an r-value, hence this expression can only occur on the right side of the assignment operator (=).
A memory location that is used to identify an object is referred to as an “l-value.” Either the left or right side of the assignment operator (=) contains the l-value. In many cases, the l-value is utilized as an identification.
Structure: A structure is a special form of data. Multiple members of various data kinds can be contained within a single structure. The components of a structure are kept in consecutive memory regions and are always accessible. In a structure, each data object is a member or field.
Syntax:
struct [name_of_structure]
{
type member_1;
type member_2;
. . .
type member_n;
};
Example:
struct employee
{
int empno;
char empname[50];
string empphone;
};
Union: It is a user-defined data type. It is similar to the structure but each component starts at the exact same place in memory. In the precise region of memory, the union brings together items of various data types. Although a union can have numerous members, only one of them can contain a value at once. The amount of storage space allotted for the union variable is equal to the total amount of space needed for the union’s most salient data member.
Syntax:
union [name_of_union]
{
type member_1;
type member_2;
. . .
type member_n;
};
Example:
union Employee {
char empname[32];
int empage;
string empdept;
};
Practically all other languages are based on the fundamental language C. All other programming languages are built upon it. It is a widely used and well-liked language for creating system applications. Despite the fact that other languages now outnumber it in popularity, it is still one of the most widely used programming languages.
Because C is a fundamental language, it is crucial to have at least a basic understanding of it when applying for a job. With this blog, we aimed to provide a series of frequently asked C questions that would give you confidence in the language’s fundamentals.
Aditya is a seasoned JavaScript developer with extensive experience in MEAN stack development. He also has solid knowledge of various programming tools and technologies, including .NET and Java. His hobbies include reading comics, playing games, and camping.