0% found this document useful (0 votes)
165 views36 pages

C Programming Viva Q&a

This document provides an overview of C programming and answers frequently asked questions about C in a viva or interview setting. It discusses what a program and programming language are, different types of programming languages like low-level and high-level languages, what C is and why it is called a "mother language", key features of C, its history and founder, differences between C and other languages like C++ and Java, basics of C syntax like header files and tokens, and more. The document aims to equip readers with knowledge about the fundamentals of C to help them in an oral exam context.

Uploaded by

HARIPRIYA V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
165 views36 pages

C Programming Viva Q&a

This document provides an overview of C programming and answers frequently asked questions about C in a viva or interview setting. It discusses what a program and programming language are, different types of programming languages like low-level and high-level languages, what C is and why it is called a "mother language", key features of C, its history and founder, differences between C and other languages like C++ and Java, basics of C syntax like header files and tokens, and more. The document aims to equip readers with knowledge about the fundamentals of C to help them in an oral exam context.

Uploaded by

HARIPRIYA V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

C PROGRAMMING

VIVA QUESTIONS AND ANSWERS

C Introduction

1. What is Program?

Program is defined as a sequence of instructions written to perform a well-defined task with a


computer.

2. What is meant by programming language?


Programming language is nothing but a language designed to communicate to
machines through instructions and commands.
Normally machines are computers. Programs are written using some
programming languages to control the behavior of machines/computers and to
make them to perform required tasks.

3. Types of Programming Language?


 Low level language
 High level language

4. What is Low level language?

 The language known to the computer is called low level language or machine
language.
 It uses stream of words made up of 0’s and 1’s for communication.

5. What is High level language?

 The language known to the programmer(human) is called high level language.


 This language uses meaningful words as used in English language.
 It is very easy to understand the language.

6. What is C language?

C is a mid-level and procedural programming language. The Procedural programming


language is also known as the structured programming language is a technique in which large
programs are broken down into smaller modules, and each module uses structured code. This
technique minimizes error and misinterpretation.
7. Why is C known as a mother language?

C is known as a mother language because most of the compilers and JVMs are written in C
language. Most of the languages which are developed after C language has borrowed heavily
from it like C++, Python, Rust, javascript, etc. It introduces new core concepts like arrays,
functions, file handling which are used in these languages.

8. Why is C called a mid-level programming language?

C is called a mid-level programming language because it binds the low level and high -level
programming language. We can use C language as a System programming to develop the
operating system as well as an Application programming to generate menu driven customer
driven billing system.

9. What is structured programming?

Structured programming is a programming paradigm that facilitates the creation of programs


with readable code and reusable components. All modern programming languages support
structured programming, but the mechanisms of support, like the syntax of the programming
languages, varies.

10. Who is the founder of C language?

Dennis Ritchie.

11. When was C language developed?

C language was developed in 1972 at bell laboratories of AT&T.

12. What are the key features in the C programming language?

 Portability: It is a platform-independent language.


 Modularity: Possibility to break down large programs into small modules.
 Flexibility: The possibility of a programmer to control the language.
 Speed: C comes with support for system programming and hence it compiles and
executes with high speed when compared with other high-level languages.
 Extensibility: Possibility to add new features by the programmer.

13. What is thepurpose of C developed in early?

The purpose of C developed in early is to develop a UNIX operating system.


14. What is the Structure of C?
• Preprocessor Commands
• Functions
• Variables
• Statements & Expressions
• Comments

15. Explain the difference between C and C++?

C++ C

C++ has a class concept C did not have a class concept


C++ does not support function In C programming, we use various
overloading concept functions like scanf() and printf() for input
and output.
C++ supports exception handling. C does not support exception handling

16. What is the primary difference between C and Java?

The primary difference between the two is that Java is an object-oriented programming
language, whereas C is a procedural programming language.

17. What is the extension of C file?

The extension of C is .c, for example: filename.c

18. What is Preprocessor?

A Preprocessor Directive is considered as a built-in predefined function or macro that


acts as a directive to the compiler and it gets executed before the actual C Program is
executed.

19. Explain compiler and interpreter.

 A compiler is a program that process code written in C and convert into machine
language.
 An interpreter is a program that directly executes C code without compiling into
machine language.

20. What is the main difference between the Compiler and the Interpreter?

Compiler is used in C Language and it translates the complete code into the Machine
Code in one shot. On the other hand, Interpreter is used in Java Programming Langauge
and other high-end programming languages. It is designed to compile code in line by line
fashion.
21. Describe the header file and its usage in C programming?

The file containing the definitions and prototypes of the functions being used in the
program are called a header file. It is also known as a library file.
Example: The header file contains commands like printf and scanf is from the stdio.h
library file.

22. What is the behavioural difference when the header file is included in double-quotes
(“”) and angular braces (< >)?

When the Header file is included within double quotes (“ ”), compiler search first in the
working directory for the particular header file. If not found, then it searches the file in
the include path. But when the Header file is included within angular braces (<>), the
compiler only searches in the working directory for the particular header file.

23. Is there any possibility to create a customized header file with C programming
language?

Yes, it is possible and easy to create a new header file. Create a file with function
prototypes that are used inside the program. Include the file in the ‘#include’ section from
its name.

24. Can we compile a program without main() function?

Yes, we can compile, but it can't be executed.

