C previously asked questions

Q. What is the purpose of main() function?
Ans: The main function invokes other functions within it and the execution of a program always starts with the main() function.

Q. What are header files and their uses?
Ans: Header files also known as library files contain the function definition and prototypes which are used in a program. For example the stdio.h header file contains the definitions and prototypes of the functions like printf() and scanf().

Q. Name the files that automatically open when a C file is executed.
Ans: Standard input, standard output and standard error.
Q. Why is a semicolon (;) put at the end of every program statement?
Ans: A semicolon after a statement acts as a terminator, Which tells the compiler where each statement is ending. It may proceed to divide the statements into smaller components for checking the syntax.
Q. Difference between ++a and a++?
Ans: In ++a the value of the variable is incremented first then the resulting value is used in the operation. It is called as prefix increment. But in the case of a++, the current value of the variable is used in operation then the value of the variable is incremented. It is known as postfix increment.

Q. What does static variable mean?
Ans: It is the variable which is not seen outside the function in which it is declared but remains available until the program terminates.
Q. What is Bus Error?
Ans: It is a fatal error in the execution of a machine language instruction which occurs because the processor detects an abnormal condition on its bus.
Q. What are macros?
Ans: A macro is a block of statement as a preprocessor directive. Being a preprocessor the block of code is communicated to the compiler before entering into the actual code. It is defined with the preprocessor directive #define.

Q. What is “Segmentation Violation”?
Ans: It usually occurs when a program attempts to access the memory location which is not allowed to exist. This occurs due to invalid page faults.

Q. Difference between pass by reference and pass by value?
Ans: Pass by value always invokes or calls the function or returns a value that is based on the value. This value is passed as a constant or a variable which contains a value. But, pass by reference always invokes or calls the function by passing the address or a pointer to a memory location which contains the actual value. The function can update or change the value available at that memory location by reference to the pointer.
Q. What are register variable and their advantages?
Ans: The variables of register type modifier basically informs the compiler to store the variables in a register of CPU. The advantages of a register variable are excess optimisation and speed of program execution. The operations of these variables are comparably much faster.
Q. What is the memory leak?
Ans: An unwanted increase in programs is referred to as a memory leak. It leads to an unintentional increase in consumption of the memory. Memory leakage may cause the function of the system to stop and violation of operating system files.
Q. Which arithmetic operation can you perform on a void* pointer?
Ans: There are no arithmetic operations that can be performed on a void* pointer. Because the compiler doesn’t know the size of the pointed object.

Q. What is “auto” keyword?
Ans: It is a local variable with a local lifetime. It is declared by auto storage class specifier. This variable is visible only in a block in which it is declared. the value of an uninitialized auto variable is undefined.

Q. What is “extern” keyword?
Ans: When we use extern in the declaration of a function, it means that function is implemented externally.The program doesn’t reserve any memory for a variable declared as extern.

Q. What is a break statement?
Ans: A break statement causes the loop to terminate. Control is then passed to next block of code following the body of the loop.

Q. What is the difference between structure and union?
Ans: A structure can have different types of data type inside it and all the variables of different data types use a different memory location. Hence all the variables of various data type declared within a  structure are active at the same time. But in the case of a union, the member variable declared inside union stores the contents at the exact same memory location. So, only one data member is active at a time.
Q. What is the difference between #include”filename” and #include ?
Ans: In #include”filename” preprocessor looks for the file to be included in the same directory where the current source file resides. But in #include the preprocessor searches for the file in directories pre-designed by the compiler that means the directories where standard library header files reside.

Q. What are dangling pointers and how are they different from memory leaks?
Ans: Dangling pointers are those that point to memory locations which have already been freed. Memory leaks happen when memory locations are not freed, but there is no way to refer to them.
Q. What is the difference between pre-increment and post-increment operator?
Ans: Pre-increment operator is used to incrementing the variable value by 1 before assigning the value to the variable. But post-increment operator is used to incrementing the variable value by 1 after assigning the value to the variable.
Q. What is “&” and “*” operators in C?
Ans: The “&” operator is used to get the address or the memory location of a particular variable. But “*” operator is used to get the value stored inside a specified address.

Q. What is the use of “goto” statement?
Ans: The goto statement is used to transfer the normal flow of a program to the specified label in the program.

Q. What is the “extern” and “static” function in C?
Ans: Extern function can be used in any other source file of the same project which has many other files, but a static function can’t be used in other files of the same project.

Q. Can a variable be both volatile and constant in C?
Ans: The value of a constant variable can’t be changed by the internal program once it is declared. But a volatile variable’s value may change at any time.
Q. What does the following declaration mean?         int(*ptr)[10];
Ans: It is a pointer to an array of 10 integers.

Q. What function is used to free the memory allocated by calloc()?
Ans: free();

