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.
#include <stdio.h>
int t = 10;
main(){
int x = 0;
void funct1();
funct1();
printf(“After first call n”);
funct1();
printf(“After second call n”);
funct1();
printf(“After third call n”);
}
void funct1()
{
static int y = 0;
int z = 10;
printf(“value of y %d z %d”,y,z);
y=y+10;
}
value of y 0 z 10 After first call
value of y 10 z 10 After second call
value of y 20 z 10 After third call
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.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.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.18. Which bit wise operator is suitable for turning off a particular bit in a number?The bitwise AND operator, again. In the following code snippet, the bit number 24 is reset to zero.
some_int = some_int & ~KBit24;
19. Which bit wise operator is suitable for putting on a particular bit in a number?The bitwise OR operator. In the following code snippet, the bit number 24 is turned ON:
some_int = some_int | KBit24;
20. Does there exist any other function which can be used to convert an integer or a float to a string?Some implementations provide a nonstandard function called itoa(), which converts an integer to string.
#include
char *itoa(int value, char *string, int radix);
DESCRIPTION
The itoa() function constructs a string representation of an integer.
PARAMETERS
value: Is the integer to be converted to string representation.
string: Points to the buffer that is to hold resulting string.
The resulting string may be as long as seventeen bytes.
radix: Is the base of the number; must be in the range 2 – 36.
A portable solution exists. One can use sprintf():
char s[SOME_CONST];
int i = 10;
float f = 10.20;
sprintf ( s, “%d %fn”, i, f );22. Difference between const char* p and char const* pIn const char* p, the character pointed by ‘p’ is constant, so u cant change the value of character pointed by p but u can make ‘p’ refer to some other location.
In char const* p, the ptr ‘p’ is constant not the character referenced by it, so u cant make ‘p’ to reference to any other location but u can change the value of the char pointed by ‘p’.29. When does the compiler not implicitly generate the address of the first element of an array?Whenever an array name appears in an expression such as
· array as an operand of the size of operator
· array as an operand of & operator
· array as a string literal initializer for a character array
Then the compiler does not implicitly generate the address of the address of the first element of an array.
30. Why n++ executes faster than n+1 ?The expression n++ requires a single machine instruction such as INR to carry out the increment operation whereas, n+1 requires more instructions to carry out this operation.
31. Why doesn’t the following statement work?char str[ ] = “Hello” ;
strcat ( str, ‘!’ ) ;
Answer: The string function strcat( ) concatenates strings and not a character. The basic difference between a string and a character is that a string is a collection of characters, represented by an array of characters whereas a character is a single character. To make the above statement work writes the statement as shown below:
strcat ( str, “!” ) ;33. What is the purpose of main( ) function ?The function main( ) invokes other functions within it.It is the first function to be called when the program starts execution.
· It is the starting function
· It returns an int value to the environment that called the program
· Recursive call is allowed for main( ) also.
· It is a user-defined function
· Program execution ends when the closing brace of the function main( ) is reached.
· It has two arguments 1)argument count and 2) argument vector (represents strings passed).
· Any user-defined name can also be used as parameters for main( ) instead of argc and argv
34. How can I search for data in a linked list?Unfortunately, the only way to search a linked list is with a linear search, because the only way a linked list’s members can be accessed is sequentially.
Sometimes it is quicker to take the data from a linked list and store it in a different data structure so that searches can be more efficient.
35. Why should we assign NULL to the elements (pointer) after freeing them?This is paranoia based on long experience. After a pointer has been freed, you can no longer use the pointed-to data. The pointer is said to dangle; it doesn’t point at anything useful.
If you NULL out or zero out a pointer immediately after freeing it, your program can no longer get in trouble by using that pointer. True, you might go indirect on the null pointer instead, but that’s something your debugger might be able to help you with immediately.
Also, there still might be copies of the pointer that refer to the memory that has been deallocated; that’s the nature of C. Zeroing out pointers after freeing them won’t solve all problems.85. 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.
86. 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
87. Where are the auto variables stored?Auto variables are stored in main memory and their default value is a garbage value.
88. 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.
89. 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
90. What is the use of typedef?(i)It increases the portability.
(ii) It simplify the complex declaration and improve readability of the program.
91. What are library Functions?Library Functions are predefined functions and stored in .lib files.
92. 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.
93. 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.
94. 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)Assertion96. 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.
97. 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.
98. 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.
99. 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.
100. When should a type cast be used?There are two situations in which to use a type cast. The first use is to change the type of an operand to an arithmetic operation so that the operation will be performed properly.
The second case is to cast pointer types to and from void * in order to interface with functions that expect or return void pointers. For example, the following line type casts the return value of the call to malloc() to be a pointer to a foo structure.
struct foo *p = (struct foo *) malloc(sizeof(struct foo));
101. 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.
102. 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.
103. How are pointer variables initialized? Pointer variable are initialized by one of the following two ways
– Static memory allocation
– Dynamic memory allocation
104. 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
105. 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.
106. Where are the auto variables stored?Auto variables are stored in main memory and their default value is a garbage value.
107. 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
108. 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.
109. 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.
110. 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.
111. 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.129. 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.
130. 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.
131.What is prototype of printf function? Prototype of printf function is:
int printf( const char *format ,…)
132.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.
133. 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();
}144. 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.
145. 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 ().
146. 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 .
147. 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.
148. 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.
149. 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.
150.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).
151. 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.)
152. 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.
153. Are pointers integer?No, pointers are not integers. A pointer is an address. It is a positive number.
154. 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.
155. 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.
156. 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
157. 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.
158. Difference between formal argument and actual argument?Formal arguments are the arguments available in the function definition. They are preceded by their own data type. Actual arguments are available in the function call. These arguments are given as constants or variables or expressions to pass the values to the function.
159. what are C tokens?There are six classes of tokens: identifier, keywords, constants, string literals, operators and other separators.
160. 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.
161. 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.
162. What are the facilities provided by preprocessor?file inclusion
substitution facility
conditional compilation
163.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
164. What is FILE?FILE is a predefined data type. It is defined in stdio.h file.
165. 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.
viva & exam questions for C
viva & exam questions for C