Total Pageviews

Friday, August 15, 2008

Basics on C

Important 'C' Basic Questions


C QUESTIONS:

1. What are the languages from which C was evolved?

ALGOL, BCPL (Basic Combined Programming Language) & B.

2. Who has developed C? When & where has it been developed?

C has been developed by Dennis Ritchie at Bell Laboratories in 1972.

3. What is the other name for C?

K&R C (Kernighan & Ritchie)

4. What is C89?

The version of C which is approved by ANSI (American National Standards Institute) in December 1989 & then approved by International Standards Organization) in 1990 is known as C89.

5. What is C99?

The 1999 version of C which has got few features of C++ & Java is called C99.

6. What are C99 features?

New Keywords: bool, complex, imaginary, inline, restrict

New comment: Single line comment (//)

New Data Types: Bool, Complex, Imaginary

Declaration of Variables: variables can be declared at any point just before its use unlike C

Changes to I/O formats: ll (long long) format to scanf () & printf ()

Handling of Arrays: Variables length arrays, type specification in array declaration, flexible arrays in structures

Functions implementation: Default to Int rule, Explicit function declaration, Restriction on return statement, inline functions

Restricted pointers

Changes to compiler limitations

7. What are restricted pointers in C99?

A pointer qualified with restrict is called as restricted pointer. A pointer declared “restricted” is the only way through which we access the object it points to.

Ex.: int *restrict a;

8. What are variable length arrays in C99?

Variable length arrays are the one in which size of array can be integer variable or any valid integer expression & the value of which can be specified just before it is used.

9. What are the features of C?

C is a robust, high level, structured programming language which is highly portable, efficient and fast, and has the ability to extend itself.

10. How do you explain that C is highly portable?

The feature that a C program written for one computer can be run on another computer with little or no modifications makes it highly portable.

11. Where does the program execution begin in C?

Program execution begins at main () in C.

12. How many main () functions can be used in C?

One.

13. How do use comments in C?

/*….*/ are the comments used in C. Anything enclosed between them are considered as comment lines.

14. Can you nest the comments in C?

No.

15. What comments are used for?

· Documentation of variables and their usage

· Explaining difficult sections of code

· Describes the program, author, date, modification changes, revisions etc

· Copyrighting.

16. What is the statement terminator in C?

Semicolon (;).

17. Is the C language case sensitive or not?

C language is case sensitive & the programs are written in lower case.

18. What do you mean by predefined?

The function that has already been written, compiled, and linked together with a program at the time of linking is called predefined.

19. What does the keyword “void” indicate?///

Void indicates that the function does not return any value.

20. Explain the structure of a C program?

Documentation section, Link section, Definition section, Global Declaration section, main (), subprogram section. Except the main (), all the other sections are optional.

21. How do you execute a C program in MS-DOS?

There are 3 steps involved in executing a C program: Compiling, Linking and executing.

Compiling is done by the command MSC which creates an object file called . Linking is done by the command LINK which creates a file . To execute it enter and the results are displayed.

Basics:

22. What do you mean by trigraph sequences?

Trigraph sequences help us to enter certain characters that are not available on some keyboards.

Ex.: ??/ --- represents ^ (caret)

Each sequence consists of 3 characters, 2 question marks followed by 1 character.

23. What are tokens in C?

Smallest individual units are called tokens.

24. What are the types of tokens in C?

Keywords, Identifiers, Constants, Strings, Special symbols, Operators.

25. What do you mean keyword?

Keyword has a fixed meaning which cannot be changed.

26. How many keywords are there in C?

Thirty two (32).

27. What is single character constant?

A single character enclosed within a pair of single quotes is called single character constant.

Ex.: ‘A’

28. What is string constant?

Sequence of characters enclosed in double quotes.

29. What are escape sequences?

The combination of backslash(\) and some character which together represent one character are called escape sequences.

Ex.: ‘\n’ represents newline character.

30. What do you mean by initialization of variable?

Initialization is the process of assigning a value to variable at the time of declaration.
31. What are the fundamental data types?

The fundamental data types are integer (int), character (char), floating point (float), double precision floating point (double) and void.
32. What is the range of integer?

-32,768 to 32,767.

33. What do you mean by type definition?

Type definition is used to define an identifier that represents an existing data type, which later can be used to declare variables.

Ex.: typedef int rank;

Rank rank1, rank2;

34. Give the syntax of enum with an example?

Enum identifier {value1, value2, value3}

Ex.: enum color {violet, red, blue}

35. What is the other name for global variable?

External variable.

36. What is the use of storage classes?

Storage classes are used to provide the details of location and the visibility of the variables.

37. What are the types of storage classes?

Auto, static, extern, register are the types of storage classes.

38. What are the values stored in storage classes when they are declared?

Static and extern are automatically initialized to zero when declared, while the others contain garbage values unless they are initialized.

39. What do you mean by variable and “value” of a variable?

Variable is a data name used to store a data value and is volatile. “Value” is the data stored in or assigned to a variable.

40. What is the use of control string in scanf ()?

Control string contains the format of data taken as input.

41. What does ampersand (&) represent in scanf ()?

The ampersand (&) represents address of the variable name in scanf ().

42. What is the other name for symbolic names?
Symbolic names are also called as “constant identifiers”.

43. What is the use of the keyword “const”?
Const is a keyword using which when a variable is declared, the value of it cannot be changed during the program execution.

44. What is the use of keyword “volatile”?
Volatile keyword is used to inform compiler that the variable can be modified by the external sources.

45. What is the operator for exponentiation in C?
C does not have any operator for exponentiation.

46. What do you mean by integer expression?
The arithmetic expression in which all the operands are integers is called integer expression.

47. Which operator cannot be used with real operands?
Modulus operator (%).

48. Give an example for shorthand assignment operation?
a=a+3 can be written as a+=3;

49. What are the unary operators?
Increment (++) and decrement (++) are the unary operators.

50. Explain is the difference between prefix and postfix increment operator?
Prefix operator first increments the value and then assigns the value to the variable whereas postfix operator first assigns the value and then increments it.

51. What is the use of bitwise operators?
Manipulation of data at bit level is done using bitwise operators.

52. What are the ternary operators?
The pair “? :” are the ternary operators.

53. Give an example of comma operator and how is it evaluated?
Ex.: c= (a=3, b=2, a*b);
It is evaluated from left to right.

54. What is the use of “sizeof” operator?
Sizeof operator returns the number of bytes a variable occupies.

55. What is implicit type conversion?
The automatic conversion of ‘lower’ data type to ‘higher’ data type to maintain the significance of data is called implicit type conversion.

56. What do you mean by operator precedence?
Operator precedence is used to determine the order in which different operators in an expression are evaluated.

57. What do you mean by associativity of an operator?
The evaluation of operators from ‘left to right’ or ‘right to left’ which are of the same precedence based on their levels is called associativity.

58. Which operator has the lowest precedence?
Comma operator (,).

59. What does “stdio.h” stand for?
Standard input-output header file.

60. Which function is used to read a single character as input?
getchar();

61. What is the difference between the operators ‘=’ and ‘==”?
Equal to ‘=’ is an assignment operator i.e., the value on the right side of ‘=’ is stored in the variable on right side. Whereas ‘==’ is comparison operator i.e., it is used to compare 2 values whether they are equal or not.

62. What is the use of fflush ()?
It is used to flush out unwanted characters from the buffer.

63. In which header file are the getchar () and putchar () functions present?
#include

64. What is the other name for control string?
Format string.

65. How do you skip an input filed?
The input field may be skipped by using “*” in the place of field width.
Ex.: scanf (“%d %*d”, &a, &b) --- 10 11
à 11 is skipped.

66. What does %[characters] indicate?
It is a conversion specification which indicates that only the characters in brackets are accepted in the input string.

67. What does % [^characters] indicate?
It indicates that except the characters in the bracket, all the other characters are accepted in the input string.

68. What does the scanf () function return after reading?
Scanf () returns the count of number of values successfully read after reading.

69. What is the use of output format flags “-“ & “0”?
As the printf () forces the output to be right justified, we can use “-“ in the control string to left justify the output, and “0” for leading zeros in the output.

Control structures:

70. List the decision-making statements.
If, Switch, Conditional operator statement and Goto are decision-making statements.

71. What is the use of decision-making statements?
Decision-making statements are to used to control the flow of execution of statements.

72. List the different forms of “If” statement.
The different forms of “If” statements are simple if statement, if...else statement, Nested if…else statement, and else if ladder.

73. What is the structure of If…else statement?
If (condition)
{
statements;
}
else
{
statements;
}

74. What is the structure of nested if…else?
If (condition)
{
if (condition)
{
statements;
}
else
{
statements;
}
else
{
statements ;
}

75. Name the multiway decision statement.
Switch is the multiway decision statement.

76. Give the structure of switch statement.
switch (expression)
{
case 1:
statements;
break;
case 2:
statements;
break;
..............
..............
default:
statements;
break;
}

77. Where should be the label default placed in switch statement?
It can be placed anywhere in the switch statement, in the beginning, middle, or at the end. If all the matches fail, the statement sequences of the default get executed.

78. How many levels of nesting of switch statement is permitted in ANSI C?
15 levels.

79. What is ternary operator? What is its use?
“?:” is called ternary operator or conditional operator. It can be used instead of If statement.

80. What is the other name for entry controlled and exit controlled loops?
Entry controlled loop is also called pre-test loop and exit controlled loop is called post-test loop.

81. Name the entry controlled loops?
While and for are entry controlled loops.

82. Name the exit controlled loop/s?
Do…while

83. What are the steps in looping procedure?
Initialization, execution of steps in loop, test condition and updating control variable.

84. Name the types of loops.
While, Do and For.

85. Name the types of loops based on the type of counter variable and its value.
Based on the type of counter variable and its value loops are divided into types.
a. Counter controlled loops -- value of counter variable is known prior to loop execution
b. Sentinel controlled loops -- value of counter variable is not known prior to loop execution

86. Write the structure of while loop.
while (condition)
{
loop body;
}

87. Write the structure of Do…while.
do
{
loop body;
} while (condition);

88. What is the difference between while and do…while?
While” loop gets executed only when the condition is satisfied whereas “Do” loop gets executed at least once irrespective of the condition.

89. Why is do…while called exit controlled loop?
The test condition is evaluated at the end of the loop which controls the loop execution, so do…while is called exit controlled loop.

90. Why are while and for called entry controlled loops?
In the while and for loops the condition is checked at the beginning based on which the body of the loop is executed. So, they are called entry controlled loops.

91. Write the structure of for loop?
for ( initialization; condition; updating control variable)
{
loop body;
}

92. By which operator are the multiple arguments in the increment section of for loop are separated?
comma (,).

93. How can you setup time delay for loops?
We can setup time delay for loops by placing null statement at the end of for loop.
Ex.: for (i=1;i<=500;i++)
;

94. What is the use of break statement in loops?
Break statement is used to exit the loop and take the control to the statement following the loop.

95. How many loops can a break exit at a time?
One loop.

96. What is the difference between break and goto?
Break statement is used to transfer the control out of the loop to the next statement following the loop whereas Goto is used to transfer the control to anyplace in the program.

97. What is the use of continue statement in loops?
Continue statement is used to transfer the control to the beginning of the loop i.e., to the test condition in the case of while and do…while loop and to the increment section in case of for loop.

98. Which function is used to come out of a program and in which header file is it located?
Exit () is used to break out of a program and is found in the header file .

99. What is for loop called without the test condition?
Infinite loop.

100. What is the other name for sentinel controlled loop?
Indefinite repetition loop.

101. What is the other name for counter controlled loop?
Definite repetition loop.

102. What is an array?
An array is collection of similar kind of data types in continuous memory locations and is fixed in size.

103. What are the individual values of arrays called?
Indices or elements.

104. Why should we allocate one extra element space when declaring a character array?
The extra element space when declaring a character array is used for holding null character ‘\0’.

105. What are the things to be specified when declaring an array?
Name of the array, type and size are to be specified when declaring an array.

106. Which is the first subscript of an array 0 or 1?
zero (0).

107. How do you initialize a 2-D array?
Two-dimensional array can be initialized in the following way:
Ex.: int arr[2][3] = {1,2,3,4,5,6};

int arr[2][3] = {{1,2,3}, {4,5,6}}

int arr[2][3] = {
{1,2,3},
{4, 5,6}
};

108. In a multidimensional (2-D or 3-D) array which dimension can be optional?
First dimension.

109. What is static memory allocation?
Allocation of memory at compile time is called static memory allocation.

110. What is dynamic memory allocation?
Allocation of memory at run time is called dynamic memory allocation.

111. What are dynamic arrays?
Arrays created at run time are called dynamic arrays.

112. How are dynamic arrays created?
Dynamic arrays are created using pointer variables and memory management functions.

113. Name some memory management functions?
Malloc, Calloc, Realloc are memory management functions.

114. In which header file are memory management functions located?
In

115. What is the use of terminating null character?
As string size varied and may not be equal to array size, terminating null character is used to
determine the end of string.

116. What is the disadvantage of using scanf () for reading strings?
Scanf () terminates reading of a string as soon as it encounters white space.

117. Which function/s do you use to read strings with spaces?
Getchar (); and gets (variable name); are used to ready strings with spaces.

118. What is the difference between strcat (string, ‘!’); and strcat (string, “!”);
Characters in C are represented by small integers corresponding to their character set
values. In the strcat (string, ‘!’), ‘!’ is considered as a character. As strcat is used to
concatenate strings one should enclose it in double quotes which is done in strcat (string, “!”).

119. Are the following initializations correct? If not, why?

i. char a [] = "Hello, world!";

ii. char a [14];

a = "Hello, world!";
First initialization is correct. But in (ii) we are trying to assign “Hello, world!” directly. Strings
are arrays, and you can't assign arrays directly. Use strcpy () for it.
E.g.: strcpy (a, "Hello, world!");

120. What is concatenation of strings?
Adding of two strings is called concatenation.

121. What is the function used to concatenate strings and write its syntax?
Strcat (string1, string2);

122. What is the function used to compare strings and write its syntax?
Strcmp (string1, string2);

123. Is this statement correct?
scanf (“%s”, &string);
The statement is incorrect because we should not use address operator (&) in scanf when
reading a string.

124. What is the header file required when using string manipulation functions?

125. Name some string handling functions.
Strcmp (), strcat (), strcpy (), strlen (), strstr () are some string handling functions.

126. Which function can be used as an alternative to printf for printing strings?
puts ().

Functions:

127. What is a function?
A function is a self contained block of code that does a certain action.

128. What does the function definition should include?
Function definition should include function name, return type, argument list, local
variable declarations, function statements and a return statement.

129. Write the syntax of function definition.
(parameters)
{
variable declaration;
statements;
return statement;
}

130. What is the default return type for a function?
Integer.

131. What does function declaration or function prototype include?
Return type, function name, arguments list, terminating semicolon.

132. Write the syntax for function prototype.
Return-type function-name (arguments);

133. What is the difference between formal parameters and actual parameters?
The parameters used in function definition and prototype are formal parameters and the ones
used in function calls are call actual parameters.

134. List the different types of functions.
The different kinds of functions are:
a. Functions with no arguments and no return values.
b. Functions with arguments and one return value.
c. Functions with arguments and no return value.
d. Functions with no arguments and return value.
e. Functions with arguments and return multiple values.

135. What is the difference between “call by value” & “call by reference”?
These are the mechanisms for passing parameters in C.
In call by value only the value is passed into a function. The value can be changed inside the
function but the change doesn’t reflect in the main function whereas in call by reference the
change does reflect in the original function.

136. Give an example for ANSI C notation of a function?
int abc(int a, float b)
{
/* some code */
}

137. Give an example for Kernighan & Ritche (K & R) notation of a function?
int abc(a, b)
int a; float b;
{
/* some code*/
}

138. What is recursion?
Recursion is the process where a function calls itself repeatedly.

139. Which data structure logic is used for recursion?
Stack.

140. What is parameter passing?
The method of passing data from one function to another is called parameter passing.

141. What does extern mean in a function declaration?
Extern indicates that the function’s definition is in another source file. But there is no formal
difference between extern int a (); and int a ();

142. What do you mean by scope of a variable?
Scope determines what region of the program the variable is active.

143. What do you mean by visibility of a variable?
Visibility determines the accessibility of a variable.

144. Which storage class is used to retain the value of variable between function calls?
Static storage class.

145. Which operator is prefixed before the variables of the formal parameters in the function
declaration when using pass by pointers?

Address of operator (&).

Structure and Unions

146. Define structure.
Structure is a collection of different data types.

147. Write the syntax for structure.
struct
{
data type variable1;
data type variable2;
………..
};
struct struct1, struct2;

148. Write the different ways of structure declaration with examples.
There are 2 ways of declaring a structure.
Ex1: struct student
{
int roll_no;
char name[15];
};
struct student stud1, stud2;
Ex2: struct
{
int roll_no;
char name[15];
} stud1, stud2; //Tag name is optional

149. How do you initialize structure variables?
The structure variables cannot be initialized in the template. There are 2 ways of initializing a
structure.
1. main()
{
struct marks
{
int sub1;
int sub2;
};
struct marks m1 = {90, 88};
…….
}
2. struct marks
{
int sub1;
int sub2;
} m1 = { 99, 70};
main ()
{
struct marks m2 = { 86. 97};
……………
}

150. Can the structure variables be initialized at declaration?
No, the structure variables cannot be initialized at declaration.

151. How do you access structure members?
Structure variables are accessed using the member operator (.).
syntax: .datavariable
Ex: m1.sub;

152. What is the other name for member operator (.)?
The other name for member operator is Dot operator or period operator.

153. Can you compare structures like string comparison?
No, we cannot compare structures directly. If we want to compare we have to write our own
functions.

154. Can you perform logical operations on structure variables?
No, logical operations cannot be performed on structure variables directly. If we want to compare we have to compare the structure members individually.

155. What is slack byte in terms of structure?
Normally when structures are stored there is an unoccupied byte between two variables, this
unoccupied byte which is called slack byte.

156. Explain the reason why you can’t compare structures?
The slack bytes which are stored in the structure variables are undefined and are machine dependent. So, even though the value of 2 data members may be same but because of slack bytes they may not be equal. That is the reason we can’t compare structures.

157. What is precedence level of member operator (.) in comparison to arithmetic and relational
operators?

The member operator (.) has the higher priority then the arithmetic and relational operators.

158. Give the syntax of passing structure to function.
data_type (struct_type structure name)
{
…..
return (...);
}

159. Write the syntax for Union declaration.
union
{
data_type var1;
data_type var2;
}var;

160. What is the difference between structure and union?
Structure has separate memory allocation for each and every member of it whereas union
has a common memory allocation for all the members together and allocates memory
enough to hold the largest variable type.

Pointers:

161. What are pointer variables?
Variables that hold memory addresses are called pointer variables.

162. What are pointer constants?
Pointer constants are the memory addresses in a computer which cannot be changed.

163. Write the syntax for pointer declaration.
syntax: data_type *ptr;

164. Give an example for initializing pointer variable.
Ex.: int x, *ptr;
ptr = &x;
Now ptr contains the address of the variable x.

165. Why does the following initialization give unexpected results?
float avg;
int *ptr;
ptr = &avg;
In the above example pointer variable ptr has been assigned variable of different data type. Generally when pointer variable is declared system assumes that any address pointer holds
points to an integer variable.

166. How do you declare and initialize a pointer to pointer?
EX: int sum, *ptr1, **ptr2;
sum = 3;
ptr1 = & sum;
ptr2 = & ptr1;

167. Can a pointer variable be multiplied by a constant?
No, a pointer variable cannot be multiplied by a constant.

168. Name some rules for using expressions in pointer operations.
When performing operations on pointer variables, following points have to be kept in mind.
a. Increment and decrement operations can be done on pointer variables.
b. Any integer value can be added or deducted from pointer variable.
c. Pointers can be compared using relational operators.
d. Addition of 2 pointer variables is not possible.

169. Can you use address of (&) operator on register variables?
No, address of (&) operator cannot be used on register variables.

170. How do you initialize pointer variable with an array?
When initializing a pointer variable with an array only the base element of the array is assigned to pointer variable.
Ex: int a[3], *ptr;
ptr = & a; or
ptr = & a[0];
Because the all the array elements are stored in continuous memory locations, assigning of first value of array is sufficient to access all the array elements.

171. Can you pass arrays as arguments to functions?
No, arrays cannot be passed as arguments to functions directly, only the pointers can be passed.

172. What are anonymous locations?
The locations which are not accessed by name but accessed using the pointers are called anonymous locations.

173. How do you create anonymous locations?
Anonymous locations are created using dynamic memory allocation functions and pointers.

174. What is call by reference?
Call by reference is the process of calling a function using pointers to pass the addresses of
variables.

175. What is the difference between "char const *p" and "char * const p"?
"char const *p" is a pointer to a constant character (you can't change the character); "char *
const p" is a constant pointer to a (variable) character (i.e. you can't change the pointer).

Files:

176. What are the basic file operations?
The following are the basic file operations: Naming, Opening, Reading, Writing and Closing a
file.

177. What things are to be specified when we perform some operation on a file?
When ever we perform some operations on files, we need to mention the filename, data structure being used and the purpose of using the file.

178. Write the syntax for declaring a file.
The following is the syntax for file declaration.
FILE *fp;
FILE is defined data type or data structure.
fp is the pointer to FILE.

179. Write the syntax for opening a file.
FILE *fp;
fp = fopen (“file_name”, “mode”);
fopen () is the file operation

180. What are the different file modes?
There are 3 different file modes.
r – read mode
w – write mode
a – appending mode

181. What are the additional modes of file operation?
Apart from the normal modes of file operation, 3 additional modes of file operation exist. They are:
r+ - Reading and writing
w+ - reading and writing
a+ - reading and writing

182. Write the syntax for closing a file?
fclose (fp);
fp is the file pointer.

183. Why should we close a file once it is opened?
There are different reasons why a file should be closed after all its operations. They are:
a. When a file is closed all the data associated with the file is removed from the buffers and all file links are removed.
b. Any misuse of file can be avoided.
c. To open the file in different mode.

184. What are the files which are automatically opened when a “C” file is executed?
stdin, stdout, stderr (standard input, standard output, standard error).

185. What is the use of getc() and putc () functions?
getc () and putc () are the I/O functions for file handling.
getc () is used to read a single character from a file using file pointer until EOF is encountered.
putc () is used to write a single character to a file.

186. What is EOF?
EOF stands for End of File, which is used to check for the end of file when a file is being read.

187. What is the use of getw () and putw () functions?
getw () and putw () functions are similar to getc () and putc () functions and are used to read and write integer values.

188. Write the syntax for getc () and putc () functions.
var = getc (file pointer)
var is the variable which receives the value from getc () which reads a single character using the file pointer.
Syn : putc (var, file pointer)

189. What is the use of fprintf () and fscanf () functions.
fprintf () and fscanf () are I/O functions for operations on files.

190. Write the syntax for fscanf () and fprintf ().
Syn: fscanf (file pointer, “control string”, var);
fprintf (file pointer, “control string”, var);

191. List out the possible errors during file I/O operations.
The following are the possible error during file I/O operations.
a. Opening an invalid filename.
b. Using unopened file.
c. Trying to read beyond EOF.
d. Trying to write in write protected file.
e. Invalid file operation.

192. What is the function used to check end of file?
feof () function is used to check end of file.

193. What does fopen () return when a file is opened?
Whenever a file is opened using fopen () a file pointer is returned.

194. How can you identify whether a file is opened successfully or not?
When we open a file using fopen (), if the file is opened successfully it returns a file pointer
else it returns a NULL pointer. By using this, we can identify whether a file is opened or not.

195. List some I/O functions for handling files.
fopen () – Creates or opens a file
fclose () – Closes a file
getc () – reads a character from file
putc () – writes a character to a file
fseek () – sets the position of fp to desired location
rewind () – sets the position to file beginning

196. What is the use of function fcloseall ()?
fcloseall () closes all open streams except stdin, stdout, stderr, stdprn, and stdaux.

197. What is the use of ftell ()?
ftell () returns the current position of file pointer in a file by retuning the relative offset.

198. What is the use of rewind ()?
Rewind () is used to set the position to the beginning of a file.

199. Write the syntax for ftell ().
Syn: var = ftell (file pointer)
As ftell () returns the position in bytes it should be stored in a long data type.

200. Write the syntax for rewind ()?
rewind (file pointer)

201. What is the use of fseek ()?
fseek is used to move to a particular position in the file by mentioning the number of bytes.

202. Write the syntax for fseek ().
fseek (file pointer, offset, position)
Offset – specifies the number of bytes to be moved from the position given
Position – takes 3 values (0 – Beginning, 1 – Current position, 2 – End of file )

203. How can you identify whether fseek () is successful or not?
Whenever fseek () is used it returns a zero value (0) if successful or else returns -1. By checking this we can identify whether fseek is successful or not.

204. What is the disadvantage of using ‘w’ mode in file operation?
Whenever a file is opened using ‘w’ mode if a file exits already, the existing data of the file is
deleted.

205. What is the use of ferror ()?
ferror () function is indicates whether an error occurred or not during file processing. If an error
occurs it returns a non- zero value and if the processing is correct, it returns zero.


Dynamic memory allocation and Preprocessor:

206. What is dynamic memory allocation?
Allocation of memory at run time is called dynamic memory allocation.

207. List the memory management functions.
Malloc (), Calloc (), free (), realloc () are the memory management functions.

208. What is stack?
The area where local variables are stored is called stack.

209. What is heap?
The free memory space between the permanent storage area and stack is called heap.

210. What is the use of malloc ()?
Malloc () function is used to allocate the required number of memory bytes as specified.

211. Write the syntax for malloc () with an example?
pointer = (cast-type * ) malloc (byte size);
Ex: a = (int *) malloc (8);
In the above example malloc () allocates 8 bytes of memory.

212. What is the use of calloc ()?
Calloc () is used for allocation of multiple blocks of memory space and sets all bytes to zero.

213. Write the syntax for calloc ()?
pointer = (cast-type *) calloc (n, size);

214. How do you identify whether calloc is successful or not?
When allocating memory using calloc () if it is successful, pointer to the first byte is returned else a NULL pointer is returned.

215. What is the use of free ()?
Free () is used to release the memory allocated by malloc () and calloc () functions when it is
no longer required.

216. What is the use of realloc ()?
Whenever we allocate memory for data types, sometimes it may be less or sometimes more,
in such situations we would like to reallocate the memory for our use. Realloc () function is
used for reallocation of memory.

217. What is alloca () and why is its use discouraged?
Alloca () allocates memory which is automatically freed when the function which called
alloca () returns. Memory allocated with alloca is local to a particular function's "stack frame" or context. Alloca () cannot be written portably, and is difficult to implement on machines without a conventional stack. When its return value is passed directly to another function, as in fgets (alloca (100), 100, stdin) it doesn’t work properly.

218. What do you mean by preprocessor?
Preprocessor is a program that processes the source code before passing it to the compiler.

219. List few preprocessor directives.
Following are a few preprocessor directives.
#define – defines macro substitution
#include – mentions files to be included
#if – tests compile time condition
#else – gives alternatives when #if fails

220. What are the different categories of preprocessor directives?
The different preprocessor directives are Macro substitution directives, file inclusion directives, and compiler control directives.

221. What is macro substitution?
Macro substitution is the process of substitution of an identifier by an already defined string.

222. Give examples for different macro substitutions.
Simple Macro substitution: #define pi 3.14
Macro with arguments: #define sqr (a) a*a
Nested Macros: #define pi 3.14
#define r 5
#define area(r) pi*r*r

223. What is the difference between #include & #include “abc.h”?
The use of the angle brackets instructs the compiler to search the compiler’s include directory for the specified file. The use of the double quotes “ “around the filename instructs the compiler to search in the current directory for the specified file.

224. What is the difference between #define and typedef?
#define is used for textual replacement whereas typedef is used for declaring new type.

225. Write the syntax for pragma directive.
#pragma

226. What is the directive used to discord a macro?
#undef is used to discord a macro.

227. What is the use of #error?
#error preprocessor directive stops the compilation when an error occurs.

228. What is the use of “#”?
“#” is a string zing operator which is used to convert a formal argument in a macro definition to string.

229. Which operator is used to concatenate two strings?
“#” is used to concatenate two strings.

230. What are the different types of errors encountered with a program?
The following are the different types of errors encountered in a program:
Syntax errors: violation of syntax rules
Run-time errors : mismatch of data, out of range data etc
Logical errors : logically incorrect data
Latent errors : show up only with a particular set of data.

231. What is the use of asserts?
Asserts are used during debugging to make sure that certain conditions are satisfied. If the assertion fails the program terminates.

232. How do you disable asserts after debugging?
If we use #undef NDEBUG after debugging, it will disable all the asserts in the code.

233. What is masking?
Masking is the process of using logical bitwise operators to extract desired bits from a variable.

234. What does header file consist of?
consist utility functions like string conversion functions, memory allocation function, random number generator etc.

235. What are constants?
The values which do not change during program execution are called constants.

236. What are identifiers?
The names of arrays, classes, functions etc created by the user are called identifiers.

237. What are the rules to be followed while naming an identifier?
The following rules are to be followed while naming an identifier:
a. Identifier should not be started with a digit.
b. Alphabetic characters, underscores and digits are to be used.
c. Identifiers are case sensitive.
d. Keywords cannot be used as identifiers.

238. Is the following program correct?
main ()
{
100;
printf (“hello”);
}
Yes, the above program is correct because 100; is an executable statement with no action.

239. What are the user defined data types in C?
Enum, typedef, structure and union are the user defined data types in C++.

240. Name the built in data types in C.
Integer, float, double, character and void.

241. Name the derived data types in C.
Functions, pointers, arrays are the derived data types.

242. What is other name for built in data types?
Basic or fundamental data types.

243. What is test expression?
Test expression is the expression used to determine the course of action for conditional or iterative constructs.

244. What are the values of enum if not specified?
If the values are not specified enum assigns the numbers starting from 0.

Can you use –7 to decrement the value of 7 in C?
No, decrement (or increment) operator cannot be applied to integer values, it can be applied only to variables.




1 comment:

indianist said...

I am vary glad to say this, After reading your post i have a better understanding of What is BPO Jobs? They are many sites to know more words about BPO but your site is amazing