C Language Interview Questions For Freshers

Introduction To C  C is a programming language developed by AT & T’s Bell Laboratories of USA in 1972. It was designed and written b...

Introduction To C 

C is a programming language developed by AT & T’s Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie. C is reliable, simple and easy to use.
                       C has survived for more than 3 decades. C language is a base to learn different programming language. If you want to learn C++ or JAVA, without the knowledge of C it becomes very difficult to learn these programming languages.
                                         Many major components of popular operating systems like Windows, UNIX, LINUX is still written in C. Nothing beats C in terms of speed of execution.

C Language Interview Questions For Freshers 

What is C language?
       Refer : Above Introduction

What are the types of constants in c?
             C constants can ba divided into two categories :
Primary constants
Secondary constants

What are the types of C intructions?
            There are basically three types of instructions in C :
Type Declaration Instruction
Arithmetic Instruction
Control Instruction

What is a pointer?
       Pointers are variables which stores the address of another variable. That variable may be a scalar .The pointed-to object may be part of a larger object, such as a field of a structure or an element in an array.

What is the difference between arrays and pointers?
     Pointers are used to manipulate data using the address. Pointers use dot operator to access the data pointed to by them.

            Arrays is a collection of similar datatype. Array use subscripted variables to access and manipulate data. Array variables can be Equivalently written using pointer expression.

Can we have a pointer to a pointer in C?
char *a;      -   Pointer to a character
char **a;      -    Pointer to a pointer

What is the difference between strings and character arrays?
                   A major difference is: string will have static storage duration, whereas as a character array will not, unless it is explicity specified by using the static keyword.
               Actually, a string is a character array with following properties:
1. The multibyte character sequence, to which we generally call string, is used to initialize an array of static storage duration. The size of this array is just sufficient to contain these characters plus the terminating NUL character.
2. It not specified what happens if this array, i.e., string, is modified.
3. Two strings of same value[1] may share same memory area.

What is the output of printf(“%d”) ?
                    When we write printf(“%d”,x); this means compiler will print the value of x. But as here, there is nothing after %d so compiler will show in output window garbage value.

How would you use the functions randomize() and random()?
                     Randomize() initiates random number generation with a random value.
Random() generates random number between 0 and n-1;

What is the difference between Call by Value and Call by Reference?
                     When using Call by Value, you are sending the value of a variable as parameter to a function, whereas Call by Reference sends the address of the variable. Also, under Call by Value, the value in the parameter is not affected by whatever operation that takes place, while in the case of Call by Reference, values can be affected by the process within the function.

How do you access the values within an array?
                    Arrays contain a number of elements, depending on the size you gave it during variable declaration. Each element is assigned a number from 0 to number of elements-1. To assign or retrieve the value of a particular element, refer to the element number. For example: if you have a declaration that says “intscores[5];”, then you have 5 accessible elements, namely: scores[0], scores[1], scores[2], scores[3] and scores[4].

What are preprocessor directives?
                           Preprocessor directives are placed at the beginning of every C program. This is where library files are specified, which would depend on what functions are to be used in the program. Another use of preprocessor directives is the declaration of constants.Preprocessor directives begin with the # symbol.

What is the difference between functions getch() and getche()?
                          Both functions will accept a character input value from the user. When using getch(), the key that was pressed will not appear on the screen, and is automatically captured and assigned to a variable. When using getche(), the key that was pressed by the user will appear on the screen, while at the same time being assigned to a variable.
What are recursive functions? What are the advantages and disadvantages of recursive algorithms?
                   A function that calls itself repeatedly, satisfying some condition, is called a Recursive Function. We can’t actually declare that recursive or non-recursive algorithms are good or bad. Some problems inherently are better suited for recursion.

What is a structure and a union in C?
                  A structure is an aggregate data type. It combines one or more base or aggregate data types into a package that may treated as a whole. A structure is like a record in other languages. A union combines two or more data types in the same area of storage. The contents of a union may be one data type at one time and another type at a different time. A union is sometimes called a trick- record.

What is the difference between the -> and . operators?
                      They both provide access to members of a structure or union. They differ in that -> is used when the variable is a pointer to a structure or union. The dot is used when the variable is itself the structure or union. The -> operator combines the pointer dereferencing operator with the member access operator; it is syntactic “sugar coating.”
address->city is equivalent to (*address).city.

What is friend function?
             The function declaration should be preceded by the keyword friend.The function definitions does not use either the keyword or the scope operator ::. The functions that are declared with the keyword friend as friend function.Thus, a friend function is an ordinary function or a member of another class.

What is the purpose of main() function?
                  The main() function in C is the most vital part of a program. The program execution occurs from the main() function. The main() function may contain any number of statements and they are sequentially executed. main() function can in turn call other functions.

Can the size of an array be declared at runtime?
                  No. In an array declaration, the size must be known at compile time. You can’t specify a size that’s known only at runtime. For example, if i is a variable, you can’t write code like this:

char array[i]; /* not valid C */

Some languages provide this latitude. C doesn’t. If it did, the stack would be more complicated, function calls would be more expensive, and programs would run a lot slower.

If you know that you have an array but you won’t know until runtime how big it will be, declare a pointer to it and usemalloc() or calloc() to allocate the array from the heap.

If you know at compile time how big an array is, you can declare its size at compile time. Even if the size is some complicated expression, as long as it can be evaluated at compile time, it can be used.

What is the difference between realloc() and free()?
                  The free subroutine frees a block of memory previously allocated by the malloc subroutine. Undefined results occur if the Pointer parameter is not a valid pointer. If the Pointer parameter is a null value, no action will occur. The realloc subroutine changes the size of the block of memory pointed to by the Pointer parameter to the number of bytes specified by the Size parameter and returns a new pointer to the block. The pointer specified by the Pointer parameter must have been created with the malloc, calloc, or realloc subroutines and not been deallocated with the free or realloc subroutines. Undefined results occur if the Pointer parameter is not a valid pointer.

What do you mean by inline function?
                    The idea behind inline functions is to insert the code of a called function at the point where the function is called. If done carefully, this can improve the application’s performance in exchange for increased compile time and possibly (but not always) an increase in the size of the generated binary executables.

What is a random access file?
                     A file can be accessed at random through function fseek(),
fseek(fp,position,origin); file pointer number file pointer fp positions number of bytes offset origin from 0, 1 or 2 denoting the starting, current or end position of the file respectively.

What are the types of files?
                   Files are of two types
1-high level files (stream oriented files) – These  are accessed through library functions
2-low level files (system oriented files) – These are accessed through system calls

Difference between logical vs syntax errors?
             Logical error:
It occurs by a wrong algorithm or a mistyped statement in such a way that it does not violate the syntax, and these are difficult to find.

Syntax error:

It involves syntax validation, and compiler prints diagnostic message.

Difference between array and pointer?
(i) Arrays allocate space, automatically
(ii) They can not be reassigned
(iii) They can not be resized
(iv) sizeof (arrayname) returns the number of bytes occupied by the array.


(i) Assigned explicitly to point an allocated space.
(ii) It can be reassigned
(iii) It can be resized using realloc() function
(iv) sizeof (p) gives the number of bytes used to store the pointer variable p.

What is pre-increment and post-increment?
                            ++num (pre-increment) increases value of num before it is used in the expression, where num++ (post-increment) increases the num after its value is used.

Difference between pass by reference and pass by value?
                          Pass by reference passes a pointer to the value. This allows the callee to modify the variable directly.Pass by value gives a copy of the value to the callee. This allows the callee to modify the value without modifying the variable. (In other words, the callee simply cannot modify the variable, since it lacks a reference to it.)

What is static identifier?
               A file-scope variable that is declared static is visible only to functions within that file. A
function-scope or block-scope variable that is declared as static is visible only within that scope. Furthermore, static variables only have a single instance. In the case of function- or block-scope variables, this means that the variable is not “automatic” and thus retains its value across function invocations.

Where is the auto variables stored?
                  Auto variables can be stored anywhere, so long as recursion works. Practically, they’re stored on the stack. It is not necessary that always a stack exist. You could theoretically allocate function invocation records from the heap.

Where does global, static, and local, register variables, free memory and C Program instructions get stored?
                 Global: Wherever the linker puts them. Typically the “BSS segment” on many platforms.
Static: Again, wherever the linker puts them. Often, they’re intermixed with the globals. The only difference between globals and statics is whether the linker will resolve the symbols across compilation units.Local: Typically on the stack, unless the variable gets register allocated and never spills.Register: Nowadays, these are equivalent to “Local” variables. They live on the stack unless they get register-allocated.

Difference between arrays and linked list?
                    An array is a repeated pattern of variables in contiguous storage. A linked list is a set of
structures scattered through memory, held together by pointers in each element that point to the next element. With an array, we can (on most architectures) move from one element to the next by adding a fixed constant to the integer value of the pointer. With a linked list, there is a “next” pointer in each structure which says what element comes next.

What are enumerations?
                     They are a list of named integer-valued constants. Example:enum color { black , orange=4,yellow, green, blue, violet };This declaration defines the symbols “black”, “orange”, “yellow”, etc. to have the values “1,” “4,” “5,” … etc. The difference between an enumeration and a macro is that the enum actually declares a type, and therefore can be type checked.

Describe about storage allocation and scope of global, extern, static, local and register variables?
                            Globals have application-scope. They’re available in any compilation unit that includes an appropriate declaration (usually brought from a header file). They’re stored wherever the linker puts them, usually a place called the “BSS segment.”
Extern? This is essentially “global.” Static: Stored the same place as globals, typically, but only available to the compilation unit that contains them. If they are block-scope global, only available within that block and its subblocks.Local: Stored on the stack, typically. Only available in that block and its subblocks.(Although pointers to locals can be passed to functions invoked from within a scope where that local is valid.)Register: See tirade above on “local” vs. “register.” The only difference is that
the C compiler will not let you take the address of something you’ve declared as “register.”

What are register variables? What are the advantages of using register variables?
                           If a variable is declared with a register storage class,it is known as register variable.Theregister variable is stored in the cpu register instead of main memory.Frequently used variables are declared as register variable as it’s access time is faster.

What is the use of typedef?
                      The typedef help in easier modification when the programs are ported to another machine.A descriptive new name given to the existing data type may be easier to understand the code.

Can we specify variable field width in a scanf() format string? If possible how?
                        All field widths are variable with scanf(). You can specify a maximum field width for a given field by placing an integer value between the ‘%’ and the field type specifier. (e.g. %64s). Such a specifier will still accept a narrower field width.The one exception is %#c (where # is an integer). This reads EXACTLY # characters, and it is theonly way to specify a fixed field width with scanf().

Post a Comment

Follow Us

Hot in week



Side Ads