C Interview Questions Answer For Fresher
1. What is C language?
The C programming language is a standardized programming language developed in the early
1970s by Ken Thompson and Dennis Ritchie for use on the UNIX operating system. It has
since spread to many other operating systems, and is one of the most widely used
programming languages. C is prized for its efficiency, and is the most popular programming
language for writing system software, though it is also used for writing applications.
2. What does static variable mean?
There are 3 main uses for the static.
1. If you declare within a function: It retains the value between function calls
2. If it is declared for a function name: By default function is extern..so it will be visible from
other files if the function declaration is as static..it is invisible for the outer files
3. Static for global variables: By default we can use the global variables from outside files If it
is static global..that variable is limited to with in the file.
3. What are the different storage classes in C?
C has three types of storage: automatic, static and allocated. Variable having block scope and
without static specifier have automatic storage duration.
Variables with block scope, and with static specifier have static scope. Global variables (i.e,
file scope) with or without the the static specifier also have static scope. Memory obtained
from calls to malloc(), alloc() or realloc() belongs to allocated storage class.
4. What is hashing?
To hash means to grind up, and that’s essentially what hashing is all about. The heart of a
hashing algorithm is a hash function that takes your nice, neat data and grinds it into some
randomlooking
integer.
The idea behind hashing is that some data either has no inherent ordering (such as images) or
is expensive to compare (such as images). If the data has no inherent ordering, you can’t
perform comparison searches.
5. Can static variables be declared in a header file?
You can’t declare a static variable without defining it as well (this is because the storage class
modifiers static and extern are mutually exclusive). A static variable can be defined in a header
file, but this would cause each source file that included the header file to have its own private
copy of the variable, which is probably not what was intended.
6. Can a variable be both constant and volatile?
Yes. The const modifier means that this code cannot change the value of the variable, but that
does not mean that the value cannot be changed by means outside this code.
The function itself did not change the value of the timer, so it was declared const. However, the
value was changed by hardware on the computer, so it was declared volatile. If a variable is
both const and volatile, the two modifiers can appear in either order.
7. Can include files be nested?
Yes. Include files can be nested any number of times. As long as you use precautionary
measures, you can avoid including the same file twice. In the past, nesting header files was
seen as bad programming practice, because it complicates the dependency tracking function of
the MAKE program and thus slows down compilation. Many of today’s popular compilers make
up for this difficulty by implementing a concept called precompiled headers, in which all
headers and associated dependencies are stored in a precompiled state.
8. What is a null pointer?
There are times when it’s necessary to have a pointer that doesn’t point to anything. The macro
NULL, defined in , has a value that’s guaranteed to be different from any valid pointer. NULL is
a literal zero, possibly cast to void* or char*.
Some people, notably C++ programmers, prefer to use 0 rather than NULL.
The null pointer is used in three ways:
1) To stop indirection in a recursive data structure.
2) As an error value.
3) As a sentinel value.
9. 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.
10. What is the difference between calloc() and malloc() ?
calloc(...) allocates a block of memory for an array of elements of a certain size. By default the
block is initialized to 0. The total number of memory allocated will be (number_of_elements *
size).
malloc(...) takes in only a single argument which is the memory required in bytes. malloc(...)
allocated bytes of memory and not blocks of memory like calloc(...).
malloc(...) allocates memory blocks and returns a void pointer to the allocated space, or NULL
if there is insufficient memory available.
calloc(...) allocates an array in memory with elements initialized to 0 and returns a pointer to the
allocated space. calloc(...) calls malloc(...) in order to use the C++ _set_new_mode function to
set the new handler mode.
11. What is the difference between printf() and sprintf() ?
sprintf() writes data to the character array whereas printf(...) writes data to the standard output
device.
12. How to reduce a final size of executable?
Size of the final executable can be reduced using dynamic linking for libraries.
13. Can you tell me how to check whether a linked list is circular?
Create two pointers, and set both to the start of the list. Update each as follows:
while (pointer1) {
pointer1 = pointer1>
next;
pointer2 = pointer2>
next;
if (pointer2) pointer2=pointer2>
next;
if (pointer1 == pointer2) {
print ("circular");
}}
If a list is circular, at some point pointer2 will wrap around and be either at the item just before
pointer1, or the item before that. Either way, its either 1 or 2 jumps until they meet.
15. 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:
* 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.
* it not specified what happens if this array, i.e., string, is modified.
* Two strings of same value[1] may share same memory area.
16. Write down the equivalent pointer expression for referring the same element a[i][j][k]
[l] ?
a[i] == *(a+i)
a[i][j] == *(*(a+i)+j)
a[i][j][k] == *(*(*(a+i)+j)+k)
a[i][j][k][l] == *(*(*(*(a+i)+j)+k)+l)
17. Which bit wise operator is suitable for checking whether a particular bit is on or off?
The bitwise AND operator. Here is an example:
enum {
KBit0 = 1,
KBit1,
…K
Bit31,
};
if ( some_int & KBit24 )
printf ( “Bit number 24 is ON\n” );
else
printf ( “Bit number 24 is OFF\n” );
18. What is dangling pointer in c?
If any pointer is pointing the memory address of any variable but after some variable has
deleted from that memory location while pointer is still pointing such memory location. Such
pointer is known as dangling pointer and this problem is known as dangling pointer problem.
19. What are merits and demerits of array in c?
Merits:
(a) We can easily access each element of array.
(b) Not necessity to declare too many variables.
(c) Array elements are stored in continuous memory location.
Demerits:
(a) Wastage of memory space. We cannot change size of array at the run time.
(b) It can store only similar type of data
20. Where are the auto variables stored?
Auto variables are stored in main memory and their default value is a garbage value.
21. Why Preincrement operator is faster than Postincrement?
Evaluation of any expression is from left to right. Preincrement is faster because it doesn't
need to save the current value for next instruction whereas Postincrement needs to saves
current value to be incremented after execution of current instruction.
22. Difference between arrays and linked list?
Major differences between arrays and linked lists are: (i) In array consecutive elements are
stored in consecutive memory locations whereas in linked list it not so. (ii) In array address of
next element is consecutive and whereas in linked list it is specified in the address part of each
node.(iii) Linked List makes better use of memory than arrays.(iv) Insertion or deletion of an
element in array is difficult than insertion or deletion in linked list
23. What is the use of typedef?
(i)It increases the portability.
(ii) It simplify the complex declaration and improve readability of the program.
24. What are library Functions?
Library Functions are predefined functions and stored in .lib files.
25. What is a structure?
Structure is a collection of heterogeneous (i.e. related data items which can be of different
types) held together to a single unit. The data items enclosed within a structure are called its
members which may be of data type int, float, char, array etc.
26. What is a pointer?
Pointer is a variable that contains address of another variable in the memory. Pointers are quite
useful in creation of linked data structures (such as linked lst, trees graphs), managing object
allocated memory dynamically, optimize the program to execute faster and use less memory.
27. What are the techniques you use for debugging?
(i)Using compiler’s features
(ii)Read The Fine Module
(iii)printf( ) debugging
(iv)Code grinding
(v)Assertion
28. What are macros? What are its advantages and disadvantages?
Macro is a Preprocessor.
Major advantage of using the macro is to increase the speed of the
execution of the program.
Major disadvantage of the macros are:
(i) No type checking is performed in macro. This may cause error.
(ii) A macro call may cause unexpected results.
29. What is difference between Structure and Unions?
(i) In structure every member has its own memory whereas in union its members share the
same member space.
(ii) In structure, it is possible to initialize all the members at the same time which is not
possible in case of union.
(iii) A structure requires more space than union(for the same type of members).
(iv) In union different interpretations of the same memory space are possible which is not so in
case of structures.
30. What are the advantages of using Unions?
(i) Efficient use of memory as it it does not demand memory space for its all members rather it
require memory space for its largest member only.
(ii) Same memory space can be interpreted differently for different members of the union.
31. What is the difference between ordinary variable and pointer in C?
An ordinary variable is like a container it can hold any value and we can change the value of
ordinary variable at a time throughout the program .A pointer is a variable that stores the
address of another Variable.
32. What are segment and offset addresses?
When paging technique is performed, the page will breaks into segments and its sequence is
said to be segments and its width can be said as offset. In short,segment is a physical address
and offset is logical address.
34. What is the difference between %d and %*d in c language?
%d give the original value of the variable and %*d give the address of the variable.
eg:int
a=10,b=20;
printf("%d%d",a,b);
printf("%*d%*d",a,b);
Result is 10 20 1775 1775 .Here 1775 is the starting address of the memory allocation for the
integer.a and b having same address because of contagious memory allocation.
35. How does a C program come to know about command line arguments?
When we execute our C program, operating system loads the program into memory. In case of
DOS, it first loads 256 bytes into memory, called program segment prefix. This contains file
tables,environment segment, and command line information. When we compile the C program
the compiler inserts additional code that parses the command, assigning it to the argv array,
making the arguments easily accessible within our C program.
36. How are pointer variables initialized?
Pointer variable are initialized by one of the following two ways
Static
memory allocation
Dynamic
memory allocation
37. What is modular programming?
If a program is large, it is subdivided into a number of smaller
programs that are called modules or subprograms. If a complex
problem is solved using more modules, this approach is known as
modular programming
38. Where does global, static, local, register variables and C Program instructions get
stored?
Global , static, local : In main memory
Register variable: In registers
C program : In main memory.
39. Where are the auto variables stored?
Auto variables are stored in main memory and their default value is a garbage value.
40. What is an lvalue?
An lvalue is an expression to which a value can be assigned. The lvalue
expression is located on the left side of an assignment statement,
whereas an rvalue is located on the right side of an assignment
statement. Each assignment statement must have an lvalue and an
rvalue. The lvalue expression must reference a storable variable in
memory. It cannot be a constant
41. What is an argument? Differentiate between formal arguments and
actual arguments?
An argument is an entity used to pass the data from calling function to
the called function. Formal arguments are the arguments available in
the function definition. They are preceded by their own data types.
Actual arguments are available in the function call.
42. When is a switch statement better than multiple if statements?
A switch statement is generally best to use when you have more than two conditional
expressions based on a single variable of numeric type.
43. Differentiate between a linker and linkage?
A linker converts an object code into an executable code by linking together the necessary
build in functions. The form and place of declaration where the variable is declared in a program
determine the
linkage of variable.
44. Define Operator, Operand, and Expression in 'C'?
Operators are symbols which take one or more operands or expressions and perform arithmetic
or logical computations.
Operands are variables or expressions which are used in operators to evaluate the expression.
Combination of operands and operators form an expression.
45. What will be the result of the following code?
#define TRUE 0 // some code
while(TRUE)
// some code
}
Answer: This will not go into the loop as TRUE is defined as 0.
46. What will be printed as the result of the operation below:
main()
{
int a=0;
if(a==0)
printf(“Cisco Systemsn”);
printf(“Cisco Systemsn”);
}
Answer: Two lines with “Cisco Systems” will be printed.
47. Do you know pragma directives in c?
Pragma is implementation specific directive i.e each pragma directive has different
implementation rule and use. If compiler does not recognize particular pragma it simply ignore
that pragma statement without showing any error or warning message and execute the whole
program assuming this pragma statement is not present.
48. Predict the output or error
main()
{
clrscr();
}
clrscr();
Ans:No output/error
Explanation:The first clrscr() occurs inside a function. So it becomes a function call. In the
second clrscr(); is a function declaration (because it is not inside any function).
49. Predict the output or error
enum colors {BLACK,BLUE,GREEN}
main()
{
printf("%d..%d..%d",BLACK,BLUE,GREEN);
return(1);
}
Answer: 0..1..2
Explanation: enum assigns numbers starting from 0, if not explicitly defined.
50. Predict the output or error
main()
{i
nt i;
printf("%d",scanf("%d",&i)); // value 10 is given as input here
}
Answer:1
Explanation: Scanf returns number of items successfully read and not 1/0. Here 10 is given as
input which should have been scanned successfully. So number of items read is 1.
51. what will be the position of the file marker?
a: fseek(ptr,0,SEEK_SET);
b: fseek(ptr,0,SEEK_CUR);
Ans: a: The SEEK_SET sets the file position marker to the starting of the file.
b: The SEEK_CUR sets the file position marker to the current position
of the file.
52. Predict the output or error
main()
{
main();
}
Ans: Runtime error : Stack overflow.
Explanation: main function calls itself again and again. Each time the function is called its
return address is stored in the call stack. Since there is no condition to terminate the function
call, the call stack overflows at runtime. So it terminates the program and results in an error.
53. Predict the output or error
main()
{
int i=5,j=6,z;
printf("%d",i+++j);
}
Answer:11
Explanation:the expression i+++j is treated as (i++ + j)
54. Predict the output or error
main()
{
int k=1;
printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE");
}
Ans: 1==1 is TRUE
Explanation: When two strings are placed together (or separated by whitespace)
they are
concatenated (this is called as "stringization" operation). So the string is as if it is given as
"%d==1 is %s". The conditional operator( ?: ) evaluates to "TRUE".
55. What is use of void data type?
Void is an empty data type normally used as a return type in C/C++, C#, Java
functions/methods to declare that no value will be return by the function.
The another used of void is to declare the pointer in C/C++ where It is not sure what data type
is addressed by the pointer.
56. four type of scope in c:
Block scope.
Function scope.
File scope.
Program scope.
57. Tell any five properties of auto variables?
auto variables are defined inside a function. A variable declared inside the function without
storage class name is, by default, an auto variable. These functions are declared on the stack.
The stack provides temporary storage.
58. What are differences between sizeof operator and strlen function?
sizeof is keyword of c which can find size of a string constant including null character but strlen
is function which has been defined string.h and can find number of characters in a string
excluding null character.
59. What is command line argument?
Getting the arguments from command prompt in c is known as command line arguments. In c
main function has three arguments.
They are:
Argument counter
Argument vector
Environment vector
60. void main(){
int x=5,y=10,z=15,val;
val=sum(x,(y=0,z=0,y),z);
clrscr();
printf("%d",val);
getch();
}
sum(int x,int y,int z){
return x+y+z;
}
Answer:20
Explanation: In the above program comma after Y=0 &Z=0 are behaving as operator.
61. what is nested structure?
A structure is a collection of one or more variables, possibly of different data types, grouped
together under a single name for convenient handling. Structures can contain other structures
as members; in other words, structures can nest.
62. What is slack byte in structure?
To store any type of data in structure there is minimum fixed byte which must be reserved by
memory. This minimum byte is known as word boundary. Word boundary depends upon
machine. TURBO C is based on 8086 microprocessor which has two byte word boundary. So
any data type reserves at least two byte space.
63.What is prototype of printf function?
Prototype of printf function is:
int printf( const char *format ,…)
64.What is difference between declaration and definition?
During declaration we just specify the type and no memory is allocated to the variable. But
during the
definition an initial value is assigned and memory is allocated to the variable.
65. What is function recursion?
When a function of body calls the same function then it is called as 'recursive function.'
Example:
Recursion()
{
printf("Recursion !");
Recursion();
}
66. What is self referential structure ?
A selfreferential
structure is one of the data structures which refer to the pointer to (points) to
another structure of the same type.
67. What is far pointer?
The pointer which can point or access whole the residence memory of RAM i.e. which can
access all 16 segments is known as far pointer.
68. What is pascal and cdecl keyword in c language?
There are two types of parameters passing conventions in c:
1. pascal: In this style function name should (not necessary ) in the uppercase .First parameter
of function call is passed to the first parameter of function definition and so on.
2. cdecl: In this style function name can be both in the upper case or lower case. First
parameter of function call is passed to the last parameter of function definition. It is default
parameter passing convention.
69. What is use of #pragma inline directive in c language?
#pragma inline only tells the compiler that source code of program contain inline assembly
language code .In c we can write assembly language program with help of asm keyword.
70. What is the meaning of multilevel pointers in c?
A pointer is pointer to another pointer which can be pointer to others pointers and so on is
known as multilevel pointers. We can have any level of pointers.
71. What is huge pointer in c?
The pointer which can point or access whole the residence memory of RAM i.e. which can
access all the 16 segments is known as huge pointer.
72. Is it possible to rename any function in c?
Yes, we can rename any function using typedef keyword. It is useful when function declaration
is too complex and we have to give any simple name or if we have to create more numbers of
function of the same type.
73. Do you know, what is the meaning and use of static keyword in c?
Keyword static is used for declaring static variables in c. This modifier is used with all data
types like int, float, double, array, pointer, structure, function etc.
74. What is difference between .com program and .exe program?
Both .com and .exe program are executable program but .com program execute faster than
.exe program. All drivers are .com program. .com file has higher preference than .exe For
example:
75. Difference between TSR and TSO program
TSO means terminate but stay outside. It is that program, which release the main memory after
the execution of the program. Example ms paint, notepad, turbo c compilers etc.
TSR means terminate but stay residence .It is those program, which after the execution of the
program does not release the RAM (main memory).e.g. antivirus.
76. Describe turbo c compiler?
Turbo c is an IDE of c programming language created by Borland. Turbo C 3.0 is based on MS
DOS operation system. It is one of the most popular c compilers. It uses 8086 microprocessor
which is 16 bit microprocessor. It has 20 address buses and 16 data bus. Its word length is two
byte.
77. Out of fgets() and gets() which function is safe to use and why?
fgets() is safer than gets(), because we can specify a maximum input length. Neither one is
completely safe, because the compiler can’t prove that programmer won’t overflow the buffer he
pass to fgets ().
78. Difference between strdup and strcpy?
Both copy a string. strcpy wants a buffer to copy into. strdup allocates a buffer using malloc().
Unlike strcpy(), strdup() is not specified by ANSI .
79. Differentiate between a for loop and a while loop? What are it uses?
For executing a set of statements fixed number of times we use for loop while when the number
of
iterations to be performed is not known in advance we use while loop.
80. What is storage class? What are the different storage classes in C?
Storage class is an attribute that changes the behavior of a variable. It controls the lifetime,
scope and linkage. The storage classes in c are auto, register, and extern, static, typedef.
81. What are the uses of a pointer?
(i)It is used to access array elements
(ii)It is used for dynamic memory allocation.
(iii)It is used in Call by reference
(iv)It is used in data structures like trees, graph, linked list etc.
82. In header files whether functions are declared or defined?
Functions are declared within header file. That is function prototypes exist in a header file,not
function bodies. They are defined in library (lib).
83. 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.)
84. Are pointers integer?
No, pointers are not integers. A pointer is an address. It is a positive number.
81. What is static memory allocation?
Compiler allocates memory space for a declared variable. By using the address of operator, the
reserved address is obtained and this address is assigned to a pointer variable. This way of
assigning pointer value to a pointer variable at compilation time is known as static memory
allocation.
82. What is dynamic memory allocation?
A dynamic memory allocation uses functions such as malloc() or calloc() to get memory
dynamically. If these functions are used to get memory dynamically and the values returned by
these function are assigned to pointer variables, such a way of allocating memory at run time is
known as dynamic memory allocation.
83. What modular programming?
If a program is large, it is subdivided into a number of smaller programs that are called modules
or subprograms. If a complex problem is solved using more modules, this approach is known
as modular programming
84. What is a function?
A large program is subdivided into a number of smaller programs or subprograms. Each
subprogram specifies one or more actions to be performed for the larger program. Such sub
programs are called functions.
85. what are C tokens?
There are six classes of tokens: identifier, keywords, constants, string literals, operators and
other separators.
86. What are C identifiers?
These are names given to various programming element such as variables, function, arrays.It
is a combination of letter, digit and underscore.It should begin with letter. Backspace is not
allowed.
87. Difference between syntax vs logical error?
Syntax Error
These involves validation of syntax of language.
compiler prints diagnostic message.
Logical Error
logical error are caused by an incorrect algorithm or by a statement mistyped in such a way
that it doesn’t violet syntax of language.
difficult to find.
88. What are the facilities provided by preprocessor?
file inclusion
substitution facility
conditional compilation
89.What do the functions atoi(), itoa() and gcvt() do?
atoi() is a macro that converts integer to character.
itoa() It converts an integer to string
gcvt() It converts a floating point number to string
90. What is FILE?
FILE is a predefined data type. It is defined in stdio.h file.
91. What is a file?
A file is a region of storage in hard disks or in auxiliary storage devices.It contains bytes of
information .It is not a data type.
No comments:
Post a Comment