C Programming Viva Q&a
C Programming Viva Q&a
C Introduction
1. What is Program?
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.
6. What is C 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.
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.
Dennis Ritchie.
C++ C
The primary difference between the two is that Java is an object-oriented programming
language, whereas C is a procedural programming language.
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.
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
4. What is Token in C?
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.
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 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.
Operators
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.
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.
Size of operator is used to find the number of bytes occupied by the operand.
Syntax: var_name= sizeof(operand);
The address of the variable an be got with the help of the address operator (&).
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.
Converting one datatype into another is known as type casting (or) type-conversion.
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
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.
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.
• getch()
• getche()
• getchar()
• putchar()
• gets()
• puts()
• printf()
• scanf()
• sprint()
• sscanf()
gets()- This function is used to read a string of charaters until a new line character ‘\n’
is entered(return key).
Looping Statements
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);
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
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).
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?
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.
Array
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.
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.
Array Structure
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
scanf()
getchar()
gets()
87. What are the string handling functions?
strlen()
strcat()
strcmp()
strcpy()
strrev()
This function takes only one argument and return one argument.
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
fp- it is a file pointer variable that points to the beginning of the buffer. It is
also known as stream pointer.
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
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.
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.
In C, #line is used as a preprocessor to re-set the line number in the code, which
takes a parameter as line number.
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
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
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?
5.What is array?
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?
A data type that has no values or operators and is used to represent nothing..
Decision Control Statements
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.
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?
Syntax:
if(test_expression)
{
statement 1;
statement 2;
...
}
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
}
Syntax:
if(test_expression one)
{
if(test_expression two) {
//Statement block Executes when the boolean test expression two is true.
}
}
else
{
//else statement block
}
--------
--------
label:
statement - X;
/* This the forward jump of goto statement */
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?
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;
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?
Structure Union
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.
Wild pointer
Complex pointers
Near pointer
Far pointer
Huge pointers
Null pointer
Dangling pointer
Generic pointer
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?
Array Pointer
Allocates the memory space which cannot Allocated memory size can be resized.
resize or reassigned
The assembly code of Array is different than The assembly code of Pointer is different than
Pointer. Array.
A function pointer can point to a specific function when it is assigned the name of that
function ...
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.
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
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?
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.