But, if we use #define, we can compile and run a C program without using the main()
function. For example:

#include<stdio.h>
#define start main
void start() {
printf("Hello");
}

25. What is the difference between #include "..." and #include <...>?

For #include <filename> the C preprocessor looks for the filename in the predefined
list of system directories first and then to the directories told by the user(we can use -I
option to add directories to the mentioned predefined list).

For #include "filename" the preprocessor searches first in the same directory as the file
containing the directive, and then follows the search path used for the #include
<filename> form. This method is normally used to include programmer-defined header
files.
26. What is the use of a semicolon (;) at the end of every program statement?

It is majorly related to how the compiler reads( or parses) the entire code and breaks it into
a set of instructions(or statements), to which semicolon in C acts as a boundary between
two sets of instructions.

Tokens in C

1. What is Character set in C?


 The character set is the fundamental raw material of any language and used to
represent information.
 The C character set consists of upper and lowercase alphabets, digits, special
characters and white spaces.

2. What are the types of character set?


 Source character set - upper and lowercase alphabets, digits, special characters
and white spaces.
 Execution character set – Escape sequence.

3. What is Escape sequence?


• An escape sequence in C language is a sequence of characters that doesn't
represent itself when used inside string literal or character.
• It is composed of two or more characters starting with backslash \.

4. What is Token in C?

 A token is the smallest individual unit in a program.


 Tokens are usually referred as individual text and punctuation in a passage of
text.

5. What are the tokens in C?

 Identifiers: Identifiers refer to the name of the variables.


 Keywords: Keywords are the predefined words that are explained by the
compiler.
 Constants: Constants are the fixed values that cannot be changed during the
execution of a program.
 Operators: An operator is a symbol that performs the particular operation.
 Special characters: All the characters except alphabets and digits are treated as
special characters.

6. What is constant?
The item where values cannot be changed during the execution of program are called
constants. const keyword is used to declare constant.
7. What is keyword?
 Keywords are reserved words whose meaning has already explained to the Compiler.
 Keywords are also called as reserved words.
 They must be written in lowercase.
 There are 32 keywords available in C.
8. What is Identifier?
Identifiers are names given to various program elements, such as variables, functions
and arrays etc.
9. Define Format Specifier.
• To tell the compiler what type of data the variable is storing.
• It starts with a percentage sign % followed by a character.
10. Describe the newline escape sequence with a sample program?

The Newline escape sequence is represented by \n. This indicates the point that the
new line starts to the compiler and the output is created accordingly.

Variable
11. What is variable?
 Variables are containers for storing data values.
 A variable is an identifier that is used to represent some specified type of
information within a designed portion of the program.

12. Explain static variable.

It is an access specifier. The value of a static variable does not change during the
execution of the program.

13. What is the difference between the local variable and global variable in C?
Global variable Local Variable
A variable which is declared outside A variable which is declared inside
function or block is known as a global function or block is known as a local
variable. variable.
The scope of variable is available in The scope of a variable is available within
throughout the program. a function in which they are declared.
Any statement in the entire program can Variables can be accessed only by those
access variables. statements inside a function in which they
are declared.
Life of a variable exists until the program Life of a variable is created when the
is executing. function block is entered and destroyed
on its exit.
The compiler decides the storage location Variables are stored in a stack unless
of a variable. specified.
14. What is /0 character?

The Symbol mentioned is called a Null Character. It is considered as the terminating


character used in strings to notify the end of the string to the compiler.

15. What are reserved words with a programming language?

The words that are a part of the standard C language library are called reserved
words. Those reserved words have special meaning and it is not possible to use them
for any activity other than its intended functionality. Example: void, return int.

16. What is volatile variable in C?

A volatile keyword in C is nothing but a qualifier that is used by the programmer


when they declare a variable in source code. It is used to inform the compiler that the
variable value can be changed any time without any task given by the source code.

Operators

17. What is operator?

An operator is a symbol which represents some operations that can be performed on


data.

18. What is operand?

The operand is a data or variable. Example int a; here, a is a operand.

19. What is Arithmetic operator?

Arithmetic operators are used to perform Arithmetic calculations.


There are two types:
 Binary operators- It need two operands for operations.
 Unary operators- It need only one operand for operation.

20. What is Relational operator?

Relational operators are used to find out the relationship between two operands.
21. What is Logical operator?
Logical operators are used to find out the relationship between relational expression.

22. What is Increment and Decrement operator?

 These are two special operators in C to control the loops in an efficient manner.
 ++ is the increment operator and – is the decrement operator.
23. What is the process to create increment and decrement statement in C?
There are two possible methods to perform this task.
Use increment (++) and decrement (--) operator.
Example: When x=4, x++ returns 5 and x-- returns 3.
Use conventional + or – sign.
24. Describe the difference between = and == symbols in C programming?

 ‘==’ is the comparison operator which is used to compare the value or expression
on the left-hand side with the value or expression on the right-hand side.
 ‘=’ is the assignment operator which is used to assign the value of the right-hand
side to the variable on the left-hand side.

25. What is operator overloading?

Operator overloading is a polymorphism which allows existing operators to be redefined


so that they can be used on objects of user-defined classes.

26. What is Ternary operator?


 It is also known as Conditional Operator.
 The Conditional operators ? and : are used to build simple condition expression.

