CP Viva
CP Viva
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.
5.What is Preprocessor?
In case you are facing any challenges with these C Programming Interview Questions, please
write your problems in the comment section below.
Ans: printf() is used to print the values on the screen. To print certain values, and on the other
hand, scanf() is used to scan the values. We need an appropriate datatype format specifier for
both printing and scanning purposes. For example,
%d: It is a datatype format specifier used to print and scan an integer value.
%s: It is a datatype format specifier used to print and scan a string.
%c: It is a datatype format specifier used to display and scan a character value.
%f: It is a datatype format specifier used to display and scan a float value.
8. What is an array?
Ans. The array is a simple data structure that stores multiple elements of the same datatype in
a reserved and sequential manner. There are three types of arrays, namely,
9. What is /0 character?
Ans: 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.
10.What is a C token?
he smallest individual units in C are known as C tokens. There are six types of tokens in C,
Keywords
Constants
Identifiers
Strings
Operators
Special symbols.
11. What is a C keyword?
Predefined reserved words with fixed meanings are called keywords. Keywords are the
building blocks for program statements.
12.How many reserved keywords are there in C?
The names of variables, functions, and arrays are referred to as identifiers. These are user-
defined names consisting of a sequence of letters and digits. Identifiers must be unique.
A variable is a user-defined name given to memory locations that can be used to store data. A
variable may take different values at different times during the execution of a program.
The part of the code where a declared variable can be accessed directly is called the scope of
the variable.
Fixed values that do not change throughout program execution are called constants.
Derived types are data types that are derived from fundamental data types. Arrays, pointers,
function types, structures, and so on are examples.
User-defined datatypes – structures, unions, enum.
Assignment Operator += -= *= /= %=
Conditional Operator ?:
The modulo division (%) operator produces the remainder of an integer division.
== is the comparison operator used to check whether the value or expression on both the side
are same (return 0) or not ((return 1)
The process of executing a sequence of statements repeatedly until some conditions for
termination of the loop is satisfied is called looping.
The
break
statement can be used to accomplish an early exit from a loop. When a
break
statement is encountered inside a loop, the loop is immediately exited and the program
continues with the statement immediately following the loop.
25. What is the use of the continue statement?
The
continue
statement is used to skip a part of a loop. It causes the loop to be continued with the next
iteration after skipping any statements in between.
26. What is an array?
An Array is a collection of variables of a similar type that are referenced by a common name.
There are three types of arrays, namely,
One Dimensional Array
Two Dimensional Array
Multi DImensional Array
27. Define String?
A sequence of characters that are treated as a single data item is called a string.
28. What is the important string handling functions in C?
strcpy()
– String assignment function.
strlen()
– Returns the number of characters in the string.
strcmp()
– Compares two strings.
strcat()
– Concatenate two strings.
strstr()
– Used to locate substring.
29. What is a function?
Ans:
Actual arguments cannot be changed and remain Operations are performed on actual arguments,
Safety
safe hence not safe
Memory Separate memory locations are created for actual Actual and Formal arguments share the same
Location and formal arguments memory space.
Arguments Copy of actual arguments are sent Actual arguments are passed
What are the different storage class specifiers in C?
auto
register
static
extern
BASIS FOR
WHILE DO-WHILE
COMPARISON
} while( Condition );
Controlling Condition In 'while' loop the controlling condition In 'do-while' loop the controlling condition
appears at the start of the loop. appears at the end of the loop.
Iterations The iterations do not occur if, the The iteration occurs at least once even if the
condition at the first iteration, appears condition is false at the first iteration.
false.
32. What is the difference between library functions and user-defined functions?
The functions that are already defined in the C library are called library functions(Built-in
functions). They are stored in different header files and these functions cannot be modified by
the user. To use a library function in a program, the user has to include the corresponding
header file in the program. Some of the library functions in C include
33. What is the difference between a formal parameter and an actual parameter?
The parameters used in function definitions are called formal parameters and those used in
function calls are called actual parameters. The formal parameters list declares the variables
that will receive the data sent by the calling program.
All local variables that are declared inside a function are known as auto variables unless not
specified. That is, by default a local variable is an auto variable. An auto variable is created
each time when the function is called and destroyed when the program’s execution leaves the
function.
A static variable is similar to an automatic variable. A static variable is declared once and
only destroys when the execution of the program finishes.
AUTOMATIC VARIABLES STATIC VARIABLES
All local variables are automatic by default. Using the keyword static keyword must be used to declare a static
auto is optional. varibale.
The scope of an automatic variable is always local to the function The scope of an static variable is always local to the
in which it is declared. function.
Automatic variables are destroyed when the execution of the The value of a static variable persists until the end of
function is completed. the program.
The region of a program in which a variable is available for use is called the scope of the
variable.
The ability of a program to access a variable from the memory is the visibility of a variable.
The duration of time in which a variable exists in the memory during execution is the lifetime
of a variable.
An array is a collection of data elements of the same type. Structures can have elements of
different types.
An array can be used as a built-in datatype. All we have to do is declare and use the array.
But in the case of a structure, we have to design and declare it as a data structure before the
structure variables are declared and used.
Both structure and union are user-defined data types in C. The major difference between a
structure and a union is in terms of storage. In structures, each member has its own storage
location, but all the members of a union share the same location. That is a union can handle
only one member at a time.
The total size of the structure is the sum of the size of every data member. The total size of a
union is the size of the largest data member.
39. What is meant by nested structures and arrays of structures?
The individual members of a structure can be other structures as well, such structure is called
a nested structure. That is, a structure may contain another structure as its member.
It is possible to declare an array of structures. The array will have individual structures as its
elements.
Pointers are a special type of variable that is used to store the address of another variable as
their values.
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 if statement gives the user the choice of executing a statement (possibly compound) if
the expression is evaluated to true or skipping it is the expression is evaluated to false.
Syntax: An if statement in C is generally in the following form:
if (expression)
{ Statement; }
C language also lets one choose between two statements by using the if-else structure.
Syntax: if (expression)
{ Statement 1 }
Else
{ Statement 2 }
In this case, if the expression is true, then the statement 1 is executed. Otherwise, statement 2
is executed.
C functions are used to avoid the rewriting the same code again and again in our
program.
C functions can be called any number of times from any place of our program.
When a program is divided into functions, then any part of our program can easily be
tracked.
C functions provide the reusability concept, i.e., it breaks the big task into smaller
tasks so that it makes the C program more understandable.
Following are the differences between a call by value and call by reference are:
An Array is declared similar to how a variable is declared, but you need to add [] after
the type.
Example: int [] intArray;
Advantages:
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.
Disadvantages:
We 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.
50. Can you declare an array without assigning the size of an array?
No we cannot declare an array without assigning size.
Structure
Structure is a user defined datatype. It is used to combine different types of data into a single
type. It can have multiple members and structure variables. The keyword “struct” is used to
define structures in C language. Structure members can be accessed by using dot(.) operator.
Here is the syntax of structures in C language,
struct structure_name {
member definition;
} structure_variables;
Union
Union is also a user defined datatype. All the members of union share the same memory
location. Size of union is decided by the size of largest member of union. If you want to use
same memory location for two or more members, union is the best for that.
Unions are similar to the structure. Union variables are created in same manner as structure
variables. The keyword “union” is used to define unions in C language.
Here is the syntax of unions in C language,
union union_name {
member definition;
} union_variables;
Array
It refers to a collection that consists of elements of homogenous/same data
type.
It uses subscripts/ ‘[ ]’ (square brackets) to access the elements.
It is a pointer that points to the first element of the collection.
The array objects can’t be instantiated.
The size of array is fixed based on the number of elements in the array.
This size is the product of number of elements and size of every element.
Bit field is not possible in an array.
It is considered as a primitive data type.
Traversing through and searching for elements in an array is quick and easy.
It can be declared using the ‘[ ]’.
Array size is fixed and is basically the number of elements multiplied by the
size of an element.
It is stored in contiguous memory locations.
Example
data_type array_name[size];
Structure
It is a collection that consists of elements of heterogenous/dissimilar data
types.
It uses the ‘.’ (dot operator) to access the elements.
It isn’t a pointer.
It can be instantiated.
The size is not fixed.
This is because the elements in a structure can be of different data types and
sizes.
Bit field is possible to create in a structure.
It can be declared using the ‘struct’ keyword.
It is a user-defined datatype.
Traversing and searching through a structure is slow and complex.
They may or may not be stored in a contiguous memory location.
Example
struct sruct_name {
data_type1 ele1;
data_type2 ele2;
};
_