C Language Questions and Answers: Visit For More: WWW - Learnengineering.In
C Language Questions and Answers: Visit For More: WWW - Learnengineering.In
C Language Questions and Answers: Visit For More: WWW - Learnengineering.In
in
1. What is C language?
C is a programming language developed at AT&T's Bell Laboratories of USA in 1972. The C
programming language is a standardized programming language developed in the early
1970s by Ken Thompson and Dennis Ritchie for use on the UNIX operating system. It has
since spread to many other operating systems, and is one of the most widely used
programming languages.
4. What is a pointer?
Pointers are variables which stores the address of another variable. That variable may be a
scalar (including another pointer), or an aggregate (array or structure). The pointed-to
object may be part of a larger object, such as a field of a structure or an element in an
array.
5. What is an array?
Array is a variable that hold multiple elements which has the same data type.
64
21. What is the difference between a string copy (strcpy) and a memory copy (memcpy)?
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 () 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 you want to copy from the source to the destination.
22. What is the difference between const char*p and char const* p? const char*p - p is
pointer to the constant character. i.e value in that address location is constant.
const char* const p - p is the constant pointer which points to the constant string, both
value and address are constants.
65
30. Differentiate between for loop and a while loop? What are it uses?
For executing a set of statements fixed number of times we use for loop while when the
number of iterations to be performed is not known in advance we use while loop.
31. What is the difference between printf(...) and sprintf(...)? printf(....) -------------> is
standard output statement sprintf(......)-----------> is formatted output statement.
66
A conversion constructor declared with the explicit keyword. The compiler does not use an
explicit constructor to implement an implied conversion of types. Explicit constructors are
simply constructors that cannot take part in an implicit conversion.
37. What are macros? What are its advantages and disadvantages?
Macros are abbreviations for lengthy and frequently used statements. When a macro is
called the entire code is substituted by a single line though the macro definition is of
several lines.
The advantage of macro is that it reduces the time taken for control transfer as in case of
function. The disadvantage of it is here the entire code is substituted so the program
becomes lengthy if a macro is called several times.
38. What are register variables? What are the advantages of using register variables? If a
variable is declared with a register storage class, it is known as register variable. The
register variable is stored in the CPU register instead of main memory. Frequently used
variables are declared as register variable as it’s access time is faster.
39. What is storage class? What are the different storage classes in C?
Storage class is an attribute that changes the behavior of a variable. It controls the
lifetime,scope and linkage. The storage classes in c are auto, register, and extern, static,
typedef.
67
41. In C, why is the void pointer useful? When would you use it?
The void pointer is useful because it is a generic pointer that any pointer can be cast into
and back again without loss of information.
42. What is the difference between the functions memmove() and memcpy()? The
arguments of memmove() can overlap in memory. The arguments of memcpy() cannot.
68
Enumerated types allow the programmers to use more meaningful words as values to a
variable.
Each item in the enumerated type variable is actually associated with a numeric code.
69
1- 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.
scanf(): The scanf() function is used to take input from the user.
8- What is the difference between the local variable and global variable in C?
Following are the differences between a local variable and global variable:
Basis for
Local variable Global variable
comparison
A variable which is declared inside A variable which is declared outside
Declaration function or block is known as a local function or block is known as a
variable. global variable.
The scope of a variable is available within The scope of a variable is available
Scope
a function in which they are declared. throughout the program.
Variables can be accessed only by those
Any statement in the entire program
Access statements inside a function in which
can access variables.
they are declared.
Life of a variable is created when the
Life of a variable exists until the
Life function block is entered and destroyed
program is executing.
on its exit.
Variables are stored in a stack unless The compiler decides the storage
Storage
specified. location of a variable.
9- What is the use of a static variable in C?
Following are the uses of a static variable:
A variable which is declared as static is known as a static variable. The static variable
retains its value between multiple function calls.
Static variables are used because the scope of the static variable is available in the
entire program. So, we can access a static variable anywhere in the program.
The static variable is initially initialized to zero. If we update the value of a variable, then
the updated value is assigned.
The static variable is used as a common value which is shared by all the methods.
The static variable is initialized only once in the memory heap to reduce the memory
usage.
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.
11- What is the difference between call by value and call by reference in C?
Following are the differences between a call by value and call by reference are:
Output:
Value of a is: 10
Value of b is: 20
Example of call by reference:
1. #include <stdio.h>
2. void change(int*,int*);
3. int main()
4. {
5. int a=10,b=20;
6. change(&a,&b); // calling a function by passing references of variables.
7. printf(“Value of a is: %d”,a);
8. printf(“\n”);
9. printf(“Value of b is: %d”,b);
10. return 0;
11. }
12. void change(int *x,int *y)
13. {
14. *x=13;
15. *y=17;
16. }
Output:
Value of a is: 13
Value of b is: 17
12- What is recursion in C?
When a function calls itself, and this process is known as recursion. The function that
calls itself is known as a recursive function.
1. Winding phase
2. Unwinding phase
Winding phase: When the recursive function calls itself, and this phase ends when the
condition is reached.
Unwinding phase: Unwinding phase starts when the condition is reached, and the
control returns to the original call.
Example of recursion
1. #include <stdio.h>
2. int calculate_fact(int);
3. int main()
4. {
5. int n=5,f;
6. f=calculate_fact(n); // calling a function
7. printf(“factorial of a number is %d”,f);
8. return 0;
9. }
10. int calculate_fact(int a)
11. {
12. if(a==1)
13. {
14. return 1;
15. }
16. else
17. return a*calculate_fact(a-1); //calling a function recursively.
18. }
Output:
Syntax:
1. data_type array_name[size];
Syntax:
1. data_type array_name[size];
Example of an array:
1. #include <stdio.h>
2. int main()
3. {
4. int arr[5]={1,2,3,4,5}; //an array consists of five integer values.
5. for(int i=0;i<5;i++)
6. {
7. printf(“%d “,arr[i]);
8. }
9. return 0;
10. }
Output:
12345
14- What is a pointer in C?
A pointer is a variable that refers to the address of a value. It makes the code
optimized and makes the performance fast. Whenever a variable is declared inside a
program, then the system allocates some memory to a variable. The memory contains
some address number. The variables that hold this address number is known as the
pointer variable.
For example:
1. Data_type *p;
The above syntax tells that p is a pointer variable that holds the address number of a
given data type value.
Example of pointer
1. #include <stdio.h>
2. int main()
3. {
4. int *p; //pointer of type integer.
5. int a=5;
6. p=&a;
7. printf(“Address value of ‘a’ variable is %u”,p);
8. return 0;
9. }
Output:
Accessing array elements: Pointers are used in traversing through an array of integers
and strings. The string is an array of characters which is terminated by a null character
‘\0’.
Dynamic memory allocation: Pointers are used in allocation and deallocation of
memory during the execution of a program.
Call by Reference: The pointers are used to pass a reference of a variable to other
function.
Data Structures like a tree, graph, linked list, etc.: The pointers are used to construct
different data structures like tree, graph, linked list, etc.
If a pointer is pointing any memory location, but meanwhile another pointer deletes
the memory occupied by the first pointer while the first pointer still points to that
memory location, the first pointer will be known as a dangling pointer. This problem is
known as a dangling pointer problem.
Dangling pointer arises when an object is deleted without modifying the value of the
pointer. The pointer points to the deallocated memory.
1. #include<stdio.h>
2. void main()
3. {
4. int *ptr = malloc(constant value); //allocating a memory space.
5. free(ptr); //ptr becomes a dangling pointer.
6. }
In the above example, initially memory is allocated to the pointer variable ptr, and then
the memory is deallocated from the pointer variable. Now, pointer variable, i.e., ptr
becomes a dangling pointer.
The problem of a dangling pointer can be overcome by assigning a NULL value to the
dangling pointer. Let’s understand this through an example:
1. #include<stdio.h>
2. void main()
3. {
4. int *ptr = malloc(constant value); //allocating a memory space.
5. free(ptr); //ptr becomes a dangling pointer.
6. ptr=NULL; //Now, ptr is no longer a dangling pointer.
7. }
In the above example, after deallocating the memory from a pointer variable, ptr is
assigned to a NULL value. This means that ptr does not point to any memory location.
Therefore, it is no longer a dangling pointer.
1. #include <stdio.h>
2. int main()
3. {
4. int a=10;
5. int *ptr,**pptr; // *ptr is a pointer and **pptr is a double pointer.
6. ptr=&a;
7. pptr=&ptr;
8. printf(“value of a is:%d”,a);
9. printf(“\n”);
10. printf(“value of *ptr is : %d”,*ptr);
11. printf(“\n”);
12. printf(“value of **pptr is : %d”,**pptr);
13. return 0;
14. }
In the above example, pptr is a double pointer pointing to the address of the ptr
variable and ptr points to the address of ‘a’ variable.
In case of static memory allocation, memory is allocated at compile time, and memory
can’t be increased while executing the program. It is used in the array.
The lifetime of a variable in static memory is the lifetime of a program.
The static memory is allocated using static keyword.
The static memory is implemented using stacks or heap.
The pointer is required to access the variable present in the static memory.
The static memory is faster than dynamic memory.
In static memory, more memory space is required to store the variable.
1. For example:
2. int a[10];
The above example creates an array of integer type, and the size of an array is fixed,
i.e., 10.
In case of dynamic memory allocation, memory is allocated at runtime and memory can
be increased while executing the program. It is used in the linked list.
The malloc() or calloc() function is required to allocate the memory at the runtime.
An allocation or deallocation of memory is done at the execution time of a program.
No dynamic pointers are required to access the memory.
The dynamic memory is implemented using data segments.
Less memory space is required to store the variable.
1. For example
2. int *p= malloc(sizeof(int)*10);
22- What functions are used for dynamic memory allocation in C language?
1. malloc()
o The malloc() function is used to allocate the memory during the execution of
the program.
o It does not initialize the memory but carries the garbage value.
o It returns a null pointer if it could not be able to allocate the requested space.
Syntax
Syntax
1. ptr = realloc(ptr, newsize); // updating the memory size using realloc() function.
2. free():The free() function releases the memory allocated by either calloc() or malloc()
function.
In this example Name of the function is Sum, the return type is the integer data type and it
accepts two integer parameters.
Q #13) What is the explanation for the cyclic nature of data types in C?
Answer: Some of the data types in C have special characteristic nature when a developer
assigns value beyond the range of the data type. There will be no compiler error and the
value changes according to a cyclic order. This is called cyclic nature. Char, int, long int
data types have this property. Further float, double and long double data types do not
have this property.
Q #14) Describe the header file and its usage in C programming?
Answer: 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.
Q #15) There is a practice in coding to keep some code blocks in comment symbols
than delete it when debugging. How this affects when debugging?
Answer: This concept is called commenting out and this is the way to isolate some part of
the code which scans possible reason for the error. Also, this concept helps to save time
because if the code is not the reason for the issue it can simply be removed from
comment.
Q #16) What are the general description for loop statements and available loop
types in C?
Answer: A statement that allows the execution of statements or groups of statements in a
repeated way is defined as a loop.
The following diagram explains a general form of a loop.
Answer:
#include <stdio.h>
int main () {
int a;
int b;
/* for loop execution */
for( a = 1; a < 6; a++ )
{
/* for loop execution */
for ( b = 1; b <= a; b++ )
{
printf("%d",b);
}
printf("\n");
}
return 0;
}
Q #26) Explain the use of function toupper() with an example code?
Answer: Toupper() function is used to convert the value to uppercase when it used with
characters.
Code:
#include <stdio.h>
#include <ctype.h>
int main()
{
char c;
c = 'a';
printf("%c -> %c", c, toupper(c));
c = 'A';
printf("\n%c -> %c", c, toupper(c));
c = '9';
printf("\n%c -> %c", c, toupper(c));
return 0;
}
Result:
Q #27) What is the code in a while loop that returns the output of the given code?
#include <stdio.h>
int main () {
int a;
return 0;
}
Answer:
#include <stdio.h>
int main () {
int a;
while (a<=100)
{
printf ("%d\n", a * a);
a++;
}
return 0;
}
Q #28) Select the incorrect operator form in the following list(== , <> , >= , <=) and
what is the reason for the answer?
Answer: Incorrect operator is ‘<>’. This format is correct when writing conditional
statements, but it is not the correct operation to indicate not equal in C programming. It
gives a compilation error as follows.
Code:
#include <stdio.h>
int main () {
if ( 5 <> 10 )
printf( "test for <>" );
return 0;
}
Error:
Q #29) Is it possible to use curly brackets ({}) to enclose a single line code in C
program?
Answer: Yes, it works without any error. Some programmers like to use this to organize
the code. But the main purpose of curly brackets is to group several lines of codes.
Q #30) Describe the modifier in C?
Answer: Modifier is a prefix to the basic data type which is used to indicate the
modification for storage space allocation to a variable.
Example– In a 32-bit processor, storage space for the int data type is 4.When we use it
with modifier the storage space change as follows:
Long int: Storage space is 8 bit
Short int: Storage space is 2 bit
Q #31) What are the modifiers available in C programming language?
Answer: There are 5 modifiers available in the C programming language as follows:
Short
Long
Signed
Unsigned
long long
Q #32) What is the process to generate random numbers in C programming
language?
Answer: The command rand() is available to use for this purpose. The function returns an
integer number beginning from zero(0). The following sample code demonstrates the use
of rand().
Code:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int a;
int b;
Output:
printf("String 01 \n");
printf("String 02 \n");
return 0;
}
Output:
What are the valid places for the keyword break to appear.
Break can appear only with in the looping control and switch statement. The purpose
of the break is to bring the control out from the said blocks.
What is difference between including the header file with-in angular braces < > and
double quotes “ “
If a header file is included with in < > then the compiler searches for the particular
header file only with in the built in include path. If a header file is included with in “ “,
then the compiler searches for the particular header file first in the current working
directory, if not found then in the built in include path.
int i = 20;
S++ or S = S+1, which can be recommended to increment the value by 1 and why?
S++, as it is single machine instruction (INC) internally.
What is a token?
A C program consists of various tokens and a token is either a keyword, an identifier,
a constant, a string literal, or a symbol.
What is a preprocessor?
Preprocessor is a directive to the compiler to perform certain things before the actual
compilation process begins.
How can you print a \ (backslash) using any of the printf() family of functions.
Escape it using \ (backslash).
How many operators are there under the category of ternary operators?
There is only one operator and is conditional operator (? : ).
What is a pointer to a function? Give the general syntax for the same.
A pointer holding the reference of the function is called pointer to a function. In general
it is declared as follows.
T (*fun_ptr) (T1,T2…); Where T is any date type.
Once fun_ptr refers a function the same can be invoked using the pointer as follows.
fun_ptr();
[Or]
(*fun_ptr)();
Which compiler switch to be used for compiling the programs using math library with
gcc compiler?
Opiton –lm to be used as > gcc –lm <file.c>
Which operator is used to continue the definition of macro in the next line?
Backward slash (\) is used.
E.g. #define MESSAGE "Hi, \
Welcome to C"
Which operator is used to receive the variable number of arguments for a function?
Ellipses (…) is used for the same. A general function definition looks as follows
void f(int k,…) {
}
Which built-in library function can be used to re-size the allocated dynamic memory?
realloc().
Define an array.
Array is collection of similar data items under a common name.
Which built-in function can be used to move the file pointer internally?
fseek()
What is a variable?
A variable is the name storage.
Which operator can be used to determine the size of a data type or variable?
sizeof
void f() {
int var;
}
main() {
int var;
}
Which operator can be used to access union elements if union variable is a pointer
variable?
Arrow (->) operator.
Name the predefined macro which be used to determine whether your compiler is ANSI
standard or not?
__STDC__
What is typecasting?
Typecasting is a way to convert a variable/constant from one type to another type.
What is recursion?
Function calling itself is called as recursion.
What is the first string in the argument vector w.r.t command line arguments?
Program name.
How can we determine whether a file is successfully opened or not using fopen()
function?
On failure fopen() returns NULL, otherwise opened successfully.
Can a function return multiple values to the caller using return reserved word?
No, only one value can be returned to the caller.
What is a constant pointer?
A pointer which is not allowed to be altered to hold another address after it is holding
one.
Which built-in library function can be used to match a patter from the string?
Strstr()
Which control loop is recommended if you have to execute set of statements for fixed
number of times?
for – Loop.
What is a constant?
A value which cannot be modified is called so. Such variables are qualified with the
keyword const.
Can we use just the tag name of structures to declare the variables for the same?
No, we need to use both the keyword ‘struct’ and the tag name.