27. What is Bitwise operator?

Bitwise operators are used to perform bit by bit operation.


 Bitwise left shift – used to shift the bit pattern left.
 Bitwise right shift – used to shift the bit pattern right.
 Bitwise complement- changes all the zero’s to one and ones to zero in the bit
patern.

28. What is the use of sizeof() operator?

 Size of operator is used to find the number of bytes occupied by the operand.
 Syntax: var_name= sizeof(operand);

29. Which is address operator?

The address of the variable an be got with the help of the address operator (&).

30. What is the difference between ++a and a++?


‘++a” is called prefixed increment and the increment will happen first on a variable.
‘a++’ is called postfix increment and the increment happens after the value of a
variable used for the operations.
31. Why n++ executes faster than n+1 ?

n++ being a unary operation, it just needs one variable. Whereas, n = n + 1 is a


binary operation that adds overhead to take more time (also binary operation: n +=
1). However, in modern platforms, it depends on few things such as processor
architecture, C compiler, usage in your code, and other factors such as hardware
problems.

While in the modern compiler even if you write n = n + 1 it will get converted into
n++ when it goes into the optimized binary, and it will be equivalently efficient.

32. What is C Expression?

An expression is a linear combination of constants variable and operators.

33. What is Type Casting?

Converting one datatype into another is known as type casting (or) type-conversion.

34. What are the types of Type casting?


 Implicit Type Conversion
 Explicit Type Conversion

35. What are the ways to convert a datatype into another?

 Implicit Type Conversion: Done by the compiler on its own, without any
external trigger from the user.
 Explicit Type Conversion - it is user defined. the user can type cast the result to
make it of a particular data type. Syntax: (type) expression

Input/Output Statements

36. What are types of Input/Output Statements in C?

• Formatted I/O Statement


• Unformatted I/O Statement

37. What is the use of printf() and scanf() functions?

printf(): The printf() function is used to print the integer, character, float and string
values on to the screen.
scanf(): The scanf() function is used to take input from the user.

38. What is the purpose of sprintf() function?


The sprintf() stands for "string print." The sprintf() function does not print the output
on the console screen. It transfers the data to the buffer. It returns the total number of
characters present in the string.
39. Differentiate Formatted I/O Statement & Unformatted I/O Statement.

40. What is the difference between getch() and getche()?

The getch() function reads a single character from the keyboard. It doesn't use any
buffer, so entered data will not be displayed on the output screen.

The getche() function reads a single character from the keyword, but data is displayed
on the output screen. Press Alt+f5 to see the entered character.

41. What are the functions used in Unformatted I/O Statement?

• getch()
• getche()
• getchar()
• putchar()
• gets()
• puts()

42. What are the functions used in formatted I/O Statement?

• printf()
• scanf()
• sprint()
• sscanf()

43. Differentiate getchar() and gets()?

getchar()- This function is used to read a single character.

gets()- This function is used to read a string of charaters until a new line character ‘\n’
is entered(return key).
Looping Statements

44. What is Loop?


Loop are used to execute a group of statements repeatedly until some condition is
satisfied.

45. How the loop classified based on the position of condition?


 Entry Controlled Loop
 Exist Controlled Loop

46. What is Entry Controlled Loop?


 A condition is checked before executing the body of a loop. It is also called
as a pre-checking loop.
 Entry Controlled Loop in C is, for loop and while loop.

47. What is Exist Controlled Loop?


 A condition is checked after executing the body of a loop. It is also called as a
post-checking loop.
 Exist Controlled Loop in C is, do-while loop.

48. What is while loop?


 The while loop is an Entry controlled loop.
 It means the condition is evaluated first and if it is true, then the body of the
loop is executed.
 If the condition is false, then the control transferred out of the loop.

49. What is the syntax of while?


while(condition)
{
……
Body of the loop;
…….
}

50. What is do…while?


 It is also repetitive control structure and executes the body of the loop once
irrespective of the condition.
 Then it checks the condition and continues the execution until the condition
becomes false.

51. What is for loop?

The for loop is repetitive control structure, and is used to execute set of instructions
repeatedly until the condition becomes false.
52. What is the syntax of do…while?
do
{
…..
Body of the loop;
…..
}
while(condition);

53. What are the three parts of for loop?


 Initialize counter
 Test condition
 Increment/Decrement counter

54. What is the syntax of for loop?


for(initialize counter; Test condition; Increment/Decrement counter)
{
……
Body of the loop;
……
}

55. What is nested for loop?


The loop within loop is called nested loop. In nested for loop two or more for
statements are included in the body of the loop.

56. Differentiate while and do…while: -

While do…while
It is entry-controlled loop. It is exist controlled loop.
First test the condition it true means It execute the body once, after it checks
execute the body of the loop. the condition.
Loop will not be executed if the condition Loop is executed atleast once.
is false.
Top-down testing Bottom-up testing

57. Which loop is faster in c programming for, while or do while?


Do-while is the fastest among others. The main reason is that in the do-while loop the
is first executed and then the condition is checked.

But in both for loop and while loop has a conditional part at the beginning of the loop
and after the condition is checked it enters into the loop and starts execution.
58. Which loop is more powerful in C?

For loops (at least considering C99) are superior to while loops because they limit the
scope of the incremented variable(s).

59. What is an infinity loop?


An infinite loop (sometimes called an endless loop ) is a piece of coding that lacks a
functional exit so that it repeats indefinitely. In computer programming, a loop is a
sequence of instruction s that is continually repeated until a certain condition is
reached.

60. Why while loop called as pre-test loop?


Because the while loop checks the condition/expression before the block is executed,
the control structure is often also known as a pre-test loop.

61. What is empty loop?


An empty loop is a loop which does not have any updation or value of iteration. For
example, for(int i = 1;;) An empty loop is infinite.

62. Is recursion better than nested loop?


Yes, recursion does get a new call-stack on each call to itself, but that may or may not
take much memory, depending on what needs to be stored.

63. What is the difference between for and while loop?

For loop While loop


Initialization may be either in loop Initialization is always outside the loop.
statement or outside the loop.
Once the statement(s) is executed Increment can be done before or after
then after increment is done. the execution of the statement(s).
It is normally used when the It is normally used when the number of
number of iterations is known. iterations is unknown.
Condition is a relational Condition may be expression or non-
expression. zero value.
It is used when initialization and It is used for complex initialization.
increment is simple.
For is entry-controlled loop. While is also entry-controlled loop.
for ( init ; condition ; iteration ) { while (condition) {
statement(s); statement(s);
} }
used to obtain the result only when used to satisfy the condition when the
number of iterations is known. number of iterations is unknown

64. What are the valid places to have keyword “Break”?

The purpose of the Break keyword is to bring the control out of the code block which
is executing. It can appear only in looping or switch statements.
65. What is the usage of break statement?

When a break statement is encountered inside a loop, the loop is immediately


terminated and the program control resumes at the next statement following the loop.

It can be used to terminate a case in the switch statement.

66. How continue statement works?

 The continue statement in C programming works somewhat like


the break statement. Instead of forcing termination, it forces the next iteration
of the loop to take place, skipping any code in between.
 For the for loop, continue statement causes the conditional test and increment
portions of the loop to execute.

67. What is the purpose of break and continue?

The break statement terminates a while or for loop completely. The continue
statement terminates execution of the statements within a while or for loop and
continues the loop in the next iteration.

68. Differentiate break and continue.


Break Statement Continue Statement
The Break statement is used to exit The continue statement is not used to exit
from the loop constructs. from the loop constructs.
The break statement is usually used The continue statement is not used with the
with the switch statement, and it switch statement, but it can be used within
can also use it within the while the while loop, do-while loop, or for-loop.
loop, do-while loop, or the for-
loop.
When a break statement is When the continue statement is
encountered then the control is encountered then the control automatically
exited from the loop construct passed from the beginning of the loop
immediately. statement.
Syntax: Syntax:
break; continue;

Array

69. What is Array?


 An array is a collection of similar data type that are stored under a common
name.
 A value in an array is identified by index or subscript enclosed in square
brackets with array name.

70. What are the Types of arrays in C?


 One dimensional array
 Two-dimensional array
71. What is one-dimensional array in C?
The array having only one subscript is called one dimensional array. Example: int
a[10];

72. What is two-dimensional array in C?


The array having two subscript is called two dimensional array. Example: int a[5][6];

73. What is character array?


 Character array is string.
 A String is a collection of characters.
 A string constant is a one dimensional array of characters terminated by a
null(‘\0’) character.

74. How to create an Array?


An Array is declared similar to how a variable is declared, but you need to add [] after
the type.

75. What are the advantages of Array in C?

 We can put in place other data structures like stacks, queues, linked lists, trees,
graphs, etc. in Array.
 Arrays can sort multiple elements at a time.
 We can access an element of Array by using an index.

76. What are the disadvantages of Array?


 have to declare Size of an array in advance. However, we may not know what
size we need at the time of array declaration.
 The array is static structure. It means array size is always fixed, so we cannot
increase or decrease memory allocation.

77. Can we change the size of an array at run time?

 No, we cannot change the array size. Though there are similar data types
available which allow a change in size.

78. Can you declare an array without assigning the size of an array?
No we cannot declare an array without assigning size.

 If we declare an array without size, it will throw compile time error

 Example: marks = new int []; //COMPILER ERROR

79. What is the default value of Array?


 For byte, short, int, long – default value is zero (0).
 For float, double – default value is 0.0.
 For Boolean – default value is false.
 For object – default value is null.
80. Can we declare array size as a negative number?

No. We cannot declare the negative integer as an array size.

If we declare, there will be no compile-time error.

However, we will get NegativeArraySizeException at run time.

81. When will we get ArrayStoreException?


It is a runtime exception. For example, we can store only string elements in a String
Array. If anybody tries to insert integer element in this String Array, then we will get
ArrayStoreException at run time.

82. Can we add or delete an element after assigning an array?


No, it is not possible.

83. What is the difference between array and structure?

Array Structure

An array is a collection, to related data Structure can have elements of different


elements of same type. types.

An array is a derived data type. A structure is a programmer-defined


datatype.

Any array behaves like a built-in data But in the case of structure, first we have
type. All we have to do is to declare an to design and declare a data structure
array variable and use it. before the variable of that type are
declared and used.

String

84. What is String? and how declare it?


 The group of characters, digits and symbols enclosed within quotation marks
