Below are some of the top C interview questions for freshers (with proper answers).
A computer scientist named Dennis Ritchie from the USA developed the C programming language. He developed this with the help of his colleague Ken Thompson.
It was invented in 1972.
It is the mother of most of the JVMs and compilers. Most of the languages use functions from this language. It has concepts like arrays, functions, file handling, etc.
Global variables are declared outside the function, and the scope of a variable is available till the end of the program. Local variables are declared inside the function, and the scope is available in these variables.
The life of the local variable is created when the block is entered and ends on exit, but the life of a global variable exists until the execution of the program.
Local variables are stored in a stack unless specified, and the compiler decides the storage location of a variable in global ones. So, these are the differences between a local and global variable in C.
Functions in C language are used to avoid rewriting the code. They can be called multiple times from a program.
With the help of functions, any program can be tracked easily. It also provides the concept of reusability. Furthermore, a user can also break the big task into smaller tasks with the assistance of C functions.
It is a group of elements of similar sizes. The size of the array can also be changed after declaration. The array has a contiguous memory location, and it makes code optimized and easier.
Arrays in C are of two types:
When memory is allocated at the run time in any program and can be increased at the time of execution, it is called dynamic memory allocation in C programming.
It is used in the linked list. At runtime, malloc() and calloc() are used. It is implemented with the help of data segments, and less memory is required for storage.
ANSI, or American National Standard Institute, is an organization that maintains computer languages, data safety, data encoding, etc.
A token is an identifier that can be a keyword, constant, operator, a string literal, etc. It is also the smallest unit in a program in C.
printf() function in C is used to print the values of character, float, integers, and string values. Some specifiers are %d, %s, %c, %f used for printing the values.
scanf() function in C is used for getting input from the user for a program.
When a variable is declared as static, it is known as a static variable. It keeps the value between various function calls. These are used because the static variable in C is available throughout the program.
It initialized to zero and later updated to the needed value. It is used only one time so that the memory space can be utilized.
When a function calls itself, the process is known as recursion, and the function is known as a recursive function. It has two functions, the winding phase, and the unwinding phase.
A variable that refers to the memory address of any value is called a pointer. It is used to optimize the program to run faster. Some memory is allocated to a variable and it holds the address number that is called the pointer variable.
When a pointer is not used to refer to any address, it is called a NULL pointer. When we assign 0 to any pointer, it is called a NULL pointer.
The parameters sent from the main function to subdivided functions are called actual parameters. The parameters declared at subdivided functions are called formal parameters.
It is a structure that contains an element of another structure as its member. Basically, A nested structure is a structure within a structure. It is done the same way structure members are declared in a function.
A function in C can be declared as
return_type function_name(formal parameter list)
{
function_body;
}
Actual arguments remain safe and can not be changed in the call by value in C. The operators are not safe as they are performed on actual parameters.
In the call by value, actual segments are not passed, and a copy of the segment is sent, but in the call by reference, actual arguments are passed.
For call by value, separate memory locations are created, and for the call by reference, they share the same memory space.
Yes, I can generate random numbers in C with the help of the rand() function. Here is how:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
printf(“The number is: %d”, rand());
printf(“\n The number is: %d”, rand());
printf(“\n The number is: %d”, rand());
getch();
}
The output will be:
The number is: 455
The number is: 15648
The number is: 23212
The arrow operator in C is used to access the data member of a union and declared as a pointer variable. It is expressed as -> in C language.
Storage class specifiers in C are extern, static, register, and auto.
When one data type is converted into another data type, the process is known as typecasting.
Local variables are preferred, and it is not possible to access them in global variables.
An enumeration in C is a user-defined data type widely known as an enum. It has constant integers and integrals, which have names assigned by the user. It is used when a user needs a variable with a set of values.
Union in C programming is a data type that is user-defined that allows many types of data in a single unit. This data type holds the memory of the largest member, and it does not use the sum of the memory of all functions.
In this, we can access one variable at a time as it allocates one common space for all members of a union.
It is a relational operator that is used to compare two variables or values in a program. It is called equal to or equivalent to.
The conditional operator is known as the conditional operator in the C language. It is denoted as (?:).
The C preprocessor is a software program that is used to process a source file before compilation. Header files, conditional compilation, line control, macro expansion, etc. can be included in a preprocessor.
The backward slash(/) is used for this.
The expression that is present on the right side of the assignment operator is known as the rvalue. It is assigned to lvalue which is on the left side of the assignment operator.
The lvalue represents a variable and not a constant.
The data is stored in FIFO format in C language. The queue works on First-In-First-Out. The first data stored will be the first one to be accessed in a program.
A tab in C programming has eight spaces.
The if-else statement in C programming works as a control flow statement. it is followed by an optional else statement in C language.
The memory that is allocated during the time of compilation is called static memory allocation. It is implemented using heap or stacks.
This type of memory can not be executed with the program. The lifespan of a static memory is the lifetime of a program. It is used by the array in C.
Variables are identifiers that are made of one or more characters and hold a particular value. The values that are held by variables can be changed in a program.
The values given to constants are not changeable and can only be assigned once, mostly at the start of the program.
This is the AND operator in the C program. There is a situation for using this operator where all the conditions are true. When only one statement is false, it will return false as a result.
When there is a run-time error in a program, the execution of a program is paused, and it is called the run-time error. It will show which part of the program caused the error.
The C compiler has a function called getenv() that is used to retrieve the environment variable. It returns the pointer to an environment string on success. When the function variable can not find environment variables, it returns NULL.
The variable will contain the address of another value or memory.
Embedded systems don’t have any sort of file system or operating system and the ANSI standards call these environments a free-standing environment. When a program is running on a system, it is called a hosted environment.
Memory allocated in a program can be released by the free() function. The alternate method is when the pointer is holding a memory address which is realloc(ptr,0).
C programs can not declare a static variable without defining it. Static variables in a program can be defined in a header file, but it will result in causing every source file to have a different copy of a variable.
Yes, this can be done. Here is the program:
#include<stdio.h>
int main()
{
int i;
printf(“Enter a number: ”);
scanf(“%d”, &i);
if((number &1)==0)
printf(“%d is even”, i);
else
printf(“%d is odd”, i)
getch();
}
A bitwise AND operator can also be used to check if the number is even or odd.
Both calloc() and malloc() are used for allocating memory functions. The only difference is that calloc() loads the memory location, but malloc() will not.
For the execution of a C program, the main() function is needed. Without this function, the program will be compiled but can not be executed.
It is considered a predefined function in a C program. It is used as a directive to the compiler and executed before the execution of a program.
A compiler translates the code into the machine code and it is used in C language. The interpreter is used in other high-level languages to compile code in line by line method.
The local variable of a function is known as the auto variable or automatic variable. These are declared inside a block. It allocates memory with the entry and releases the used memory when the program is ended. When there is no value in the local variable, then it has a garbage value.
When a pointer is assigned to a memory location, but another program erases the memory used by the first pointer, and the first pointer is still using the same location, it is called a dangling pointer.
It is a pointer that stores the memory address of variables. It is used to point to the variable memory location that is released in a program.
The concatenation operator is used to combine or concatenate two strings into one string. It is used in C macros.
No, the working of the exit() function and return is not the same. The exit() is used to exit the program and return control to the OS. The return statements return from a function and return control to the calling function.
No, we can not use a continue statement without loops in C. it can only be used when in a loop. Otherwise, it will show a compile-time error.
The #ifdef and #ifndef preprocessor directives are used to check if the symbol is defined or not.
No, you can not store this value. The integer data type can only support values from -32768 to 32767. You can use float or long int for that.
It refers to the codes that are overlapped as the program progresses. This is an unstructured approach that needs to be avoided as it makes the program more complex.
It is a 32-bit pointer accessed outside of a segment. Huge pointers can be modified, which is not possible in far pointers.
These functions are used to read characters from the input.
It is a function used to convert lowercase characters into upper case.
A double-quoted variable is called a string in C, but a single-quoted variable is known as an identifier in C. A string variable ends with a null terminator, which makes a two-character array.
It is a preprocessor directive used to turn on or off features in a C program. It has two types #pragma startup, which is used to specify functions at the start of a program, and #pragma exit, which is used to specify functions called on the exit of the program.
It is an algorithm that steps through a list and compares elements, and swaps them to make them in order. This process is repeated frequently until the list is sorted.
I will use the goto keyword to perform unconditional branching in a C program.
Yes, I can create a header file with function prototypes that are used in a program. These files are used after the #include.
The type of pointer that can access all 16 segments of RAM is called a far pointer. It is a 32-bit pointer that uses information outside of memory.
Yes, it is possible to create header files as per the requirement. It is done by including the function prototypes in the C program. #include directive is used by the name of a header file in C.
It can be basically done with two methods.
Here is how it is done with arithmetic operators:
#include<stdio.h>
int main()
{
int a=5, b=15;
print(“values before swapping: a=%d b=%d”, a,b);
a=a+b;
b=a-b;
a=a-b;
printf(\n Values after swapping a=%d b=%d, a,b);
return0;
}
Output:
Values before swapping: a=5 b=15
Values before swapping: a=15 b=5
%% is used to print a string with a % symbol.
printf(“The total marks obtained %%d”);
It is a condition where dynamic memory is allocated to the program but the used memory is not deleted or released. It is harmful to a program.
#include<stdio.h>
main()
{
char*x=”xyz”;
x[2]=’a’;
printf(“%z”, *x);
}
This program will not run and crash because the program is trying to change the values throughout the program. The pointer indicates a constant string which is the reason for the crash.
x++ is more efficient in a program as it gives just a single instruction to the compiler.
Yes, I can print Hello World without a semicolon. It can be done with three methods.
Here is how I will do it:
#include<stdio.h>
int main()
{
if(printf(“Hello World”))
{}
return0;
}
Identifier names with two underscores are used by the compiler or standard library functions when they appear in a program. All identifier names started with an underscore are reserved in the file scope.
When a reserved identifier is used, the program will not compile. It is best to avoid using variable names with underscores at the start.
#include<stdio.h>
int main()
{
printf(“Hello World!”)
}
Output:
Hello World
#include<stdio.h>
int main()
{
int a, b, sum;
print(“Enter two integers: ”);
scanf(“%d %d”, &a, &b”);
sum=a+b;
printf(“%d+%d=%d”, a,b,sum);
return0;
}
Output:
Enter two integers: 5
12
5+12=17
#include<stdio.h>
int main()
{
char c;
printf(“Enter a character: ”);;
scanf(“%c”, &c);
printf(“ASCII value of %c=%d”, c,c);
return0;
}
Output:
Enter a character: G
ASCII value of G: 71
#include<stdio.h>
int main()
{
int num;
printf(“Enter a number: ”);
scanf(“%d”, &num);
if(num%2==0)
printf(“%d is even”, num);
else
printf(“%d is odd”, num);
return0;
}
Output:
Enter a number: 5
5 is odd.
#include<stdio.h>
int main()
char s[] = “WsCube Tech”;
int a;
for(a=0; s[a]!= ‘\0’; ++a);
printf(“Length of the string: %d”, a);
return0;
Output:
Length of the string: 11
#include<stdio.h>
int main()
{
int a, reverse=0, remainder;
printf(“Enter an integer: ”);
scanf(“%d”, &n);
while(n!=0)
{
remainder=n%10;
reverse=reverse*10+remainder;
n/=10;
}
printf(Reversed value: %d, reverse);
return0;
}
Output:
Enter an integer: 2598
Reversed value: 8952
Here are some of the most asked questions that are generally asked regarding C. It will clear your queries related to the C programming career.
Yes, you can. There are many companies that hire C programmers. You can also find a job in the government sector or teaching sector with skills in C.
The salary of a C programmer ranges from INR 2 LPA to INR 15 LPA in India.
Yes. C is the mother of all the programming languages and it is still very much in demand. There are certain operations that can only be processed with C programming and that is what makes it relevant.
There are plenty of jobs in C programming. C programmers can get a job as a developer, an analyst, software developers, a government sector employee, a trainer, or a teacher.
There are plenty of companies that are hiring C programming experts.
Here are the top companies hiring C developers: