C Programming Interview Questions

In C Programming Interview Questions section, we are providing to understand C programming language and gain confidence in working with it, you would be required to type programs.

C Programming Interview Questions have been designed specially to get you make familiar with the nature of questions you may experieence during your interview for the subject of C Programming.

C programming interview question

About C Programming Language - C is a mid-level and system programming language. It is a technique in which large programs are broken down into smaller modules, and each module uses structured code.

Q1- What is C Programming Language?

C programming language is a computer programming language that was developed to do system programming for the operating system UNIX and is an imperative programming language.

  • C was developed in 1972 by Dennis Ritchie at bell Telephone Laboratories.
  • It is an outgrowth of an earlier language called B, which was also developed at Bell Laboratories.
  • C was largely confined to use within Bell Laboratories until 1978, when Brian Kernighan and Ritchie Published a definitive description of the language. More Details →

Q2- Basic structure of C program

Structure of C program is defined by set of rules called protocol, to be followed by programmer while writing C program, every c programs are having these sections which are mentioned below:

  • Documentation section - This is the first part of C program.
  • Link - Header files that are required to execute a C program.
  • Definition - In definition section, variables are defined and values are set to these variables
  • Global Declaration - When a variable is to be used throughout the program.
  • Function prototype declaration
  • Main function - Function prototype gives many information about a function like return type, parameter names used inside the function.
  • User defined function - User can define their own functions which perform particular task as per the user requirement.

Q3- What is a pointer in C? How to represent pointer?

A pointer (*p) is a variable that refers to the address of a value. It makes the code optimized & the performance fast. Whenever a variable (i) is declared inside a program, then the system allocates some memory to a variable. The memory contains some address number. The variables that hold this address number is known as the pointer variable.

Example Explained -

Example: data_type *p;
int main()
   {
        int *p;
        int i=5;
        p=&i;
        printf("Address value of 'i' variable is %u", p);
        return 0; 
   }

The above syntax tells that p is a pointer variable that holds the address number of a given data type value.

Q4- What is the usage of the pointer in C?

The use of pointer in C program, There are a small group of steps that you need to follow:

  • Accessing array elements - Pointers are used to make one's way across through an array of integers and strings. The string is an array of characters which is terminated by a null character '\0'.
  • Dynamic memory allocation - Pointers are used in allocation and deallocation of memory during the execution of a program.
  • Call by Reference - Pointers are used to pass a reference of a variable to other function.
  • Data Structures like a tree, graph, linked list - The pointers are used to construct different data structures like tree, graph, linked list etc.

Q5- When should we use pointers in a C program?

There are some points mentioned; When should we use pointers in a C program:

  • The first use of pointer to get address of a variable
  • Pointers allow different functions to share and modify their local variables.
  • To pass large structures so that complete copy of the structure can be avoided.
  • To implement “linked” data structures like linked lists and binary trees.

Q6- What is NULL pointer?

NULL Pointer is used to represent that the pointer doesn’t point to a valid location. Basically, we should initialize pointers as NULL if we don’t know their value at the time of declaration. Also, we should make a pointer NULL when memory pointed by it is deallocated in the middle of a program.

HTML Tutorials for Beginner →
C Programming Tutorials →

Q7- What is a pointer on pointer?

It’s a pointer (*p) variable which can hold the address of another pointer (**q) variable. It de-refers twice to point to the data held by the designated pointer variable.

Example Explained -

Example: int i=5, *p=&i, **q=&p;
int main()
   {
        int i=5;
        int *p, **q;
        p = &i;
        q = &p;                                                                
        printf("value of 'i' is : %d", i);
        Printf("\n");
        printf("value of '*p' is : %d", *p);
        Printf("\n");
        printf("value of '**q' is : %d", **q);
        return 0; 
   }

Therefore " i " can be accessed by **q.

Q8- What is embedded C Programming?

Historically, embedded C programming requires nonstandard extensions to the C language in order to support exotic features such as fixed-point arithmetic, multiple distinct memory banks, and basic I/O operations.

In 2008, the C Standards Committee published a technical report extending the C language to address these issues by providing a common standard for all implementations to adhere to. It includes a number of features not available in normal C, such as fixed-point arithmetic, named address spaces, and basic I/O hardware addressing.