are called as string.
 Strings are array of characters.
 The characters of the string are enclosed within a pair of double quotes.
 Null character (‘\0’) is used to mark the end of the string.

85. How to declare a string variable?


A string variable should be declared as a character type array and it is used to store
string.
86. What are the functions used to read the string?

 scanf()
 getchar()
 gets()
87. What are the string handling functions?

 strlen()

 strcat()

 strcmp()

 strcpy()

 strrev()

88. What is the function of strlen()?


This function is used to count and return the number of characters present in a string.

89. What is strcat()?


This strcat() function is used to combine, two strings together and form a new
concatenated string.

90. What is the usage of strcmp()?


Strcmp() function compares two strings to find out whether they are same or different.

91. Define strrev().

 The strrev() function is used to reverse a string.

 This function takes only one argument and return one argument.

92. What is strcpy()?


This function is used to copy the contents of one string to another and it almost works
like string assignment operator.

93. What is the difference between a string copy (strcpy) and a memory copy
(memcpy)? When should each be used?

The strcpy() function is designed to work exclusively with strings. It copies each byte
of the source string to the destination string and stops when the terminating null
character (\0) has been moved. On the other hand, the memcpy() function is designed
to work with any type of data.
Because not all data ends with a null character, you must provide
the memcpy() function with the number of bytes want to copy from the source to the
destination.
94. How can I remove the trailing spaces from a string?
The C language does not provide a standard function that removes trailing spaces
from a string. It is easy, however, to build your own function to do just this.

File Management

95. How to define a file in c?


FILE *fp;

 FILE- it is a special structure type defined within the standard Input/output


library.

 fp- it is a file pointer variable that points to the beginning of the buffer. It is
also known as stream pointer.

96. How to create a new file?


The fopen() function to create a new file or to open an existing file.

97. What are the different mode for file opening and their operations in file
handling?
 r - open a file in read mode
 w - opens or create a text file in write mode
 a - opens a file in append mode
 r+ - opens a file in both read and write mode
 a+ - opens a file in both read and write mode
 w+ - opens a file in both read and write mode

98. What are the basic build-in function to perform file operations?
 fopen() - create a new file or open a existing file
 fclose() - close a file
 getc() - reads a character from a file
 putc() - writes a character to a file
 fscanf() - reads a set of data from a file
 fprintf() - writes a set of data to a file
 getw() - reads a integer from a file
 putw() - writes a integer to a file
 fseek() - set the position to desire point
 ftell() - gives current position in the file
 rewind() - set the position to the beginning point
99. What are the functions to open and close the file in C language?

The fopen() function is used to open file whereas fclose() is used to close file.

100. What are the different modes and operations in file handling?
Mode Description
R Opens a text file in reading mode
W Opens or create a text file in writing mode
A Opens a text file in append mode
r+ Opens in both reading & writing mode
w+ Opens in both reading & writing mode
a+ Opens in both reading & writing mode
Rb Opens a binary file in reading mode
Wb Opens or create a binary file in writing mode
Ab Opens a binary file in append mode
rb+ Opens a binary file in both reading and writing mode
wb+ Opens a binary file in both reading and writing mode
ab+ Opens a binary file in both reading and writing mode

101. What are Input Output operation of file in C?


 getc()- read a character
 putc()-write a character
 fprintf()-write set of data values
 fscanf()-read set of data values
 getw()-read integer
 putw()-write integer

102. What are the types of error occur during Input/Output operations?
 Trying to read beyond end-of-file.
 Trying to use a file that has not been opened.
 Perform operation on file not permitted by ’fopen’ mode.
 Open file with invalid filename.
 Write to write-protected file Error handling.

103. What is foef()?


The foef function tests the end-of-file indicator for the stream pointed to by stream
and returns nonzero if and only if the end-of-file indicator is set for stream, otherwise
it returns zero.

104. What is ferror()?


The ferror function tests the error indicator for the stream pointed to by stream and
returns non zero if and only if the error indicator is set for stream, otherwise it returns
zero.
105. What is the description for syntax errors?

 The mistakes/errors that occur while creating a program are called syntax
errors.
 Misspelled commands or incorrect case commands, an incorrect number of
parameters in calling method /function, data type mismatches can be identified
as common examples for syntax errors.

106. What is the use of rewind() function?


It resets the position of the file pointer to the start of the file. It is used to read a file
more than once without having to close and open the file. Syntax: rewind(fp);

107. What is command line argument?


The command line argument is a parameter supplied to main() function when the
program is invoked.

108. What is the syntax of command line argument?


main(argc, *argv[j]);
in argc, char*argv[j];
{
……..
……..
}

109. What is the use of argv()?


Argv is an argument vector. It represents an array of character pointer that point to the
command line arguments. The size of this array is equal to the value of argc.

110. What is meant by preprocessor?


The preprocessor is a program that processes the source program before it passes it
passes through the compiler.

111. What are the different types of preprocessor directives?


 Macro substitution directives
 File inclusion directives
 Compile control directives

112. What are the rules for macro definition.


 No space between # and define.
 One blank space between #define and identifier and between identifier and
string.
 No semicolon at the end.
 Identifier must be valid c name.

113. What are the different forms of macro substitution?


 Simple macro substitution.
 Argumented macro substitution.
 Nested macro substitution.
114. Define macro.
The macro substitution is used to define a global symbol in the C program that
represents a value. The general form is #define identifier symbol.

