Top 50 most asked C programming interview questions

Published By: SHREY REDDY

About C Programming Language


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



C Programming Interview Questions


C programming interview questions will help you to understand the basics of the C language & gain confidence when you are going to attend C programming job interview.


It has been designed specially to get you to make familiar with the nature of questions you may experience during your interview for the subject of C programming language.


List of Top Most Asked Interview Questions


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.



Q2 Basic structure of C program


The structure of the C program is defined by a set of rules called a protocol to be followed by the programmer while writing C program, every c program are having these sections:


  • Documentation section - This is the first part of the C program.
  • Link - Header files 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 the particular tasks as per the user requirement.



Q3 What is a pointer in C? How to represent a 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 numbers. The variable that holds this address number is known as the pointer variable.


int main()

   {

        int *p; //Pointer

        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?


  • Accessing Array Elements - Pointers are used to make one's way across through an array of integers & strings "the string is an array of characters that 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 - It is used to construct different data structures like tree, graph, linked list, etc.



Q5 When should we use pointers in a C program?


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


Q6 What is a NULL pointer?


A 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, should make a pointer NULL when memory pointed by it is deallocated in the middle of a program.



Q7 What is a pointer on a 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.


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?


An 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 is used to compile the program. There are several such IDEs available in the market targeted towards the different operating systems.


  1. Turbo C
  2. Turbo C++


These are popular compilers that work under MS-DOS; Visual Studio 2008 and Visual Studio 2008 express edition are the compilers that work under windows, whereas is GCC compiler work under Linux. Visual Studio 2008 Express edition and GCC compilers are available free of cost.



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 on 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.



Q11 How to compile a 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.


  • Start the compiler at C> prompt. The compiler Turbo C is usually present in the 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 the Options menu and then select Compiler C++ options. In the dialog box that pops up, select CPP always use C++ compiler options.

Again select the Options menu and then select Environment Editor. Make sure that the default extension is C rather than CPP.



Q12 What are the variables and constants in C?


Variables are a tool to reserve space in a computer's memory & the reserved space is given a name, which we call a variable name.


Constants are stored in this reserved space by the variable may vary from time to time hence the name variable. The name was once given to a location, however, remains unchanged.


For example, i=4; 4 is a constant which is being stored in a location that has been given a name i. So, i is a variable just as a, x, Nate, or str are.


Accordingly, the 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 range -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 data type 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.


  • An arithmetic operation between an integer and an 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 yields a float result.
  • On assigning a float to an integer using the operator, the float is demoted to an integer.
  • On assigning an integer to the float, it is promoted to the float.


Integer conversions


  • i=5/2 (Result is 2)
  • i=5.0/2 (Result is 2)
  • i=5/2.0 (Result is 2)
  • i=2/5 (Result is 0)
  • i=2.0/5 (Result is 0)
  • i=2/5.0 (Result is 0)
  • i=2.0/5.0 (Result is 0)


Float Conversions


  • i=5/2 (Result is 2.000000)
  • i=5.0/2 (Result is 2.500000)
  • i=5/2.0 (Result is 2.500000)
  • i=2/5 (Result is 0.000000)
  • i=2.0/5 (Result is 0.400000)
  • i=2/5.0 (Result is 0.400000)
  • i=2.0/5.0 (Result is 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 standard library function used to display the output on the screen.


The general form of prinft() look like:


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


In the upper example, The first prinft() prints the value of the variable name, age, and sal. As against, second would print messages against each value displayed.


scanf() is once again a standard library function. It is used to receive the 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 occur only format specifications like %c, %d, %f, etc.

The variable names must always be preceded by the 'address of operator &'


Q15 What are the operators in C?


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.


  • 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 are relational operators? How to represent in C?


Relational operators allow us to compare two values to see whether they are equal to each other, unequal, or whether one is greater than the other.


  • 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 the main() function?


The main() function is the beginning function in c programming, it always returns an int value to the environment that is 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.



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.



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


A local static variable's lifetime doesn’t end with a function call where it is declared. It extends for the lifetime of a 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 is 0.



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


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


int i;

int main (void) 

{

   i = 2;

 }



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 the static keyword.
  • The static memory is implemented using stacks or heaps.
  • The pointer is required to access the variable present in the static memory.
  • 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 in dynamic memory allocation in the 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() function is the same as the 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 divide the program into subprograms (modules/function) to achieve the given task is the 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 access the same structure but the memory allocated for each variable will be different.


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 the loop in C Language?


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



Q27 How many types of loops are used in C?


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


  • 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 abbreviate 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 loops 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.


#include<stdio.h>

int main () 

{

   for(; ;)

     {

        printf("This loop will run forever\n");

     }

     return 0;

 }


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 to as a nested loop. The first loop is called the Outer loop and inside the loop is called the Inner Loop. Inner loop executes the number of times define an outer loop.



Q30 What are Arrays in C programming? How to use an 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.


Arrays Declaring


  • type arrayName [arraySize];


The array size must be an integer constant greater than zero and the 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 a small number of objects, but if we want to store a large number of instances, it becomes difficult to manage them with normal variables. The idea of an array is to represent many instances in one variable.



Q32 Can the sizeof operator be 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.


Arrays Initializing


  • 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?


  • 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 compared 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.


  • C is a structure/procedure-oriented programming language but C++ is an object-oriented programming language.
  • C language program design is a top-down approach but C++ is using a bottom-up approach.
  • Polymorphism, virtual function, inheritance, Operator overloading, namespace concepts are not available in C but the C++ language supports all these concepts and features.
  • C language gives importance to functions rather than data but C++ gives importance to data rather than functions.
  • Data and function mapping are difficult in C but Data and function mapping are simple in C++ that can be done using objects.
  • C language does not support user define data types but C++ supports user define data types.
  • Exception handling is not present in C but exception handling is present in the C++ language.
  • C language allows data to freely flow around the functions but 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.
  • 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 embedded systems in which embedded C is used to program and control these devices.



Q39 What is the difference between assembler, compiler & 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 in the program?


#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 the variable.



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


The major difference between FOR and WHILE loop is:


  • 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 the WHILE loop keeps the loop control statements together in one place.
  • FOR loop is used in more compact cases 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 the X variable.
  • X++ - X++ is called postfix increment and the increment happens after the value of X variable is used for the operations.



Q44 Is C language case sensitive?


Yes, C language is 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 & what are storage Variables?


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


Five types of storage classes are used:


  1. auto
  2. static
  3. extern
  4. register
  5. 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 a macro?


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


Syntax example of Macro-


  • #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 lists of integer constants with name.
  • Enumerators are defined with the keyword enum.
  • It start with 0 (zero) by default and the value is incremented by 1 for the sequential identifiers in the list.


Email Us: advertise@gdatamart.com

Donate Us: Support to GDATAMART

LifeStyle & Fun

© 2024 GDATAMART.COM (All Rights Reserved)