Sunday, February 3, 2019

C Basic Interview Questions

  1. 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;
    }
    0 

    Add a comment


  2. C language



    1. What is a pointer?
    The pointer in C language is a variable which stores the address of another variable. This variable can be of type int, char, array, function, or any other pointer. The size of the pointer depends on the architecture. However, in 32-bit architecture, the size of a pointer is 2 byte.

    Consider the following example to define a pointer which stores the address of an integer.

    int n = 10;   
    int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of type integer. 

        2. What is a dangling pointer?
    Dangling pointers arise when an object is deleted or de-allocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the de-allocated memory.
    In short pointer pointing to non-existing memory location is called  dangling pointer.
    Example:Using free or de-allocating memory
    #include<stdlib.h>
    {
        char *ptr = malloc(Constant_Value);
        .......
        .......
        .......
        free (ptr);      /* ptr now becomes a dangling pointer */
    }
    We have declared the character pointer in the first step. After execution of some statements, we have de-allocated memory which is allocated previously for the pointer.

    As soon as memory is de-allocated for pointer, pointer becomes dangling pointer

          3. What is a data type?

    In the C programming language, data types are declarations for memory locations or variables that determine the characteristics of the data that may be stored and the methods (operations) of processing that are permitted involving them.
    Data types specify how we enter data into our programs and what type of data we enter. C language has some predefined set of data types to handle various kinds of data that we can use in our program. These datatypes have different storage capacities.

    C language supports 2 different type of data types:

    Primary data types:
    These are fundamental data types in C namely integer(int), floating point(float), character(char) and void.

    Derived data types:
    Derived data types are nothing but primary datatypes but a little twisted or grouped together like array, stucture, union and pointer.
    4.     What is size of integer data type?
    • Integer data type allows a variable to store numeric values.
    • “int” keyword is used to refer integer data type.
    • The storage size of int data type is 2 or 4 or 8 byte.
    • It varies depend upon the processor in the CPU that we use.  If we are using 16 bit processor, 2 byte  (16 bit) of memory will be allocated for int data type.
    • Like wise, 4 byte (32 bit) of memory for 32 bit processor and 8 byte (64 bit) of memory for 64 bit processor is allocated for int datatype.
    • int (2 byte) can store values from -32,768 to +32,767
    • int (4 byte) can store values from -2,147,483,648 to +2,147,483,647.
    • If you want to use the integer value that crosses the above limit, you can go for “long int” and “long long int” for which the limits are very high
    5.     What is the size of char data type?
    • Character data type allows a variable to store only one character.
    • Storage size of character data type is 1. We can store only one character using character data type.
    • “char” keyword is used to refer character data type.
    • For example, ‘A’ can be stored using char datatype. You can’t store more than one character using char data type.


    6.     What is the size of unsigned char?
    Type
    Storage size
    Value range
    char
    1 byte
    -128 to 127 or 0 to 255
    unsigned char
    1 byte
    0 to 255
    signed char
    1 byte
    -128 to 127

    1. What is the size of signed char?
    Type
    Storage size
    Value range
    char
    1 byte
    -128 to 127 or 0 to 255
    unsigned char
    1 byte
    0 to 255
    signed char
    1 byte
    -128 to 127

    1. What is the size of long?
    Type
    Storage size
    Value range
    char
    1 byte
    -128 to 127 or 0 to 255
    unsigned char
    1 byte
    0 to 255
    signed char
    1 byte
    -128 to 127
    int
    2 or 4 bytes
    -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
    unsigned int
    2 or 4 bytes
    0 to 65,535 or 0 to 4,294,967,295
    short
    2 bytes
    -32,768 to 32,767
    unsigned short
    2 bytes
    0 to 65,535
    long
    4 bytes
    -2,147,483,648 to 2,147,483,647
    unsigned long
    4 bytes
    0 to 4,294,967,295
    1. What is the size of short?
    Type
    Storage size
    Value range
    char
    1 byte
    -128 to 127 or 0 to 255
    unsigned char
    1 byte
    0 to 255
    signed char
    1 byte
    -128 to 127
    int
    2 or 4 bytes
    -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
    unsigned int
    2 or 4 bytes
    0 to 65,535 or 0 to 4,294,967,295
    short
    2 bytes
    -32,768 to 32,767
    unsigned short
    2 bytes
    0 to 65,535
    long
    4 bytes
    -2,147,483,648 to 2,147,483,647
    unsigned long
    4 bytes
    0 to 4,294,967,295
    1. What is the range of integer data type in C?
    Type
    Storage size
    Value range
    char
    1 byte
    -128 to 127 or 0 to 255
    unsigned char
    1 byte
    0 to 255
    signed char
    1 byte
    -128 to 127
    int
    2 or 4 bytes
    -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
    unsigned int
    2 or 4 bytes
    0 to 65,535 or 0 to 4,294,967,295
    short
    2 bytes
    -32,768 to 32,767
    unsigned short
    2 bytes
    0 to 65,535
    long
    4 bytes
    -2,147,483,648 to 2,147,483,647
    unsigned long
    4 bytes
    0 to 4,294,967,295
    1. What is the size of float data type?
    Type
    Storage size
    Value range
    Precision
    float
    4 byte
    1.2E-38 to 3.4E+38
    6 decimal places
    double
    8 byte
    2.3E-308 to 1.7E+308
    15 decimal places
    long double
    10 byte
    3.4E-4932 to 1.1E+4932
    19 decimal places

    1. What is malloc?
    MALLOC():
    Ø  malloc () function is used to allocate space in memory during the execution of the program.
    Ø  malloc () does not initialize the memory allocated during execution.  It carries garbage value.
    Ø  malloc () function returns null pointer if it couldn’t able to allocate requested amount of memory.
    Syntax Examples:
    int *ptr;
    ptr=(int*)malloc(sizeof (int));           //2 byte
    long double*ldptr;
    ldptr=(long double*)malloc(sizeof(long double))        // 2 byte
    char*cptr;
    cptr=(char*)malloc(sizeof(char));  //1 byte
    int*arr;
    arr=(int*)malloc(sizeof int()*10);    //20 byte
    cahr*str;
    str=(char*)malloc(sizeof(char)*50);          //50 byte

    1. What is the difference between do while and for loop?
    while
    do while
    Loop is executed only when condition is true.
    Loop is executed for first time irrespective of the condition. After executing while loop for first time, then condition is checked.
    while ( condition) {
    statements; //body of loop
    }
    do{
    .
    statements; // body of loop.
    .
    } while( Condition );
    In 'while' loop the controlling condition appears at the start of the loop
    In 'do-while' loop the controlling condition appears at the end of the loop.
    The iterations do not occur if, the condition at the first iteration, appears false.
    The iteration occurs at least once even if the condition is false at the first iteration.

    1. What is a string?
    The string can be defined as the one-dimensional array of characters terminated by a null ('\0'). The character array or the string is used to manipulate text such as word or sentences. Each character in the array occupies one byte of memory, and the last character must always be 0. The termination character ('\0') is important in a string since it is the only way to identify where the string ends. When we define a string as char s[10], the character s[10] is implicitly initialized with the null in the memory.
    There are two ways to declare a string in c language.
    1. By char array
    2. By string literal
    Let's see the example of declaring string by char array in C language.
    char ch[10]={'c''l''a''n''g''u''a''g''e''\0'};  
    As we know, array index starts from 0, so it will be represented as in the figure given below.
    While declaring string, size is not mandatory. So we can write the above code as given below:
    char ch[]={'c''l''a''n''g''u''a''g''e''\0'};  
    We can also define the string by the string literal in C language. For example:
    char ch[]="clanguage";  
    In such case, '\0' will be appended at the end of the string by the compiler.
    1. /0 in string?
    It is the ascii value of null. It is use to identify whether there is null present in the string or not while performing string oeration.
    1. What is recursion?
    Recursion is the process which comes into existence when a function calls a copy of itself to work on a smaller problem. Any function which calls itself is called recursive function, and such function calls are called recursive calls. Recursion involves several numbers of recursive calls. However, it is important to impose a termination condition of recursion. Recursion code is shorter than iterative code however it is difficult to understand.
    Recursion cannot be applied to all the problem, but it is more useful for the tasks that can be defined in terms of similar subtasks. For Example, recursion may be applied to sorting, searching, and traversal problems.
    Generally, iterative solutions are more efficient than recursion since function call is always overhead. Any problem that can be solved recursively, can also be solved iteratively. However, some problems are best suited to be solved by the recursion, for example, tower of Hanoi, Fibonacci series, factorial finding, etc.
    1. What is printf?
    The printf() function is used for output. It prints the given statement to the console.
    The syntax of printf() function is given below:
    printf("format string",argument_list); 
    The format string can be %d (integer), %c (character), %s (string), %f (float) etc.
    1. What is math.h?
    C Programming allows us to perform mathematical operations through the functions defined in <math.h> header file. The <math.h> header file contains various methods for performing mathematical operations such as sqrt(), pow(), ceil(), floor() etc.
    1. What is the argument of a function?
    A function in C can be called either with arguments or without arguments. These function may or may not return values to the calling functions. All C functions can be called either with arguments or without arguments in a C program. Also, they may or may not return any values.
    1. What is assignment operator in C?
    These are used to assign the values for the variables in C programs.
    ·         In C programs, values for the variables are assigned using assignment operators.
    ·         For example, if the value “10” is to be assigned for the variable “sum”, it can be assigned as “sum = 10;”
    ·         There are 2 categories of assignment operators in C language. They are,
    1. Simple assignment operator ( Example: = )
    2. Compound assignment operators ( Example: +=, -=, *=, /=, %=, &=, ^= )

    1. What is the relational operator in C?
    Relational operators are used to find the relation between two variables. i.e. to compare the values of two variables in a C program.
    Operators
    Example/Description
    > 
    x > y (x is greater than y)
    < 
    x < y (x is less than y)
    >=
    x >= y (x is greater than or equal to y)
    <=
    x <= y (x is less than or equal to y)
    ==
    x == y (x is equal to y)
    !=
    x != y (x is not equal to y)
    1. What is the logical operator in C?
    These operators are used to perform logical operations on the given expressions.
    There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and logical NOT (!).
    Operators
    Example/Description
    && (logical AND)
    (x>5)&&(y<5)
    It returns true when both conditions are true
    || (logical OR)
    (x>=10)||(y>=10)
    It returns true when at-least one of the condition is true
    ! (logical NOT)
    !((x>5)&&(y<5))
    It reverses the state of the operand “((x>5) && (y<5))”
    If “((x>5) && (y<5))” is true, logical NOT operator makes it false


    1. What is the bitwise operator in C?
    These operators are used to perform bit operations. Decimal values are converted into binary values which are the sequence of bits and bit wise operators work on these bits.
    Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise NOT), ^ (XOR), << (left shift) and >> (right shift).
    TRUTH TABLE FOR BIT WISE OPERATION & BIT WISE OPERATORS:
    truth-table
    1. What are all decision control statements in C?
    In decision control statements (if-else and nested if), group of statements are executed when condition is true.  If condition is false, then else part statements are executed.
    There are 3 types of decision making control statements in C language. They are,
    Ø  if statements
    Ø    if else statements
    Ø    nested if statements
    “IF”, “ELSE” AND “NESTED IF” DECISION CONTROL STATEMENTS IN C:
    Syntax for each C decision control statements are given in below table with description.
    Decision control statements  
    Syntax/Description
    If Syntax:
    if (condition)
    { Statements; }
    Description:
    In these type of statements, if condition is true, then respective block of code is executed.
    if…else Syntax:
    if (condition)
    { Statement1; Statement2; }
    else
    { Statement3; Statement4; }
    Description:
    In these type of statements, group of statements are executed when condition is true.  If condition is false, then else part statements are executed.
    nested if Syntax:
    if (condition1){ Statement1; }
    else_if(condition2)
    { Statement2; }
    else Statement 3;Description:
    If condition 1 is false, then condition 2 is checked and statements are executed if it is true. If condition 2 also gets failure, then else part is executed.
    1. What are all loop control statements in C?
    Loop control statements in C are used to perform looping operations until the given condition is true. Control comes out of the loop statements once condition becomes false.
    TYPES OF LOOP CONTROL STATEMENTS IN C:
    There are 3 types of loop control statements in C language. They are,
    Ø  For
    Ø  While
    Ø  do-while
    Syntax for each C loop control statements are given in below table with description.
    For Syntax
    for (exp1; exp2; expr3)
    { statements; }
    Where,exp1 – variable initialization
    ( Example: i=0, j=2, k=3 )
    exp2 – condition checking
    ( Example: i>5, j<3, k=3 )
    exp3 – increment/decrement
    ( Example: ++i, j–, ++k )
    while (condition)
    { statements; }where,
    condition might be a>5, i<10
    do while      do { statements; }
    while (condition);where,
    condition might be a>5, i<10
    1. What is the difference between single equal “=” and double equal “==” operators in C?
    Assignment Operator (=)
    Ø  = is an Assignment Operator in C, C++ and other programming languages, It is Binary Operator which operates on two operands.
    Ø  = assigns the value of right side expression’s or variable’s value to the left side variable.
    Let's understand by example:
    Ø  x=(a+b);
    Ø  y=x;
    Here, When first expression evaluates value of (a+b) will be assigned into x and in second expression y=x; value of variable x will be assigned into y.
    Equal To Operator (==)
    Ø  == is an Equal To Operator in C and C++ only, It is Binary Operator which operates on two operands.
    Ø  == compares value of left and side expressions, return 1 if they are equal other will it will return 0.

    Let's understand by example:
    int x,y;
    x=10;
    y=10;
    if(x==y)
        printf("True");
    else
        printf("False");
    When expression x==y evaluates, it will return 1 (it means condition is TRUE) and "TRUE" will print.
    So it's cleared now, ,both are not same, = is an Assignment Operator it is used to assign the value of variable or expression, while == is an Equal to Operator and it is a relation operator used for comparison (to compare value of both left and right side operands).
    1. What is the difference between pre increment operator and post increment operator?
    Pre-increment operator: A pre-increment operator is used to increment the value of a variable before using it in a expression. In the Pre-Increment, value is first incremented and then used inside the expression.
    Syntax:  a = ++x;
    Post-increment operator: A post-increment operator is used to increment the value of variable after executing expression completely in which post increment is used. In the Post-Increment, value is first used in a expression and then incremented.
    Syntax: a = x++;

    28.                       What is the difference between pre decrement operator and post decrement operator?
    Pre-decrement operator: A pre- decrement operator is used to decrement the value of a variable before using it in a expression. In the Pre- decrement, value is first decremented and then used inside the expression.
    Syntax:  a = --x;
    Post- decrement operator: A post- decrement operator is used to decrement the value of variable after executing expression completely in which post decrement is used. In the Post- decrement, value is first used in a expression and then decremented.
    Syntax: a = x--;
    1. What is “&” and “*” operators in C?
    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.
    1. What is the purpose of main() function?
    The main function invokes other functions within it and the execution of a program always starts with the main() function.

No comments:

Post a Comment