115. In C, What is the #line used for?

In C, #line is used as a preprocessor to re-set the line number in the code, which
takes a parameter as line number.

116. What are the advantages of Macro over function?

Macro on a high-level copy-paste, its definitions to places wherever it is called. Due


to which it saves a lot of time, as no time is spent while passing the control to a new
function and the control is always with the callee function. However, one downside
is the size of the compiled binary is large but once compiled the program
comparatively runs faster.

117. Which is better #define or enum?

 The compiler can build enum values automatically. However, each of the defined
values must be given separately.
 Because macros are preprocessors, unlike enums, which are compile-time entities, the
source code is unaware of these macros. So, if we use a debugger to debug the code, the
enum is superior.
 Some compilers will give a warning if we use enum values in a switch and the default
case is missing.
 Enum always generates int-type identifiers. The macro, on the other hand, allowed us to
pick between various integral types.
 Unlike enum, the macro does not have a defined scope constraint

118. How do you override a defined macro?

To override a defined macro we can use #ifdef and #undef preprocessors as follows:

 #ifdef A
 #undef A
 #endif
 #define A 10

If macro A is defined, it will be undefined using undef and then defined again using
define.
119. What is different between macro and function?
Macros Functions

It is preprocessed rather than It is compiled not preprocessed.


compiled.
It is preprocessed rather than Function checks for compilation errors.
compiled.
Code length is increased. Code length remains the same.

Macros are faster in execution. Functions are a bit slower in execution.

Macros are useful when a small Functions are helpful when a large piece
piece of code is used multiple times of code is repeated a number of times.
in a program.
Data Types
1. What is data type in c?
A data type is a special keyword used to allocate sufficient memory space for the data, in other
words, Data type is used for representing the data in main memory (RAM) of the computer.
2. What are the data types in c?

Types Data types

Basic Data Type int, char, float, double

Derived Data Type array, pointer, structure, union

Enumeration Data Type enum

Void Data Type void

3. what is basic data type?


Basic Data Type: int,char,float,double
int: As the name suggests, an int variable is used to store an integer.
Char: The most basic data type in C. It stores a single character and requires a single byte of
memory in almost all compilers.
Float: It is used to store decimal numbers (numbers with floating point value) with single
precision.
Double: It is used to store decimal numbers (numbers with floating point value) with double
precision.

4. What is derived data types?


The derived data types are basically derived out of the fundamental data types.
 Array
 Pointer
 Structure
 Union.

5.What is array?

 An array is a collection of similar data elements stored at contiguous memory locations.


 It is the simplest data structure where each data element can be accessed directly by only
using its index number.
Syntax:

data_Type array_Name[arraySize];

6.What is pointer?

A pointer is a variable that stores a memory address. Pointers are used to store the addresses of
other variables or memory items.

Syntax:

datatype *var_name;

7.what is structure?
 A structure is a key word that create user defined data type in C.
 A structure creates a data type that can be used to group items of possibly different
types into a single type.
Syntax:
struct structureName { dataType member1; dataType member2; ... };
8.what is union?
 A union is a special data type available in C that allows to store different data types in
the same memory location.
 You can define a union with many members, but only one member can contain a value
at any given time.
 Unions provide an efficient way of using the same memory location for multiple-
purpose.
Syntax:
union unionName { dataType member1; dataType member2; ... };
9. What is enumeration data type?

 Enumeration or Enum in C is a special kind of data type defined by the user.


 It consists of constant integrals or integers that are given names by a user.
 The use of enum in C to name the integer values makes the entire program easy to learn,
understand, and maintain by the same or even different programmer.

10. what is void data type?

A data type that has no values or operators and is used to represent nothing..
Decision Control Statements

1.What is decision making in c?

C conditional statements allow you to make a decision based upon the result of a condition.
These statements are called Decision Making Statements or Conditional Statements.

2. What are the conditional statements in c?

Conditional Statements in C

 If statement
 if statement
 if-else statement
 Nested if-else statement
 else if-statement

 goto statement
 switch statement
 Conditional Operator
3. What is if statement?

 If statements in C is used to control the program flow based on some condition,


 it's used to execute some statement code block if the expression is evaluated to true.
Otherwise, it will get skipped.
 This is the simplest way to modify the control flow of the program.

Syntax:

if(test_expression)
{
statement 1;
statement 2;
...
}

4. What is if else statement?

 If else statements in C is also used to control the program flow based on some
condition,
 only the difference is: it's used to execute some statement code block if the expression
is evaluated to true, otherwise executes else statement code block.
Syntax:
if(test_expression)
{
//execute your code
}
else
{
//execute your code
}

5. What is nested if statement?


Nested if else statements play an important role in C programming, it means you can use
conditional statements inside another conditional statement.

Syntax:

if(test_expression one)
{
if(test_expression two) {
//Statement block Executes when the boolean test expression two is true.
}
}
else
{
//else statement block
}

6. What is else if statement?


else-if statements in C is like another if condition, it's used in a program when if statement
having multiple decisions.
Syntax:
if(test_expression)
{
//execute your code
}
else if(test_expression n)
{
//execute your code
}
else
{
//execute your code
}

7. What is goto statement in c?


The goto statement is used by programmers to change the sequence of execution of a C program
by shifting the control to a different part of the same program.
Syntax:
goto label;