Q. What is the difference between new and malloc?
Ans: The new() initializes the allocated memory by calling the constructor. Memory allocated with new should be released with delete(). But malloc() allocated uninitialized memory and allocated memory has to be released with free().

Q. What is the difference between function overloading and operator overloading?
Ans: Function overloading means you can define many functions with the exact same name, but the parameter passed through the functions are has to be of different types or different numbers. On the other hand, operator overloading means you can use almost all built in operators as a function to do any task that you want rather than what it actually does.
Q. What is dynamic binding (late binding)?
Ans: Dynamic binding is a process where the code associated with a given function call is not known until the time of that function call at runtime.
Q. What is the difference between class and structure?
Ans:
·         A structure contains only data but a class can have both data and member functions.
·         A class provides data hiding facility, but the structure doesn’t.
·         By default, the members of structures are public, but for a class is private.

Q. What is the difference between implicit and explicit conversion?
Ans: If you are doing a conversion between a smaller to bigger datatype that means a wider conversion, then it is an implicit conversion. For example, if an int variable is to be converted to float then it is an implicit conversion.
int x = 10;
float y;
y = x;
If you are converting a bigger data type to a smaller one, then it is called explicit conversion.
int x;
float y = 10.5;
x = y;
Q. How can you reallocate pointers?

Ans: Using realloc().

Q. WHAT IS C LANGUAGE?

·         C language is a structure/procedure oriented, middle level programming language developed at Bell Laboratories in 1972 by Dennis Ritchie.
·         C language was invented for implementing UNIX operating system.
·         In 1978, Dennis Ritchie and Brian Kernighan published the first edition “The C Programming Language”.
·         Also, C language is an ANSI/ISO standard and powerful programming language for developing real time applications

Q.Why is C known as a mother language?

C is known as a mother language because most of the compilers and JVMs are written in C language. Most of the languages which are developed after C language has borrowed heavily from it like C++, Python, Rust, javascript, etc. It introduces new core concepts like arrays, functions, file handling which are used in these languages.

Q. Why is C called a mid-level programming language?

C is called a mid-level programming language because it binds the low level and high -level programming language. We can use C language as a System programming to develop the operating system as well as an Application programming to generate menu driven customer driven billing system.

Q. What are the features of the C language?

The main features of C language are given below:
  • Simple: C is a simple language because it follows the structured approach, i.e., a program is broken into parts
  • Portable: C is highly portable means that once the program is written can be run on any machine with little or no modifications.
  • Mid Level: C is a mid-level programming language as it combines the low- level language with the features of the high-level language.
  • Structured: C is a structured language as the C program is broken into parts.
  • Fast Speed: C language is very fast as it uses a powerful set of data types and operators.
  • Memory Management: C provides an inbuilt memory function that saves the memory and improves the efficiency of our program.
  • Extensible: C is an extensible language as it can adopt new features in the future.

Q.What is the use of printf() and scanf() functions?

printf(): The printf() function is used to print the integer, character, float and string values on to the screen.
Following are the format specifier:
  • %d: It is a format specifier used to print an integer value.
  • %s: It is a format specifier used to print a string.
  • %c: It is a format specifier used to display a character value.
  • %f: It is a format specifier used to display a floating point value.
scanf(): The scanf() function is used to take input from the user.

Q. What is the difference between the local variable and global variable in C?

Following are the differences between a local variable and global variable:
Basis for comparison
Local variable
Global variable
Declaration
A variable which is declared inside function or block is known as a local variable.
A variable which is declared outside function or block is known as a global variable.
Scope
The scope of a variable is available within a function in which they are declared.
The scope of a variable is available throughout the program.
Access
Variables can be accessed only by those statements inside a function in which they are declared.
Any statement in the entire program can access variables.
Life
Life of a variable is created when the function block is entered and destroyed on its exit.
Life of a variable exists until the program is executing.
Storage
Variables are stored in a stack unless specified.
The compiler decides the storage location of a variable.


Q. What is the use of a static variable in C?

Following are the uses of a static variable:
  • A variable which is declared as static is known as a static variable. The static variable retains its value between multiple function calls.
  • Static variables are used because the scope of the static variable is available in the entire program. So, we can access a static variable anywhere in the program.
  • The static variable is initially initialized to zero. If we update the value of a variable, then the updated value is assigned.
  • The static variable is used as a common value which is shared by all the methods.
  • The static variable is initialized only once in the memory heap to reduce the memory usage.

Q. What is the use of the function in C?

Uses of C function are:
  • C functions are used to avoid the rewriting the same code again and again in our program.
  • C functions can be called any number of times from any place of our program.
  • When a program is divided into functions, then any part of our program can easily be tracked.
  • C functions provide the reusability concept, i.e., it breaks the big task into smaller tasks so that it makes the C program more understandable.

Q. What is the difference between call by value and call by reference in C?

Following are the differences between a call by value and call by reference are:
Call by value
Call by reference
Description
When a copy of the value is passed to the function, then the original value is not modified.
When a copy of the value is passed to the function, then the original value is modified.
Memory location
Actual arguments and formal arguments are created in separate memory locations.
Actual arguments and formal arguments are created in the same memory location.
Safety
In this case, actual arguments remain safe as they cannot be modified.
In this case, actual arguments are not reliable, as they are modified.
Arguments
The copies of the actual arguments are passed to the formal arguments.
The addresses of actual arguments are passed to their respective formal arguments.

Q. What is recursion in C?

When a function calls itself, and this process is known as recursion. The function that calls itself is known as a recursive function.
Recursive function comes in two phases:
  1. Winding phase
  2. Unwinding phase
Winding phase: When the recursive function calls itself, and this phase ends when the condition is reached.
Unwinding phase: Unwinding phase starts when the condition is reached, and the control returns to the original call.
Q. What is an array in C?
An Array is a group of similar types of elements. It has a contiguous memory location. It makes the code optimized, easy to traverse and easy to sort. The size and type of arrays cannot be changed after its declaration.
Arrays are of two types:
One-dimensional array: One-dimensional array is an array that stores the elements one after the another.
Syntax:
data_type array_name[size]; 
Multidimensional array: Multidimensional array is an array that contains more than one array.
Syntax:
data_type array_name[size];

Q. What is a pointer in C?

A pointer is a variable that refers to the address of a value. It makes the code optimized and makes the performance fast. Whenever a variable 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.
For example:
Data_type *p; 

Q. What is the usage of the pointer in C?

  • Accessing array elements: Pointers are used in traversing 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: The pointers are used to pass a reference of a variable to other function.
  • Data Structures like a tree, graph, linked list, etc.: The pointers are used to construct different data structures like tree, graph, linked list, etc.

Q. What is a NULL pointer in C?

A pointer that doesn't refer to any address of value but NULL is known as a NULL pointer. When we assign a '0' value to a pointer of any type, then it becomes a Null pointer.

Q. What is a far pointer in C?

A pointer which can access all the 16 segments (whole residence memory) of RAM is known as far pointer. A far pointer is a 32-bit pointer that obtains information outside the memory in a given section.

Q. What is dangling pointer in C?

  • If a pointer is pointing any memory location, but meanwhile another pointer deletes the memory occupied by the first pointer while the first pointer still points to that memory location, the first pointer will be known as a dangling pointer. This problem is known as a dangling pointer problem.
  • Dangling pointer arises when an object is deleted without modifying the value of the pointer. The pointer points to the deallocated memory.

Q. What is pointer to pointer in C?

In case of a pointer to pointer concept, one pointer refers to the address of another pointer. The pointer to pointer is a chain of pointers. Generally, the pointer contains the address of a variable. The pointer to pointer contains the address of a first pointer. 

Q. What is static memory allocation?

  • In case of 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.
  • For example:  
  • int a[10];

Q. What is dynamic memory allocation?

  • In case of 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.
  • For example  
  • int *p= malloc(sizeof(int)*10); 

Q. What functions are used for dynamic memory allocation in C language?

  1. 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.
Syntax
ptr = (cast-type*) malloc(byte-size) // allocating the memory using malloc() function.  
2.      calloc()
The calloc() is same as malloc() function, but the difference only is that it initializes the memory with zero value.
Syntax
ptr = (cast-type*)calloc(n, element-size);// allocating the memory using calloc() function.
3.      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.
Syntax
ptr = realloc(ptr, newsize); // updating the memory size using realloc() function. 
In the above syntax, ptr is allocated to a new size.
4.      free():The free() function releases the memory allocated by either calloc() or malloc() function.
Syntax
free(ptr); // memory is released using free() function

Q. What is the difference between malloc() and calloc()?


calloc()
malloc()
Description
The malloc() function allocates a single block of requested memory.
The calloc() function allocates multiple blocks of requested memory.
Initialization
It initializes the content of the memory to zero.
It does not initialize the content of memory, so it carries the garbage value.
Number of arguments
It consists of two arguments.
It consists of only one argument.
Return value
It returns a pointer pointing to the allocated memory.
It returns a pointer pointing to the allocated memory.
Q. 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.
Syntax of structure

struct structure_name 
  Member_variable1; 
 Member_variable2 
}[structure variables]; 

Q. What is a union?

  • The union is a user-defined data type that allows storing multiple types of data in a single unit. However, it doesn't occupy the sum of the memory of all members. It holds the memory of the largest member only.
  • In union, we can access only one variable at a time as it allocates one common space for all the members of a union.