Q9- What is Integrated Development Environment?

Integrated Development Environment is also called IDEs, it use to compile the program.There are several such IDEs available in the market targeted towards different operating system. For example, Turbo C and Turbo C++ are popular compailers that work under MS-DOS; Visual Studio 2008 and Visual Studio 2008 Express edition are the compilers that work under windows, where is gcc compiler work under Linux. Visual Asudio 2008 Express edition and gcc compilers are available free of cost. More Details →

Q10- ANIS C and ISO C

In 1983, the American National Standards Institute (ANSI) formed a committee, X3J11, to establish a standard specification of C. X3J11 based the C standard on the Unix implementation; however, the non-portable portion of the Unix C library was handed off to the IEEE working group 1003 to become the basis for the 1988 POSIX standard. In 1989, the C standard was ratified as ANSI X3.159-1989 "Programming Language C". This version of the language is often referred to as ANSI C, Standard C, or sometimes C89.

In 1990, the ANSI C standard (with formatting changes) was adopted by the International Organization for Standardization (ISO) as ISO/IEC 9899:1990, which is sometimes called C90. Therefore, the terms "C89" and "C90" refer to the same programming language. More Details →

Q11- How to compile program using Turbo C/Turbo C++ in C?

There are several steps that you need to follow to compile and execute programs using Turbo C / Turbo C++ in C programming. More Details →

  • Start the compiler at C> prompt. The compiler Turbo C is usually present in C:\TC\BIN directory.
  • Select New from the File menu
  • Type the Program
  • Save the program using F2 under a proper name for example (Program1.c)
  • Use Ctrl+F9 to compile and execute the program
  • Use Alt+F5 to view the output.

If you run this program in Turbo C++ compiler, you may get an error - The function printf should have a prototype

  • Select Options menu and then select Compiler C++ options. In the dialog box that pops up, select CPP always use C++ compiler options.
  • Again select Options menu and then select Environment Editor. Make sure that the default extension is C rather than CPP.

Q12- What is the variables and constants in C?

A variable is a tool to reserve space in computer's memory; and the reserved space is given a name, which we call a variable name. Constants are stored in this reserved space. The constants stored in the space reserved by the variable may vary from time to time, hence the name variable. The name once given to a location, however, remains unchanged. For example i=4; 4 is a constant which is being stored in a location which has been give a name i. So, i is a variable just as a, x, nate or str are. More Details →

Accorrdingly memory comes in three convenient versions - char, int, float:

  • A char can store values in the range -128 to +127
  • Integer (int) can store values in the rang -2147483648 to +2147483647
  • When we want to store numbers like 3.1415, or 0.0005672 in memory.
  • A float can store values ranging from -3.4e+38 to +3.4e+38.

The Memory occupied by each datatype can be found out using an operator called sizeof. For example, sizeof (int) would yield 4. Similarly, sizeof (float) would yield 4.

Q13- What is Integer and Float Conversions in C?

It is important to understand the rules that govern the conversion of floating point and integer values in C. These are mentioned- More Details →

  • An arithmetic operation between an integer and integer always yields an integer result.
  • An arithmetic operation between a float and float always yields a float result
  • In an arithmetic operation between an integer and float, the integer is first promoted to float and then the operation is carried out. And hence it always yield a float result.
  • On assigning a float to an integer using the operator, float is demoted to an integer.
  • On assigning an integer to a float, it is promoted to a float.
Integer conversions Float conversions
Operation Result Operation Result
i=5/2 2 a=5/2 2.000000
i=5.0/2 2 a=5.0/2 2.500000
i=5/2.0 2 a=5/2.0 2.500000
i=2/5 0 a=2/5 0.000000
i=2.0/5 0 a=2.0/5 0.400000
i=2/5.0 0 a=2/5.0 0.400000
i=2.0/5.0 0 a=2.0/5.0 0.400000

Q14- How to use printf() and scanf() function?

printf() is one of the most versatile statements in C programming. In fact, it is a standerd library fuction used to display the output on the screen. More Details →

The general from of prinft() look like:

  • printf("format string", list of variable);
  • Example - printf("name=%c age=%d salary=%f\n", name, age, sal);

The first prinft() print the value of the variable name, age and sal. As againest this, second would print messages against each value displayed. In the third prinft(), the \n is used to send the cursor to the next line.