--------
--------

label:

statement - X;
/* This the forward jump of goto statement */

8.What is switch statement in c?

 C switch statement is used when you have multiple possibilities for the if statement.
Switch case will allow you to choose from multiple options.
 When we compare it to a general electric switchboard, you will have many switches in
the switchboard but you will only select the required switch, similarly, the switch case
allows you to set the necessary statements for the user.

Syntax:

switch(variable)
{
case 1:
//execute your code
break;

case n:
//execute your code
break;

default:
//execute your code
break;
}
Structures

1.What is structure in c?

 A structure is a key word that create user defined data type in C.


 A structure creates a data type that can be used to group items of possibly different
types into a single type.
Syntax:
struct structureName { dataType member1; dataType member2; ... };
2. Why structure called as a function arguments?
You can pass a structure as a function argument in the same way as you pass any other variable
or pointer.

3.What is Pointers to Structures

You can define pointers to structures in the same way as you define pointer to any other variable.
Syntax:
Struct Books*struct_pointer;
4.What is nested structure in c?
A structure inside another structure is called nested structure.

Example:

struct emp
{

int eno;
char ename[30];
float sal;
float da;
float hra;
float ea;

}e;

5. What is array of structure in c?


An array of structure in C programming is a collection of different datatype variables, grouped
together under a single name.

Syntax:
struct tagname
{
datatype member1;
datatype member2;
datatype member n;
};
6. What is structure to function in c?

 Structure-function can be used to write code effectively. The structure can be passed as a
parameter to the function.
 An entire structure can be passed to a function, or individual members of the structure
can be passed into the function.
 Individual members of the structure can be accessed using the dot operator(.).

7.What is typedif in c?

 typedef is a reserved keyword in the programming languages C and C++.


 It is used to create an additional name (alias) for another data type,
 but does not create a new type, except in the obscure case of a qualified typedef of an
array type where the typedef qualifiers are transferred to the array element type.
Union
1.What is union?
 A union is a special data type available in C that allows to store different data types in the
same memory location.
 You can define a union with many members, but only one member can contain a value
at any given time.
 Unions provide an efficient way of using the same memory location for multiple-
purpose.
Syntax:
union unionName { dataType member1; dataType member2; ... };
2. How to create union variable?
Union variables can be created in similar manner as structure variable
Example:
Union car {
Char name[50];
int price;
Car1,car2,*car3;
}
3. What is the difference between structure and union?

Structure Union

Keyword The keyword struct is used to The keyword is used to


define a structure define a union.

Size when a variable is associated when a variable is


with a structure, the compiler associated with a
allocates the memory for union, the compiler
each member. The size of allocates the memory
structure is greater than or by considering the size
equal to the sum of size of its of the largest memory.
members. So, size of union is
equal to the size of
largest member.

Memory Each member within a Memory allocated is


structure is assigned unique shared by individual
storage are of location members of union.

Value Altering Altering the value of a Altering the value of


member will not affed other any of the member will
member of the structure alter other member
values.

Accessing members Individual member can be Only one member can


accessed at a time be accessed at a time

Initialization of Members Several member of structure Only the first member


can initialize at once. of a union can be
initialize

4.What is the size of union in C?


 When we declare a union, memory allocated for a union variable of the type is equal to
memory needed for the largest member of it, and all members share this same memory
space. In above example, "char arr[8]" is the largest member.
 Therefore size of union test is 8 bytes.
5. How to define a union?
union car{
Char name[50];
int price,
};
6. How to create union variable?

When a union is defined, it creates a user-defined type. However, no memory is allocated. To


allocate memory for a given union type and work with it, we need to create variables.
union car
{
Char name[50];
int price;
}
car1,car2,*car3;

Pointer

1.What is pointer?

 A pointer is a variable that stores the memory address of another variable as its value.
 A pointer variable points to a data type (like int) of the same type, and is created with
the * operator. The address of the variable you're working with is assigned to the pointer.

2. What are the different types of pointers?

 Wild pointer

 Complex pointers

 Near pointer

 Far pointer

 Huge pointers

 Null pointer

 Dangling pointer

 Generic pointer

3.What is the dangling pointer in C?

When there is a pointer pointing to a memory address of any variable, but after some time the
variable was deleted from the memory location while keeping the pointer pointing to that
location is known as a dangling pointer in C.
4.What is Wild Pointers in C?

Uninitialized pointers in the C code are known as Wild Pointers. They point to some arbitrary
memory location and can cause bad program behaviour or program crash.
5.What is Null pointer?

You create a null pointer by assigning the null value at the time of pointer declaration.
This method is useful when you do not assign any address to the pointer. A null pointer always
contains value 0.
6.What is generic pointer?
 It is a pointer that has no associated data type with it. A void pointer can hold addresses
of any type and can be typecast to any type.
 It is also called a generic pointer and does not have any standard data type.
 It is created by using the keyword void.
7.What is Complex pointers?

 A Complex pointers contains of [] , * ,(), data type, identifier.


 These operators have different associativity and precedence. () & [] have the highest
precedence & associativity from left to right followed by * & Identifier having
precedence 2 & associativity from right to left and Data Type having the least priority.
8. What is near pointer?
 Near pointer is a pointer which is used to bit address of up to 16 bits in a given section of
