C Functions Interview Questions And Answers
Download C Functions Interview Questions and Answers PDF
Optimize your C Functions interview preparation with our curated set of 45 questions. Each question is designed to test and expand your C Functions expertise. Suitable for all experience levels, these questions will help you prepare thoroughly. Secure the free PDF to access all 45 questions and guarantee your preparation for your C Functions interview. This guide is crucial for enhancing your readiness and self-assurance.
45 C Functions Questions and Answers:
C Functions Job Interview Questions Table of Contents:
1 :: Do you have any idea how to compare array with pointer in C?
The following declarations are NOT the same:
char *p;
char a[20];
The first declaration allocates memory for a pointer; the second allocates memory for 20 characters.
Read Morechar *p;
char a[20];
The first declaration allocates memory for a pointer; the second allocates memory for 20 characters.
2 :: Do you know pointer in C?
A pointer is an address location of another variable. It is a value that designates the address or memory location of some other value (usually value of a variable). The value of a variable can be accessed by a pointer which points to that variable. To do so, the reference operator (&) is pre-appended to the variable that holds the value. A pointer can hold any data type, including functions.
Read More3 :: Tell me what is NULL pointer in C?
A null pointer does not point to any object.
NULL and 0 are interchangeable in pointer contexts.Usage of NULL should be considered a gentle reminder that a pointer is involved.
It is only in pointer contexts that NULL and 0 are equivalent. NULL should not be used when another kind of 0 is required.
Read MoreNULL and 0 are interchangeable in pointer contexts.Usage of NULL should be considered a gentle reminder that a pointer is involved.
It is only in pointer contexts that NULL and 0 are equivalent. NULL should not be used when another kind of 0 is required.
4 :: What is #pragma statements?
The #pragma Directives are used to turn ON or OFF certain features. They vary from compiler to compiler.
Examples of pragmas are:
#pragma startup // you can use this to execute a function at startup of a program
#pragma exit // you can use this to execute a function at exiting of a program
#pragma warn –rvl // used to suppress return value not used warning
#pragma warn –par // used to suppress parameter not used warning
#pragma warn –rch // used to suppress unreachable code warning
Read MoreExamples of pragmas are:
#pragma startup // you can use this to execute a function at startup of a program
#pragma exit // you can use this to execute a function at exiting of a program
#pragma warn –rvl // used to suppress return value not used warning
#pragma warn –par // used to suppress parameter not used warning
#pragma warn –rch // used to suppress unreachable code warning
5 :: Explain the advantages of using macro in C Language?
In modular programming, using functions is advisable when a certain code is repeated several times in a program. However, everytime a function is called the control gets transferred to that function and then back to the calling function. This consumes a lot of execution time. One way to save this time is by using macros. Macros substitute a function call by the definition of that function. This saves execution time to a great extent.
Read More6 :: Do you know what are bitwise shift operators in C Programming?
The bitwise operators are used for shifting the bits of the first operand left or right. The number of shifts is specified by the second operator.
Expression << or >> number of shifts
Ex:
number<<3;/* number is an operand - shifts 3 bits towards left*/
number>>2; /* number is an operand – shifts 2 bits towards right*/
The variable number must be an integer value.
For leftward shifts, the right bits those are vacated are set to 0. For rightward shifts, the left bits those are vacated are filled with 0’s based on the type of the first operand after conversion.
If the value of ‘number’ is 5, the first statement in the above example results 40 and stored in the variable ‘number’.
If the value of ‘number’ is 5, the second statement in the above example results 0 (zero) and stored in the variable ‘number’.
Read MoreExpression << or >> number of shifts
Ex:
number<<3;/* number is an operand - shifts 3 bits towards left*/
number>>2; /* number is an operand – shifts 2 bits towards right*/
The variable number must be an integer value.
For leftward shifts, the right bits those are vacated are set to 0. For rightward shifts, the left bits those are vacated are filled with 0’s based on the type of the first operand after conversion.
If the value of ‘number’ is 5, the first statement in the above example results 40 and stored in the variable ‘number’.
If the value of ‘number’ is 5, the second statement in the above example results 0 (zero) and stored in the variable ‘number’.
7 :: Tell me the use of bit field in C Language?
Bit Fields allow the packing of data in a structure.
This is especially useful when memory or data storage is at a premium
The maximum length of the field should be less than or equal to the integer word length of the computer.some compilers may allow memory overlap for the fields.Some store the next field in the next word.
C lets us do this in a structure definition by putting :bit length after the variable:
struct pack
{
unsigned int funny_int:9;
}p;
Also, Boolean datatype flags can be stored compactly as a series of bits using the bits fields. Each Boolean flag is stored in a separate bit.
Read MoreThis is especially useful when memory or data storage is at a premium
The maximum length of the field should be less than or equal to the integer word length of the computer.some compilers may allow memory overlap for the fields.Some store the next field in the next word.
C lets us do this in a structure definition by putting :bit length after the variable:
struct pack
{
unsigned int funny_int:9;
}p;
Also, Boolean datatype flags can be stored compactly as a series of bits using the bits fields. Each Boolean flag is stored in a separate bit.
8 :: Tell us the use of fflush() function in C Language?
In ANSI, fflush() [returns 0 if buffer successfully deleted / returns EOF on an error] causes the system to empty the buffer associated with the specified output stream.
It undoes the effect of any ungetc() function if the stream is open for input. The stream remains open after the call.
If stream is NULL, the system flushes all open streams.
However, the system automatically deletes buffers when the stream is closed or even when a program ends normally without closing the stream.
Read MoreIt undoes the effect of any ungetc() function if the stream is open for input. The stream remains open after the call.
If stream is NULL, the system flushes all open streams.
However, the system automatically deletes buffers when the stream is closed or even when a program ends normally without closing the stream.
9 :: Do you know the difference between exit() and _exit() function in C?
The following are the differences between exit() and _exit() functions:
- io buffers are flushed by exit() and executes some functions those are registered by atexit().
- _exit() ends the process without invoking the functions which are registered by atexit().
Read More- io buffers are flushed by exit() and executes some functions those are registered by atexit().
- _exit() ends the process without invoking the functions which are registered by atexit().
10 :: What is function prototype in C Language?
The basic definition of a function is known as function prototype. The signature of the function is same that of a normal function, except instead of containing code, it always ends with semicolon.
When the compiler makes a single pass over each and every file that is compiled. If a function call is encountered by the compiler, which is not yet been defined, the compiler throws an error.
One of the solution for the above is to restructure the program, in which all the functions appear only before they are called in another function.
Another solution is writing the function prototypes at the beginning of the file, which ensures the C compiler to read and process the function definitions, before there is a change of calling the function. If prototypes are declared, it is convenient and comfortable for the developer to write the code of those functions which are just the needed ones.
Read MoreWhen the compiler makes a single pass over each and every file that is compiled. If a function call is encountered by the compiler, which is not yet been defined, the compiler throws an error.
One of the solution for the above is to restructure the program, in which all the functions appear only before they are called in another function.
Another solution is writing the function prototypes at the beginning of the file, which ensures the C compiler to read and process the function definitions, before there is a change of calling the function. If prototypes are declared, it is convenient and comfortable for the developer to write the code of those functions which are just the needed ones.
11 :: What is the difference between malloc() and calloc() function in C Language?
The following are the differences between malloc() and calloc():
- Byte of memory is allocated by malloc(), whereas block of memory is allocated by calloc().
- malloc() takes a single argument, the size of memory, where as calloc takes two parameters, the number of variables to allocate memory and size of bytes of a single variable
- Memory initialization is not performed by malloc() , whereas memory is initialized by calloc().
- malloc(s) returns a pointer with enough storage with s bytes, where as calloc(n,s) returns a pointer with enough contiguous storage each with s bytes.
Read More- Byte of memory is allocated by malloc(), whereas block of memory is allocated by calloc().
- malloc() takes a single argument, the size of memory, where as calloc takes two parameters, the number of variables to allocate memory and size of bytes of a single variable
- Memory initialization is not performed by malloc() , whereas memory is initialized by calloc().
- malloc(s) returns a pointer with enough storage with s bytes, where as calloc(n,s) returns a pointer with enough contiguous storage each with s bytes.
12 :: Do you know what is the purpose of "extern" keyword in a function declaration?
The keyword ‘extern’ indicates the actual storage of the variable is visible , or body of a function is defined elsewhere, commonly in a separate source code. This extends the scope of the variables or functions to be used across the file scope.
Example:
extern int number;
Read MoreExample:
extern int number;
13 :: What is the difference between strcpy() and memcpy() function in C Programming?
The following are the differences between strcpy() and memcpy():
- memcpy() copies specific number of bytes from source to destinatio in RAM, where as strcpy() copies a constant / string into another string.
- memcpy() works on fixed length of arbitrary data, where as strcpy() works on null-terminated strings and it has no length limitations.
- memcpy() is used to copy the exact amount of data, whereas strcpy() is used of copy variable-length null terminated strings.
Read More- memcpy() copies specific number of bytes from source to destinatio in RAM, where as strcpy() copies a constant / string into another string.
- memcpy() works on fixed length of arbitrary data, where as strcpy() works on null-terminated strings and it has no length limitations.
- memcpy() is used to copy the exact amount of data, whereas strcpy() is used of copy variable-length null terminated strings.
14 :: Tell me what is the purpose of "register" keyword in C Language?
The keyword ‘register’ instructs the compiler to persist the variable that is being declared , in a CPU register.
Ex: register int number;
The persistence of register variables in CPU register is to optimize the access. Even the optimization is turned off; the register variables are forced to store in CPU register.
Read MoreEx: register int number;
The persistence of register variables in CPU register is to optimize the access. Even the optimization is turned off; the register variables are forced to store in CPU register.
15 :: Do you know what are the properties of Union in C?
A union is utilized to use same memory space for all different members of union. Union offers a memory section to be treated for one variable type , for all members of the union. Union allocates the memory space which is equivalent to the member of the union , of large memory occupancy.
Read More16 :: Explain the use of "auto" keyword in C Programming?
The keyword ‘auto’ is used extremely rare. A variable is set by default to auto. The declarations:
auto int number; and int number;
has the same meaning. The variables of type static, extern and register need to be explicitly declared, as their need is very specific. The variables other than the above three are implied to be of ‘auto’ variables. For better readability auto keyword can be used.
Read Moreauto int number; and int number;
has the same meaning. The variables of type static, extern and register need to be explicitly declared, as their need is very specific. The variables other than the above three are implied to be of ‘auto’ variables. For better readability auto keyword can be used.
17 :: What is self-referential structure in C Programming?
A self-referential structure is one of the data structures which refer to the pointer to (points) to another structure of the same type. For example, a linked list is supposed to be a self-referential data structure. The next node of a node is being pointed, which is of the same struct type. For example,
typedef struct listnode {
void *data;
struct listnode *next;
} linked_list;
In the above example, the listnode is a self-referential structure – because the *next is of the type sturct listnode.
Read Moretypedef struct listnode {
void *data;
struct listnode *next;
} linked_list;
In the above example, the listnode is a self-referential structure – because the *next is of the type sturct listnode.
18 :: What is C Programming structure?
Structures provide a convenient method for handling related group of data of various data types.
Structure definition:
struct tag_name
{
data type member1;
data type member2;
…
…
}
Example:
struct library_books
{
char title[20];
char author[15];
int pages;
float price;
};
The keyword struct informs the compiler for holding fields ( title, author, pages and price in the above example). All these are the members of the structure. Each member can be of same or different data type.
The tag name followed by the keyword struct defines the data type of struct. The tag name library_books in the above example is not the variable but can be visualized as template for the structure. The tag name is used to declare struct variables.
Ex: struct library_books book1,book2,book3;
The memory space is not occupied soon after declaring the structure, being it a template. Memory is allocated only at the time of declaring struct variables, like book1 in the above example. The members of the structure are referred as - book1.title, book1.author, book1.pages, book1.price.
Unions
Unions are like structures, in which the individual data types may differ from each other. All the members of the union share the same memory / storage area in the memory. Every member has unique storage area in structures. In this context, unions are utilized to observe the memory space. When all the values need not assign at a time to all members, unions are efficient. Unions are declared by using the keyword union , just like structures.
Ex: union item {
int code;
float price;
};
The members of the unions are referred as - book1.title, book1.author, book1.pages, book1.price.
Read MoreStructure definition:
struct tag_name
{
data type member1;
data type member2;
…
…
}
Example:
struct library_books
{
char title[20];
char author[15];
int pages;
float price;
};
The keyword struct informs the compiler for holding fields ( title, author, pages and price in the above example). All these are the members of the structure. Each member can be of same or different data type.
The tag name followed by the keyword struct defines the data type of struct. The tag name library_books in the above example is not the variable but can be visualized as template for the structure. The tag name is used to declare struct variables.
Ex: struct library_books book1,book2,book3;
The memory space is not occupied soon after declaring the structure, being it a template. Memory is allocated only at the time of declaring struct variables, like book1 in the above example. The members of the structure are referred as - book1.title, book1.author, book1.pages, book1.price.
Unions
Unions are like structures, in which the individual data types may differ from each other. All the members of the union share the same memory / storage area in the memory. Every member has unique storage area in structures. In this context, unions are utilized to observe the memory space. When all the values need not assign at a time to all members, unions are efficient. Unions are declared by using the keyword union , just like structures.
Ex: union item {
int code;
float price;
};
The members of the unions are referred as - book1.title, book1.author, book1.pages, book1.price.
19 :: What is the scope of static variables in C Language?
Static variables in C have the scopes;
1. Static global variables declared at the top level of the C source file have the scope that they can not be visible external to the source file. The scope is limited to that file.
2. Static local variables declared within a function or a block, also known as local static variables, have the scope that, they are visible only within the block or function like local variables. The values assigned by the functions into static local variables during the first call of the function will persist / present / available until the function is invoked again.
The static variables are available to the program, not only for the function / block. It has the scope within the current compile. When static variable is declared in a function, the value of the variable is preserved , so that successive calls to that function can use the latest updated value. The static variables are initialized at compile time and kept in the executable file itself. The life time extends across the complete run of the program.
Static local variables have local scope. The difference is, storage duration. The values put into the local static variables, will still be present, when the function is invoked next time.
Read More1. Static global variables declared at the top level of the C source file have the scope that they can not be visible external to the source file. The scope is limited to that file.
2. Static local variables declared within a function or a block, also known as local static variables, have the scope that, they are visible only within the block or function like local variables. The values assigned by the functions into static local variables during the first call of the function will persist / present / available until the function is invoked again.
The static variables are available to the program, not only for the function / block. It has the scope within the current compile. When static variable is declared in a function, the value of the variable is preserved , so that successive calls to that function can use the latest updated value. The static variables are initialized at compile time and kept in the executable file itself. The life time extends across the complete run of the program.
Static local variables have local scope. The difference is, storage duration. The values put into the local static variables, will still be present, when the function is invoked next time.
20 :: What is C standard library?
The C standard library is the standard library for the C programming language, as specified in the ANSI C standard. It was developed at the same time as the C POSIX library, which is a super-set of it.
Read More21 :: What is function prototype?
A function prototype is a mere declaration of a function. It is written just to specify that there is a function that exists in a program.
Read More22 :: What is fflush() function?
In ANSI, fflush() [returns 0 if buffer successfully deleted / returns EOF on an error] causes the system to empty the buffer associated with the specified output stream.
Read More25 :: Can you please explain the difference between malloc() and calloc() function?
Both functions are used to dynamically allocate the memory.
Read More