C Interview Questions with Answers
1. What is C?
C is a simple, procedure or structure oriented and middle level programming language. It was developed at Bell Laboratories to develop the UNIX Operating System by Dennis Ritchie in 1972.
2. Where are C language used?
C language is specially used to design system software. System software includes software categories such as operating systems, utility, software device, drivers, compilers and linkers, etc.
3. Explain low level, middle level and high level languages with example?
Low level languages
Low-level language is a programming language that deals with a computer's hardware components and constraints. Low-level languages are closer to the hardware. Ex- Assembly Language, Machine Language.
High level languages
High-level language is a programming language that enables development of a program in much simpler programming context and is generally independent of the computer's hardware architecture. High-level languages are closer to human languages and further from machine languages. Ex- Java, Python.
Middle level languages
Mid-level languages have much of the syntax and facilities of a higher level language, but also provide direct access in the language (as well as providing assembly language) to machine features. Medium-level language serves as the bridge between the raw hardware and programming layer of a computer system. EX- C, C++.
4. Differentiate between structured oriented and object oriented programming language?
In Structured Programming, Programs are divided into small self contained functions. Prime focus is on functions and procedures that operate on data. It follows top-down approach. It is also known as Modular Programming and a subset of procedural programming language. It is less secure as there is no way of data hiding. EX- C, Pascal.
In Object Oriented Programming, Programs are divided into small entities called objects. Prime focus is on the data that is being operated and not on the functions or procedures. It supports inheritance, encapsulation, abstraction, polymorphism, etc. It follows bottom-up approach. It is more secure as having data hiding feature. Ex- C++, Java, C#.
5. What is the difference between assembler, compiler and interpreter?
Assembler: It is system software which converts assembly level language code into machine level code (binary format).
Compiler: It is system software which converts high level language code into machine level code (binary format) in a single step.
Interpreter: It is system software which converts high level language code into machine level code (binary format) line by line.
6. Differentiate between “=” and “==” operators in C?
“=” is an assignment operator used to assign the values to the variables.
“==” is a relational operator used to compare two variable values whether they are equal are not.
7. What are an lvalue and rvalue?
The lvalue expression is located on the left side of an assignment (“=”) operator. The lvalue expression must reference a storable variable in memory. It cannot be a constant.
The rvalue expression is located on the right side of an assignment (“=”) operator. Unlike an lvalue, an rvalue can be a constant or an expression.
An assignment (“=”) operator must have both an lvalue and an rvalue.
“lvalue”=”rvalue”
8. What is the difference between pre increment operator (++var) and post increment operator (++var) in C?
The ++ operator is called the increment operator which increments the current value of the variable by 1. It can be applied to only variables.
Two types of increment operator
Pre increment operator is used to increment variable value by 1 before the variable value is used in the expression.
EX-
main(){
int a=10,b;
b=++a;
printf(“Value of a=%d and b=%d”,a,b);
}
Output: Value of a=11 and b=11
Post increment operator is used to increment variable value by 1 after the variable value is used in the expression.
EX-
main(){
int a=10,b;
b=a++;
printf(“Value of a=%d and b=%d”,a,b);
}
Output: Value of a=11 and b=10
9. What is a local block?
Any portion of a C program that is enclosed by the left brace ({) and the right brace (}) is considered as a local block.
Variables declared in a local block have local scope i.e they can be accessed within the block only.
EX: - A C function, a switch statement and also an if statement contain braces, so the portion of code between these two braces would be considered a local block.
10. What does the modulus operator (%) do? Can % operator be applied for floating point division?
The modulus operator (%) gives the remainder when a number is divided by another number.
Ex:- 10%5 gives 0 as remainder, 15%4 gives 3 as remainder.
The % operator cannot be used for floating point division. For floating point division, we use fmod(a,b) function to get the remainder.
Ex:- fmod(6.8,2.2) gives 0 as remainder, fmod(7.1,2.2) gives 0.3 as remainder.
11. What are Data Type Modifiers?
Modifiers are keywords which are used as prefix with basic data types to modify (either increase or decrease) the amount of storage space allocated to a variable.
EX: - In 32 bit processor, storage space for int is 4 byte. But its range is increased to 8 byte by using long int and decreased to 2 byte by using short int.
The types of modifiers are:
• long
• short
• unsigned
• signed
12. What are storage class specifiers?
Storage class specifiers are the keywords which tells the compiler where to store a variable, how to store the variable, what is the initial value of the variable, life time of the variable and scope of the variable.
The syntax is: storageClassSpecifier dataType variableName;
Types of Storage Class Specifiers are:
• auto
• extern
• static
13. What is type conversion in C?
The process of converting one predefined type into another is called type conversion.
Two types:
1. Implicit Type Conversion or, Type Promotion
The type conversion performed by the compiler automatically without programmer’s intervention is known as implicit type conversion or type promotion.
2. Explicit Type Conversion or, Type Casting
The type conversion performed by the programmer by posing the data type of the expression of specific type is known as explicit type conversion or type casting.
Type casting Syntax: (data_type)expression;
Here expression may be constant, variable or expression.
14. What is the difference between a variable declaration and a variable definition?
Variable Declaration
Declaration of a variable means describing its type to the compiler but not allocating any space for it.
Ex:- extern int p;
Variable Definition
Definition of a variable means declaring it and also allocating space to hold the variable. We can also initialize a variable at the time it is defined.
Ex:- int q=5;
15. What is function?
A C function is defined as a set of instructions enclosed by “{ }” which performs specific operation in a C program. The collection of these functions forms a C program.
It provides modularity and code reusability.
16. Show the function declaration, function definition and function call with an example.
int sum(int,int); // Function declaration
main()
{
int a=5,b=10,s;
s=sum(a,b); //Function call
printf(“Sum=%d”,s);
}
int sum(int x, int y){ return (x+y);} // Function definition
17. Differentiate between call by value and call by reference.
Call by value
In case of call by value, a copy of value is passed to the function, so original value is not modified.
void modify(int a,int b)
{
a=a+5;
b=b-5;
}
main()
{
int a=5,b=20;
modify(a,b);
printf(“Value of a=%d and b=%d”,a,b);
}
Output: Value of a=5 and b=20.
Call by reference
In case of call by reference, an address of value is passed to the function, so original value is modified.
void modify(int *p,int *q)
{
*p = *p+5;
*q = *q -5;
}
main()
{
int a=5,b=20;
modify(&a,&b);
printf(“Value of a=%d and b=%d.”,a,b);
}
Output: Value of a=10 and b=15.
18. What are formal arguments and actual arguments.
Formal arguments
The arguments of a function during the function declaration or function definition are called formal arguments.
Actual arguments
The arguments of a function during the function call are called actual arguments.
Example:-
int sum(int a,int b); // Function declaration---- a & b are formal arguments
main()
{
int a=5,b=10,s;
s=sum(a,b); //Function call---- a & b are actual arguments
printf(“Sum=%d”,s);
}
int sum(int x, int y) // Function definition---- x & y are formal arguments
{
return (x+y);
}
0 Comments