the computer memory that is 16 bit enabled.
 It can only access data of a small size of about 64 kb in a given period, which is the main
disadvantage of this.
9.What is far pointer?
 Far pointer is a 32-bit pointer, can access information which is outside the computer
memory in a given segment.
 To use this pointer, one must allocate his/her sector register to store data address in the
segment and also another sector register must be stored within the most recent sector
10.What is huge pointer?
 Huge pointer has the same size of 32-bit to that of a far pointer, and it can also access
bits that are located outside the sector.
 Far pointer which is fixed and hence that part of the sector in which they are located
cannot be modified in any way; huge pointers can be.
11.What is the difference between array and pointer?

Array Pointer

Array is a constant pointer Pointer variable can be changed

It refers directly to the elements It refers address of the variable

Memory allocation is in sequence. Memory allocation is random.

Allocates the memory space which cannot Allocated memory size can be resized.
resize or reassigned

It is a group of elements. It is not a group of elements. It is a single


variable.

Array can be initialized at definition. Example Pointer can’t be initialized at a definition


int num[] = { 2, 4, 5}

The assembly code of Array is different than The assembly code of Pointer is different than
Pointer. Array.

12.What is pointer to function?

 A pointer to a function is declared as follows, type (*pointer-name)(parameter); Here is


an example : int (*sum)(); //legal declaration of pointer to function int *sum(); //This is
not a declaration of pointer to function.

 A function pointer can point to a specific function when it is assigned the name of that
function ...

13.What is string as pointer?


 A pointer to string in C can be used to point to the base address of the string array, and its
value can be dereferenced to get the value of the string.
 To get the value of the string array is iterated using a while loop until a null character is
encountered.
14.What is call by reference in c?
 The call by reference method of passing arguments to a function copies the address of an
argument into the formal parameter.
 Inside the function, the address is used to access the actual argument used in the call. It
means the changes made to the parameter affect the passed argument.
15.What is call by value?
 An lvalue (locator value) represents an object that occupies some identifiable location in
memory (i.e. has an address).
 rvalues are defined by exclusion. Every expression is either an lvalue or an rvalue, so, an
rvalue is an expression that does not represent an object occupying some identifiable
location in memory.
16.What is the difference between call by value and call by reference?

Call by Value Call by Reference


When a copy of the value is passed to the When a copy of the value is passed to the
function, then the original value is not function, then the original value is modified.
modified.
Actual arguments and formal arguments are Actual arguments and formal arguments are
created in separate memory locations. created in the same memory location.
In this case, actual arguments remain safe as In this case, actual arguments are not
they cannot be modified. reliable, as they are modified.
The copies of the actual arguments are passed The addresses of actual arguments are
to the formal arguments. passed to their respective formal arguments.

17.What is a memory leak in C?


 While writing an application, the developer has allocated some memory which he missed to
deallocate after use.

 This results in unused allocated memory - and such phenomenon is referred to as a memory
leak in C. Unused allocated memory could affect the system's performance and cause the
speed to be slower.

 It is important to release all allocated memory after usage.

18. Explain the advantages of pointer in C?

 Pointers provide direct access to memory locations.


 They provide an alternate and better way to handle arrays.
 Pointers provide us the feature to dynamically allocate and deallocate memory.
 They help us to create and use complex data structures like trees, graphs, linked lists, stack,
queue, etc.
19. Explain the disadvantages of pointer?

 They are a complex feature to understand.


 If an incorrect value is updated in memory using pointers, this causes memory corruption.
 If not properly deallocated, pointers may cause a memory leak.
 They may cause a memory segmentation fault(accessing a non-existent memory location)
if left uninitialised.

20. What is the difference between NULL and VOID pointers?

NULL VOID

A null pointer is a pointer that is not pointing to A void pointer type that remains void until an
any data type currently. address is assigned to it.

It can be to point data types It is generic pointer which can point to any data
int,float,char,etc..According to its own type. type

Dynamic Memory Allocation

1. What is dynamic memory allocation?


 Dynamic memory allocation is the process of assigning the memory space during the
execution time or the run time.
 Reasons and Advantage of allocating memory dynamically: When we do not know how
much amount of memory would be needed for the program beforehand.
2. What are the different functions in dynamic memory allocation?
 malloc () − allocates a block of memory in bytes at runtime.
 calloc () − allocating continuous blocks of memory at run time.
 realloc () − used for reduce (or) extend the allocated memory.
 free () − deallocates previously allocated memory space
3.What is the different between calloc() and malloc()?

calloc() malloc()
Description The malloc() function The calloc() function allocates multiple blocks
allocates a single block of of requested memory.
requested memory.
Initialization It initializes the content of the It does not initialize the content of memory, so
memory to zero. it carries the garbage value.
Number of It consists of two arguments. It consists of only one argument.
arguments
Return value It returns a pointer pointing to It returns a pointer pointing to the allocated
the allocated memory. memory.

4. what is the difference between static and dynamic memory allocation in the C language?

Static memory allocation Dynamic memory allocation

Memory is allocated during compile time. Memory is allocated during the runtime of the
program.

Once allocated we cannot change the memory We can change the memory size according to
size. need.

Faster Slower

Memory remains allocated till the complete We can release the memory in between the
program runs. program run.

Example: Array Example: Tree

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy