C Interview questions

C LOGO

C Programming

Interview Questions for Freshers and Experienced.

Dennis Ritchie designed C in 1972 at Bell Laboratories as a general-purpose programming language. Despite its age, it is a fairly popular language.

Top Interview Questions

1.What makes C a mid-level programming language, and why?

C possesses traits from both lower-level (assembly level) and higher-level (higher level) languages. C is hence frequently referred to as a middle-level language. A user can write an operating system and a consumer billing system in C using the language.

 

2. What characteristics does the C language have?

Some of the C language's features include: 

  1. It is Easy to Use and Effective. 
  2. The C language is machine independent or portable.
  3. A middle-level programming language is C. 
  4. It is a programming language with structure.
  5. Its library is functionally rich.
  6. Manage memory dynamically.
  7. C is quite quick.
  8. In C, pointers can be used.
  9. It can be expanded.

 

3. What do tokens mean?

Tokens are the individual components of a programme. The six types of tokens listed below are available in C:

  • Identifiers
  • Keywords
  • Constants
  • Operators
  • Special Characters
  • Strings

 

4. Why are the functions printf() and scanf() used? Describe format specifiers as well.

The output is printed on the screen using the printf() function.

To read formatted data from the keyboard, use the scanf() function. 

The following are some data type format specifiers for printing and scanning:

 

%d:The data type format specifier %d is used to print and scan integer values. 

%s:The data type format specifier %s is used to print and scan strings. 

%c:The data type format specifier %c is used to display and scan character values.

%f: To display and scan a float value, use the data type format specifier%f.

 

5.  What does the expression 5["abcdef"] represent?

'f' is the correct response.

Explanation: The expression is equal to "abcdef" and the string "abcdef" is an array. The inside-out expression is equivalent, but why?  b[a] is equivalent to a[b] because *(a + b) is equivalent to *(b + a) is equivalent to *(b + a).

 

6. What in C is a built-in function?

The built-in C functions scanf(), printf(), string copy(), string write(), string comparison(), string length(), string concatenation(), and many more are the most often used ones.

Built-in functionality, usually referred to as library functions, is a convenience feature offered by the system that helps developers complete a number of frequently performed predefined activities. For instance, in C, we use printf() to print output or our programme into the terminal.

 

7.What is a preprocessor, exactly?

A preprocessor is a piece of software that performs operations on a source file before submitting it for compilation. The preprocessor allows for the inclusion of header files, macro expansions, conditional compilation, and line control.

 

8. What does the #line in C do?

The preprocessor #line, which accepts an argument as line number, is used in C to reset the line number in the code. Here is an illustration of the same.

#include <stdio.h> /*line 1*/

/*line 2*/

int main(){ /*line 3*/

/*line 4*/

printf("Hello world\n"); /*line 5*/

//print current line /*line 6*/

printf("Line: %d\n",__LINE__); /*line 7*/

//reset the line number by 36 /*line 8*/

#line 36 /*reseting*/

//print current line /*line 36*/

printf("Line: %d\n",__LINE__); /*line 37*/

printf("Bye bye!!!\n"); /*line 39*/

/*line 40*/

return 0; /*line 41*/

} /*line 42*/

 

9.How do you turn a number into a string?

The format string must be written as a string in a buffer along with a pointer to an array of char elements that need to be transformed.

 

int sprintf(char *str, const char *format, ...)

 

The output after running the above code:

Output: Value of Pi = 3.141593

 

10. In C, what is recursion?

Recursion occurs when a C function calls a copy of itself. To put it another way, this method is known as recursion when a function calls itself. This function is also referred to as a recursive function.

Recursive Function Syntax:

void do_recursion()

... .. ... 

do_recursion();

... .. ...]

int main() 

{

... .. ...

do_recursion();

... .. ...

}

11. Why is function overloading not supported in C?

The object code must still contain the symbol names after compiling the C source. In order to prevent function name conflicts, we should offer name mangling whenever we add function overloading to our source. Additionally, many things (like data types) in C are convertible to one another because it is not a rigorously typed language. As a result, in a language like C, the complexity of overload resolution might cause misunderstanding.

 

The names of symbols will be preserved during compilation of a C source. If function overloading is implemented, a name mangling approach should be included to avoid name conflicts. As a result, the built binary will have machine-generated symbol names similar to C++.

 

C does not have strict typing, either. In C, many relationships between items are implicitly convertible. The complexity of overload resolution rules may make this type of language confusing.

 

12. What distinguishes a static int declaration from a global int declaration?

This is where it differs from that—in scope. A variable that is genuinely global has a global scope and is apparent throughout your programme.

 

#include <stdio.h> 

 

int my_global_var = 0

 

int 

main(void

 

  printf("%d\n", my_global_var); 

  return 0

}

Everything in your programme can see the global variable global_temp, but if you have a multi-file project, you'll need to add a "extern int global_temp;" to other source files to make it visible in other modules.

 

Although a static variable's variables are not allocated in the memory's stack segment, it has a local scope. Although it sits in the.bss segment of your built binary, like global variables, it can have scope that is not global.

 

#include <stdio.h> 

 

int 

myfunc(int val) 

 

    static int my_static_var = 0

 

    my_static_var += val; 

    return my_static_var; 

 

int 

main(void

 

   int myval; 

 

   Myval = myfunc(1); 

   printf("first call %d\n", myval); 

 

   myval = myfunc(10); 

 

   printf("second call %d\n", myval); 

 

   return 0

}

13. In C, what is a pointer?

A pointer is a variable that holds or points to the address of another variable. A variable's address is maintained in a pointer variable, whereas its value is kept in a regular variable.

14. What is the difference between char const* and const char* p?

The pointer const char* p points to a const char.

The pointer char const* p points to a char const.

It is the same since const char and char const are equivalent.

 

15.What in C is pointer to pointer?

A pointer can also be used in C to store another pointer's address. Such a pointer is a double pointer or pointer to pointer. The first pointer contains the address of a variable, whereas the second pointer contains the address of the first pointer.

The following is the syntax for declaring a double pointer:

pointer to a pointer that points to an integer; int **p;

 

16.Why does n + + execute more quickly than n+1?

Since n++ is a unary operation, just one variable is required. In contrast, the binary operation n = n + 1 (also known as the binary operation n += 1) adds overhead and requires more time. However, on contemporary platforms, it is dependent on a few things like processor architecture, the C compiler you use, utilisation in your code, and other elements like hardware issues.

When it enters the optimised binary, it will be just as effective.

17. What does typecasting in C mean?

The process of changing a variable's datatype is known as typecasting.  We must explicitly transform the data type into another data type if we wish to store a huge type value as an int type.

Syntax: (data_type)expression;

For Example:

int x;

for(x=97; x<=122; x++)

{

   printf("%c", (char)x);   /*Explicit casting from int to char*/

}

18. What benefits does macro have over function?

Macro on an advanced copy-paste, its definitions to locations whenever it is called. Because the control is always with the callee function, it saves a lot of time since it is never sent to a new function. One drawback is that the compiled binary is enormous in size, but the programme comparably runs faster once it has been compiled.

19.What are enumerations, exactly?

Enumeration is a user-defined data type, often known as Enum in C. It is made up of constant integrals or integers with user-assigned names. The entire programme is easy to learn, comprehend, and maintain by the same programmer or even a new programmer because the integer values are named with enum in C.

 

20.When should the register storage specifier be used?

If a variable is often used, it should be defined using the register storage specifier; the compiler may then allocate a CPU register for the variable's storage to facilitate variable search.

 

21. What are the various categories of decision control statements?

A programme runs each statement that has been written sequentially from top to bottom. Depending on the circumstance, control statements are used to execute/transfer control from one area of the programme to another.

 

  • If-else statement.
    • normal if-else statement.
    • Else-if statement
    • nested if-else statement.
  • Switch statement.

 

22.What do an r-value and a l-value mean?

A data value that is kept in memory at a specific location is referred to as a "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 (=).

 

The phrase "l-value" describes a memory address that is used to identify

an object. Either the left or right side of the assignment operator (=) contains the l-value. An identifier that is widely used is l-value.

 

23. What distinguishes calloc() from malloc()?

The memory dynamic memory allocating routines calloc() and malloc(). The key distinction is that calloc() accepts two arguments—the number of blocks and the size of each block—instead of malloc()'s single argument, the number of bytes.

24.What is the distinction between a union and a struct in C?

A struct is a collection of intricate data structures that are kept together in memory and given independent memory locations so that they can all be accessed simultaneously.

 

In contrast, all of the member variables in a union are kept in the same location in memory, which means that changing the value of one member would also modify the values of all the other members.

/* struct & union definations*/

struct bar {

int a; // we can use a & b both simultaneously

char b;

} bar;

 

union foo {

int a; // we can't use both a and b simultaneously

char b;

} foo;

 

/* using struc and union variables*/

 

struct bar y;

y.a = 3; // OK to use

y.b = 'c'; // OK to use

 

union foo x;

x.a = 3; // OK

x.b = 'c'; // NOl this affects the value of x.a!

 

25. What does a function's call by reference mean?

Call by reference is the term for when a caller function calls a function without using the addresses of the actual parameters being given. Because all actions in incall by reference are conducted on the value stored in the address of real parameters, each operation made on formal parameters has an impact on the value of actual parameters.

 

26.What does function pass by reference mean?

The callee receives the address in pass by reference and copies the address of an argument into the formal parameter. The address is used by the callee function to retrieve the actual argument and perform manipulations. The caller function will also be aware of any modifications made to the value referenced at the given address by the call function.

 

27.What is a memory leak, exactly? How do you stop it?

Depending on the size of the data type, assigning a variable requires space in our RAM (either the heap or RAM). If a programmer uses memory from the heap without updating it, all of the RAM will eventually be used up, which could result in a memory leak.

int main()

{

    char * ptr = malloc(sizeof(int));

    

    /* Do some work */

    /*Not freeing the allocated memory*/

    return 0;

}

You can track every memory allocation you make in order to prevent memory leaks. Then, decide where you want to erase that memory and do so. Another method is to link it to GNU compilers using a C++ smart pointer.

 

28.What in C is dynamic memory allocation? The dynamic allocation methods should be named.

There are two main standard libraries in the C programming language: malloc() and free. C is renowned for its low-level control over the memory allocation of variables in DMA. The size of the RAM requested is the only input argument accepted by the malloc() function. It gives back a pointer to the RAM that was allotted. It returns NULL if the allocation is unsuccessful.

 The standard library function's prototype looks like this:

 

void *malloc(size_t size);
The memory is deallocated by the free() function using the pointer that malloc() returned. There is no feedback regarding success or failure. This is how the function prototype looks: 

void free(void *pointer);
To help with dynamic memory allocation in C programming, C provides 4 library functions that are defined under the stdlib.h> header file. As follows:

  • malloc()
  • calloc()
  • free()
  • realloc()

 

29. Describe typedef.

The C language keyword typedef is used to define aliases or synonyms for already existing types. We typically utilise typedef to streamline the current type declaration syntax. or to give a kind specific descriptive name.

typedef <existing-type> <new-type-identifiers>;

The complex type definition already exists and is given an alias name by typedef. Any type can easily have an alias created with typedef. Typedef will shorten your code whether it's a straightforward integer, a complicated function pointer, or a struct declaration.

 

30.Why is using gets() typically a poor idea? Offer a solution.

The gets() function of the standard input library reads user input until it comes across a new line character. The system is subject to buffer overflow and the input being written into memory where it shouldn't be because it does not verify that the size of the variable being provided by the user is below the maximum size of the data type.

As a result, we use gets() to accomplish the same with a limited set of input.

It was still a recognised element of the language until the 1999 ISO C standard, but the 2011 standard formally eliminated it. The majority of C implementations still support it, but at least GCC warns against using it.

 

31. What distinguishes #include "..." from #include...>?

The place where the preprocessor looks for the included file actually differs in practice.

The C preprocessor initially checks the predefined list of system directories for the filename for #include filename> before checking the user-specified directories (directories can be added to the predetermined list using the -I option). 

The preprocessor searches for #include "filename" in the same directory as the file containing the directive first, then uses the same search path as for the #include filename> form. This approach is typically used to include header files that are defined by programmes.

 

32. What distinguishes the letters "g" and "g" in the C language?

Single-quoted variables in C are identified as the character, but double-quoted variables are identified as a string. The string (double-quoted) variables' null terminator at the end, which makes them a 2-character array, is another significant difference.

 

 33. What do the terms "near pointer" and "far pointer" in C mean?

Close Pointer: The near pointer can be regarded generally because it is used to store the address, which can only have a maximum size of 16 bits. The close pointer cannot be used to store an address with a size greater than 16 bits. The 16-bit limit does not apply to any other smaller addresses, therefore they can all be saved. You might suppose the 16 bits are insufficient given that we can only read 64kb of data at once. It is no longer widely employed because it is thought to be one of the near-pointer's main shortcomings.

Far Pointer: A far pointer is a pointer that is 32 bits in size. However, it can make use of the current segment to access data kept outside of the computer's memory. Although we typically need to allocate the sector register to store the data address in the current segment in order to use this type of pointer.

 

34. What kind of structure connects the programme and the operating system?

The operating system and a programme are joined by the file structure. The file is defined by the "stdio.h" header file (standard input/output header file). It includes details about the file now being used, including its size and placement in memory. It includes a character pointer that directs the user to the character that is currently open. When a file is opened, a connection is made between the programme and the operating system so that they know which file will be accessed.

 

35. Assume a local and global variable has the same name. Can you access a global variable from a block that defines local variables?

No. In C, this is not feasible. The most local variable is usually given preference.

 

36.Which is preferable, #define or enum?

1.The compiler can create enum values automatically if we let it. The defined values must all be provided separately, though.

  1. The source code is oblivious of these macros because they are preprocessors rather than enums, which are compile-time entities. The enum is therefore preferable if we debug the function with a debugger.
  2. If we utilise enum values in a switch and the default case is absent, some compilers will issue a warning.

4.Enum always creates identifiers of the int type. On the other hand, the macro gave us the option to select among a number of integral types.

  1. The macro lacks a clearly known scope constraint, unlike enum.

 

37. Why does every programme statement terminate with a semicolon (;)?

The semicolon, which serves as a boundary between two sets of instructions in C, is crucial to how the compiler analyses (or parses) the entire code and breaks it down into a series of instructions.

 

38. Distinguish between source and object codes Source and object codes differences

Source code is a collection of computer instructions written in a language that can be read by humans, whereas Object code is the result of translating Source code using a compiler or an assembler into a series of statements that can be read by machines.

The manner in which modifications are reflected in Object Code is the final observation. Every time the source code is altered, the source code must be compiled to take into account the changes made to the Object Code.

 

39. What are header files and how are they used in C programming?

The.h extension is required for header files in the C programming language that define functions, data types, macros, etc. The header is useful for importing the aforementioned definitions into the source code using the #include directive. For instance, if you want your source code to accept user input, modify it, and output the results to the terminal, you should include the stdio.h file as #include stdio.h>. With this addition, we may use scanf() to capture user input, run some processing, then print using printf().

 

40.How do you define a dynamic data structure?

An arrangement or collection of data in memory known as a dynamic data structure (DDS) has the ability to expand or contract in size, giving a programmer complete control over how much memory is used. Dynamic data structures can adjust their size by allocating or releasing heap space as needed. 

Programming languages like C, C++, and Java rely heavily on dynamic data structures because they provide programmers the freedom to control how much memory a programme uses.