Syntax of union
union union_name  
{  
Member_variable1;  
Member_variable2;  
.  
.  
Member_variable n;  
}[union variables]; 

Q. What is an auto keyword in C?
In C, every local variable of a function is known as an automatic (auto) variable. Variables which are declared inside the function block are known as a local variable. The local variables are also known as an auto variable. It is optional to use an auto keyword before the data type of a variable. If no value is stored in the local variable, then it consists of a garbage value.

Q. What is the purpose of sprintf() function?

The sprintf() stands for "string print." The sprintf() function does not print the output on the console screen. It transfers the data to the buffer. It returns the total number of characters present in the string.
Syntax
int sprintf ( char * str, const char * format, ... );  -

Q. Can we compile a program without main() function?

Yes, we can compile, but it can't be executed.
But, if we use #define, we can compile and run a C program without using the main() function. For example:
#include<stdio.h>    
#define VRS main    
void VRS() {    
   printf("Hello");    
}    

Q. What is a token?

The Token is an identifier. It can be constant, keyword, string literal, etc. A token is the smallest individual unit in a program. C has the following tokens:
  1. Identifiers: Identifiers refer to the name of the variables.
  2. Keywords: Keywords are the predefined words that are explained by the compiler.
  3. Constants: Constants are the fixed values that cannot be changed during the execution of a program.
  4. Operators: An operator is a symbol that performs the particular operation.
  5. Special characters: All the characters except alphabets and digits are treated as special characters.

Q. What is command line argument?

The argument passed to the main() function while executing the program is known as command line argument. For example:
main(int count, char *args[]){  
//code to  be executed  
}

Q. What is  ANSI?

The ANSI stands for " American National Standard Institute." It is an organization that maintains the broad range of disciplines including photographic film, computer languages, data encoding, mechanical parts, safety and more.

  Q. What is the difference between getch() and getche()?

The getch() function reads a single character from the keyboard. It doesn't use any buffer, so entered data will not be displayed on the output screen.
The getche() function reads a single character from the keyword, but data is displayed on the output screen. Press Alt+f5 to see the entered character.
Let's see a simple example
#include<stdio.h>  
#include<conio.h>  
int main()  
{  
      
 char ch;  
 printf("Enter a character ");  
 ch=getch(); // taking an user input without printing the value.  
 printf("\nvalue of ch is %c",ch);  
 printf("\nEnter a character again ");  
 ch=getche(); // taking an user input and then displaying it on the screen.  
  printf("\nvalue of ch is %c",ch);  
 return 0;  
}

Q. What is the newline escape sequence?

The new line escape sequence is represented by "\n". It inserts a new line on the output screen.

Q. What is the maximum length of an identifier?

It is 32 characters ideally but implementation specific.
  Q. What is typecasting?
The typecasting is a process of converting one data type into another is known as typecasting. If we want to store the floating type value to an int type, then we will convert the data type into another data type explicitly.
Syntax
(type_name) expression;  

Q. Can we access the array using a pointer in C language?

Yes, by holding the base address of array into a pointer, we can access the array using a pointer.

Q. What is an infinite loop?

A loop running continuously for an indefinite number of times is called the infinite loop.
Infinite For Loop:
for(;;){  
//code to be executed  
}  

Q. Write a program to print "hello world" without using a semicolon?

#include<stdio.h>      
void main(){      
 if(printf("hello world")){} // It prints the ?hello world? on the screen.  
}

Q.WHAT IS IDE?

·         IDE is nothing but Integrated Development Environment. IDE is a tool that provides user interface with compilers to create, compile and execute C programs.
·         Example: Turbo C++, Borland C++ and DevC++. These provide Integrated Development Environment with compiler for both C and C++ programming language.

Q.WHAT IS ENUM IN C?

·         Enumeration is a data type that consists of named integer constants as a list.
·         It start with 0 (zero) by default and value is incremented by 1 for the sequential identifiers in the list.

Q.WHAT IS VOID IN C?

·         Void is an empty data type that has no value.
·         We use void data type in functions when we don’t want to return any value to the calling function.
  • Example:
void add (int a, int b); – This function won’t return any value to the calling function.
int add (int a, int b); – This function will return value to the calling function

Q.WHAT IS STATIC FUNCTION IN C?

All functions are global by default in a C program/file. But, static keyword makes a function as a local function which can be accessed only by the program/file where it is declared and defined. Other programs/files can’t access these static functions.

Q.WHAT IS “##” OPERATOR IN C?

## is a pre-processor macro in C. It is used to concatenate 2 tokens into one token.
#include<stdio.h>

#define add(a,b) a ## b

int main ()
{
   int ab = 25;
   printf("The concatenated value is:%d \n",add(a,b));
   return 0;
}

NEXT

No comments:

Post a Comment