scanf() is once again a standard library function. It is used to receive value of variables from the keyboard. For example, the following scanf() would let you supply the values of variables a and b.

  • scanf("%d %f", &a, &b)
  • Within the pair of double quotes there should accur only format specifications like %c, %d, %f etc.
  • The variable names must always be preceded by the 'address of operator &'

Q15- The operators in c language

C language supports a rich set of built-in operators. An operator is a symbol that tells the compiler to perform a certain mathematical or logical manipulation. Operators are used in programs to manipulate data and variables. More Details →

Classified Operators Operators Types
Arithmetic and Modulus operators *, /, %
Relational Operators <, >, <=, >=, ==, !=
Logical (AND and OR) operators && ||
Assignment operators =, +=, -=, *=, /=, %=
Conditional operators expression 1? expression 2: expression 3
Bitwise operotors &, |, ^, <<, >>
Special operotors sizeof, &, *

Q16- What is the relational operators? How to represent in C?

Relational Operators allow us to compare two values to see whether they are equal to ech other, unequal, or whether one is greater than the other. More Details →

Relational Expression Discriptions (is true if)
x==y x is equal to y
x!=y x is not equal to y
x<y x is less than y
x>y x is greater than y
x<=y x is less than or equal to y
x>=y x is greater than or equal to y

Q17- What is the Purpose Of main() Function?

The main() Function is the beginning function in c programming. The main function is always return an int value to the environment that called the program. And the program execution ends when the closing brace of the function main() is reached. Any user defined name can also be used as parameters for main() instead of argc and argv. More Details →

Q18- What are static functions?

The static function to specify the “static” keyword before a function name makes it static. Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. More Details →

Q19- What are local static variables? What is their use?

A local static variables lifetime doesn’t end with a function call where it is declared. It extends for the lifetime of complete program. All calls to the function share the same copy of local static variables.

Static variables can be used to count the number of times a function is called. Static variables get the default value as 0. Learn more about variables in C programming - More Details →

Static Variable Example -

void f() {
static int i;
    ++i;
    printf("%d", i);
  }

If a global variable is static then its visibility is limited to the same source code.

Q20- What is scope of a variable? How are variables scoped in C?

Scope of a variable is the part of the program where the variable may directly be accessible. In C, all identifiers are statically scoped.

Example -

int i;
int main (void) 
{
   i = 2;
 }

Can be accessed anywhere in a program.

Q21- What is static memory allocation?

  • In static memory allocation, memory is allocated at compile time, and memory can't be increased while executing the program. It is used in the array.
  • The lifetime of a variable in static memory is the lifetime of a program.
  • The static memory is allocated using static keyword.
  • The static memory is implemented using stacks or heap.
  • The pointer is required to access the variable present in the static memory.
  • The static memory is faster than dynamic memory.
  • In static memory, more memory space is required to store the variable.

Q22- What is dynamic memory allocation?

  • In dynamic memory allocation, memory is allocated at runtime and memory can be increased while executing the program. It is used in the linked list.
  • The malloc() or calloc() function is required to allocate the memory at the runtime.
  • An allocation or deallocation of memory is done at the execution time of a program.
  • No dynamic pointers are required to access the memory.
  • The dynamic memory is implemented using data segments.
  • Less memory space is required to store the variable.

Q23- What functions are used for dynamic memory allocation in C?

Basically, Fore functions are used for dynamic memory allocation in C language:

malloc()

  • The malloc() function is used to allocate the memory during the execution of the program.
  • It does not initialize the memory but carries the garbage value.
  • It returns a null pointer if it could not be able to allocate the requested space.

calloc()

  • The calloc() is same as malloc() function, but the difference only is that the fills allocated memory with 0’s value.

realloc()

  • The realloc() function is used to reallocate the memory to the new size.
  • If sufficient space is not available in the memory, then the new block is allocated to accommodate the existing data.

free()

  • The free() function releases the memory allocated by either calloc() or malloc() function.

Q24- Explain modular programming

When we are dividing the program in to sub programs (modules/function) to achieve the given task is modular approach. More generic functions definition gives the ability to re-use the functions, such as built-in library functions.

Q25- What is the structure?

The structure is a user-defined data type that allows storing multiple types of data in a single unit. It occupies the sum of the memory of all members.

  • The structure members can be accessed only through structure variables.
  • Structure variables accessing the same structure but the memory allocated for each variable will be different.

Example Explained -

struct student  
{  
    char name[10]; 
    int age;  
}s1;
int main()  
{  
    printf("Enter the name");  
    scanf("%s",s1.name);  
    printf("\n");  
    printf("Enter the age");  
    scanf("%d",&s1.age);  
    printf("\n");  
    printf("Name and age of a student: %s,%d",s1.name,s1.age);  
    return 0;  
}

Q26- What is loop in C Language?

A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages. For example - Suppose we want to print "number" 10 times. Given below is the general form of a loop statement in most of the programming languages

loop c programming

Q27- How many types of loops used in C?

C programming language provides the following types of loops to handle looping requirements. More Details →

Loop_Type_C Loop description
while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
for loop Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.
do..while loop Like a while statement, except that it tests the condition at the end of the loop body
nested loops You can use one or more loop inside any another while, for or do..while loop.

Q28- What is an infinite loop?

A loop running continuously for an indefinite number of times is called the infinite loop.

Example -

#include<stdio.h>
int main () 
{
   for(; ;)
     {
        printf("This loop will run forever\n");
     }
     return 0;
 }

Infinite For Loop.

When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment expression, but C programmers more commonly use the for(;;) construct to signify an infinite loop.

Q29- What is a nested loop?

A loop running within another loop is referred as a nested loop. The first loop is called Outer loop and inside the loop is called Inner loop. Inner loop executes the number of times define an outer loop.

Q30- What is an Arrays in C programming? How to use array?

Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Declaring Arrays -

type arrayName [arraySize];

The arraySize must be an integer constant greater than zero and type can be any valid C data type.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

Q31- Why do we need arrays?

We can use normal variables (v1, v2, v3,..) when we have small number of objects, but if we want to store large number of instances, it becomes difficult to manage them with normal variables. The idea of array is to represent many instances in one variable.

Q32- Can the sizeof operator is used to tell the size of an array passed to a function?

No. There's no way to tell, at runtime, how many elements are in an array parameter just by looking at the array parameter itself. And one more thing remember, passing an array to a function is exactly the same as passing a pointer to the first element. It means that passing pointers and arrays to C functions is very efficient.

Q33- Explain about Array Initializing

The number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ]. If you omit the size of the array, an array just big enough to hold the initialization is created.

Initializing Arrays -

double salary [5] = {100.0, 1000.0, 300.50, 20.65, 400.0};
double salary [] = {100.0, 1000.0, 300.50, 20.65, 400.0};

All arrays have 0 as the index of their first element which is also called the base index and the last index of an array will be total size of the array minus 1.

Q34- Mention The Characteristics Of Arrays In C

  • An array holds elements that have the same data type.
  • Array elements are stored in subsequent memory locations.
  • Two-dimensional array elements are stored row by row in subsequent memory locations.
  • Array name represents the address of the starting element.
  • Array size should be mentioned in the declaration. Array size must be a constant expression and not a variable.

Q35- What are the key features in C programming language?

  • Portability – Platform independent language.
  • Modularity – Possibility to break down large programs into small modules.
  • Flexibility – The possibility to a programmer to control the language.
  • Speed – C comes with support for system programming and hence it is compiling and executes with high speed when comparing with other high-level languages.
  • Extensibility – Possibility to add new features by the programmer.

Q36- What is the difference between structs and unions?

A struct is a complex data type that allows multiple variables to be stored in a group at a named block of memory. Each member variable of a struct can store different data, and they all can be used at once.

A union, on the other hand, stores the contents of any member variable at the exact same memory location. This allows the storage of different types of data at the same memory location. The result is that assigning a value to one member will change the value of all the other members. Unlike struct, only one member of the union type is likely to be useful at any given time.

Q37- What is the difference between C and C++?

C and C++ programming languages are belonging to middle level languages, both are differed in below:

C Language C++ Language
C is structure/procedure oriented programming language C++ is object oriented programming language.
C language program design is top down approach C++ is using bottom up approach.
Polymorphism, virtual function, inheritance, Operator overloading, namespace concepts are not available in C programming language. C++ language supports all these concepts and features.
C language gives importance to functions rather than data. C++ gives importance to data rather than functions.
Data and function mapping is difficult in C. Data and function mapping is simple in C++ that can be done using objects.
C language does not support user define data types. C++ supports user define data types.
Exception handling is not present in C programming language. Exception handling is present in C++ language.
C language allows data to freely flow around the functions. Data and functions are bound together in C++ which does not allow data to freely flow around the functions.

Q38- What is embedded C?

  • Embedded C is the extension of C programming language.
  • It is used to develop micro controller based applications.
  • Embedded C includes features not available in normal C like fixed-point arithmetic, named address spaces, and basic I/O hardware addressing.
  • Cell phones, MP3 players are some example for embedded systems in which embedded C is used to program and control these devices.

Q39- What is the difference between assembler, compiler and interpreter?

  • Assembler - It is a program that converts assembly level language (low level) into machine level language.
  • Compiler - Compiler compiles entire C source code into machine code.
  • Interpreters - It converts source code into intermediate code and then this intermediate code is executed line by line.

Q40- Write a C program to print - Hello, This is my first program?

Example -

#include<stdio.h>
int main () 
{
   printf("Hello, This is my first program\n");
   return 0;
}

Output - Hello, This is my first program

// printf() displays the string inside quotation

Q41- Differentiate Between A Linker And Linkage?

A linker converts an object code into an executable code by linking together the necessary build in functions. The form and place of declaration where the variable is declared in a program determine the linkage of variable.

Q42- What is the major difference between FOR and WHILE loop?

The major difference between FOR and WHILE loop are as follows:
Learn more FOR and WHILE loop →

  • FOR and WHILE loops are entry controlled loops it means test condition is checked for truth while entering into the loop’s body.
  • The FOR loop is usually appropriate for loops in which the initialization and increment are single statements and logically related whereas WHILE loop keeps the loop control statements together in one place.
  • FOR loop is used in more compact case comparing WHILE loop.

Q43- What is the difference between ++x and x++?

  • ++X - ++X is called prefixed increment and the increment will happen first on X variable.
  • X++ - X++ is called postfix increment and the increment happens after the value of X variable used for the operations.

Q44- Is C language case sensitive?

Yes. C language instructions/commands/functions and everything used in C program are case sensitive.

Q45- What Are The Advantages Of Auto Variables?

  • The same auto variable name can be used in different blocks.
  • There is no side effect by changing the values in the blocks.
  • The memory is economically used.
  • Auto variables have inherent protection because of local scope.

Q46- What Is Storage Class And What Are Storage Variable?

A storage class is an attribute that changes the behavior of a variable. It controls the lifetime, scope and linkage.

There are five types of storage classes:

  • auto
  • static
  • extern
  • register
  • typedef

Q47-What Is The Stack?

The stack is where all the function's local variables are created. The stack also contains some information used to call and return from functions.

  • A “stack trace” is a list of which functions have been called, based on this information. When you start using a debugger, one of the first things you should learn is how to get a stack trace.
  • The stack is very inflexible about allocating memory; everything must be deallocated in exactly the reverse order it was allocated in.
  • Allocating memory off the stack is extremely efficient. One of the reasons C compilers generate such good code is their heavy use of a simple stack.

Q48- What is Macro? Why do we use macro?

Macro is a name which is given to a value or to a piece of code/block in a program. Instead of using the value, we can use macro which will replace the value in a program.

Macro Syntax -

#define <MACRO_NAME> VALUE

Q49- What Are The Standard Predefined Macros?

The ANSI C standard defines six predefined macros for use in the C language:

  • _LINE_ Inserts the current source code line number in your code.
  • _FILE_ Inserts the current source code filename in your code.
  • _DATE_ Inserts the current date of compilation in your code.
  • _TIME_ Inserts the current time of compilation in your code.
  • _cplusplus Is defined if you are compiling a C++ program.

Q50- What are enumerations? What is enum in C?

  • Enumerations are list of integer constants with name. Enumerators are defined with the keyword enum.
  • It start with 0 (zero) by default and value is incremented by 1 for the sequential identifiers in the list.

GDATAMART

We are optimized for learning and job information. We try to improve your knowledge and practical experience through this site but we cannot warrant full correctness of all content. Learn More →

© 2019 gdatamart.com | All Rights Reserved