Question Answer in C Module -I _copy
Question Answer in C Module -I _copy
C Programming
1. Why is C called a mid-level programming language?
Due to its ability to support both low-level and high-level features, C is considered a middle-level
language. It is both an assembly-level language, i.e. a low-level language, and a higher-level
language. Programs that are written in C are converted into assembly code, and they support
pointer arithmetic (low-level) while being machine-independent (high-level). Therefore, C is
often referred to as a middle-level language. C can be used to write operating systems and
menu-driven consumer billing systems.
Sanjoy Ghosh | 1 of 39
C Question Answer | Sanjoy Ghosh
● User Defined data types: These data types are defined by the user to make the program
more readable.
● Derived data types: Data types that are derived from primitive or built-in data types.
Data Types in C
The Basic Datatypes supported in C Language are as follows:
Datatype Name Datatype Size Datatype Range
Sanjoy Ghosh | 2 of 39
C Question Answer | Sanjoy Ghosh
Sanjoy Ghosh | 3 of 39
C Question Answer | Sanjoy Ghosh
Processor in C
Output
Initial value of static variable 0
Initial value of variable without static 0
Sanjoy Ghosh | 4 of 39
C Question Answer | Sanjoy Ghosh
Number of
It only takes one argument. It takes two arguments.
arguments
Sanjoy Ghosh | 5 of 39
C Question Answer | Sanjoy Ghosh
9. What do you mean by dangling pointers and how are dangling pointers
different from memory leaks in C programming?
Pointers pointing to deallocated memory blocks in C Programming are known as dangling
pointers i.e, whenever a pointer is pointing to a memory location and In case the variable is
deleted and the pointer still points to that same memory location then it is known as a dangling
pointer variable.
In C programming memory leak occurs when we allocate memory with the help of the malloc()
or calloc() library function, but we forget to free the allocated memory with the help of the free()
library function. Memory leak causes the program to use an undefined amount of memory from
the RAM which makes it unavailable for other running programs this causes our program to
crash.
10. Write a program to convert a number to a string with the help of sprintf()
function in the C library.
C
// C program to convert number to
// string using sprintf()
#include <stdio.h>
#include <string.h>
// Driver code
int main()
{
char res[20];
float a = 32.23;
sprintf(res, "%f", a);
printf("\nThe string for the num is %s", res);
return 0;
}
Output
The string for the num is 32.230000
Sanjoy Ghosh | 6 of 39
C Question Answer | Sanjoy Ghosh
12. What is the difference between the local and global variables in C?
Local variables are declared inside a block or function but global variables are declared outside
the block or function to be accessed globally.
The life of the local variables is destroyed The life of the global variable exists until
after the block or a function. the program is executed.
Variables are stored inside the stack unless The storage location of the global
they are specified by the programmer. variable is decided by the compiler.
Sanjoy Ghosh | 7 of 39
C Question Answer | Sanjoy Ghosh
Working of Pointer
Sanjoy Ghosh | 8 of 39
C Question Answer | Sanjoy Ghosh
15. What are loops and how can we create an infinite loop in C?
Loops are used to execute a block of statements repeatedly. The statement which is to be
repeated will be executed n times inside the loop until the given condition is reached. There are
two types of loops Entry controlled and Exit-controlled loops in the C programming language. An
Infinite loop is a piece of code that lacks a functional exit. So, it repeats indefinitely. There can
be only two things when there is an infinite loop in the program. One it was designed to loop
endlessly until the condition is met within the loop. Another can be wrong or unsatisfied break
conditions in the program.
Types of Loops
Below is the program for infinite loop in C:
C
// C program for infinite loop
// using for, while, do-while
#include <stdio.h>
// Driver code
int main()
{
Sanjoy Ghosh | 9 of 39
C Question Answer | Sanjoy Ghosh
for (;;) {
printf("Infinite-loop\n");
}
while (1) {
printf("Infinite-loop\n");
}
do {
printf("Infinite-loop\n");
} while (1);
return 0;
}
16. What is the difference between type casting and type conversion?
It can be applied to both compatible data Type conversion can only be applied to only
types as well as incompatible data types. compatible data types.
Sanjoy Ghosh | 10 of 39
C Question Answer | Sanjoy Ghosh
Syntax: Syntax:
destination_data_type = (target_data_type) int a = 20; float b; b = a; // a = 20.0000
variable_to_be_converted;
Sanjoy Ghosh | 11 of 39
C Question Answer | Sanjoy Ghosh
Macro Function
Code length is increased using macro. Code length remains unaffected using function.
Execution speed using a macro is faster. Execution speed using function is slower.
Sanjoy Ghosh | 12 of 39
C Question Answer | Sanjoy Ghosh
The macro name is replaced by the macro Transfer of control takes place during the
value before compilation. function call.
Sanjoy Ghosh | 13 of 39
C Question Answer | Sanjoy Ghosh
// Driver code
int main()
{
struct student obj;
strcpy(obj.name, "Kamlesh_Joshi");
obj.roll_no = 27;
strcpy(obj.address, "Haldwani");
strcpy(obj.branch, "Computer Science And Engineering");
return 0;
}
Output
Name: Kamlesh_Joshi
Roll_No: 27
Address: Haldwani
Branch: Computer Science And Engineering
Sanjoy Ghosh | 14 of 39
C Question Answer | Sanjoy Ghosh
Sanjoy Ghosh | 15 of 39
C Question Answer | Sanjoy Ghosh
Sanjoy Ghosh | 16 of 39
C Question Answer | Sanjoy Ghosh
int main()
{
enum week day;
day = Wed;
printf("%d", day);
return 0;
}
Output
2
In the above example, we declared “day” as the variable, and the value of “Wed” is allocated to
day, which is 2. So as a result, 2 is printed.
Sanjoy Ghosh | 17 of 39
C Question Answer | Sanjoy Ghosh
29. Write a C program to print the Fibonacci series using recursion and
without using recursion.
Fibonacci Numbers
C
// C program to print Fibonacci Series
// with recursion and without recursion
#include <stdio.h>
// Driver code
int main()
Sanjoy Ghosh | 18 of 39
C Question Answer | Sanjoy Ghosh
{
int num;
printf(
"Fibonacci Series with the help of Recursion:\n");
Fibonacci(num - 2, 0, 1, 0);
first = second;
second = third;
}
return 0;
}
Output:
Please Enter number of Elements: 5
Fibonacci Series with the help of Recursion:
01123
Fibonacci Series without Using Recursion:
01123
Sanjoy Ghosh | 19 of 39
C Question Answer | Sanjoy Ghosh
// Driver code
int main()
{
int num;
int check = 1;
Sanjoy Ghosh | 20 of 39
C Question Answer | Sanjoy Ghosh
if (num <= 1) {
check = 0;
}
if (check == 1) {
printf("%d is a prime number", num);
}
else {
printf("%d is not a prime number", num);
}
return 0;
}
Sanjoy Ghosh | 21 of 39
C Question Answer | Sanjoy Ghosh
Source code can be easily modified and Object code cannot be modified and
contains less number of statements than contains more statements than source
object code. code.
Source code can be changed over time and Object code can be modified and is system
is not system specific. specific.
Source code is less close to the machine Object code is more close to the machine
and is input to the compiler or any other and is the output of the compiler or any
translator. other translator.
Sanjoy Ghosh | 22 of 39
C Question Answer | Sanjoy Ghosh
● Dynamic memory allocation: Memory allocation done at execution or run time is known
as dynamic memory allocation. Dynamic memory allocation is slower than static memory
allocation as memory allocation is done from the heap. This memory allocation method
is more efficient as compared to static memory allocation. It is mostly preferred in the
linked list.
// Driver code
int main()
{
int num = 20;
printf("Value of num before passing is: %d\n", num);
return 0;
}
Sanjoy Ghosh | 23 of 39
C Question Answer | Sanjoy Ghosh
// Driver code
int main()
{
// This will print the hello-world
// on the screen without giving any error
if (printf(“Hello - World”)) {
}
return 0;
}
Sanjoy Ghosh | 24 of 39
C Question Answer | Sanjoy Ghosh
38. Write a C program to swap two numbers without using a third variable.
int main()
{
// Variable declaration
int var1 = 50;
int var2 = 60;
printf(
"Values before swap are var1 = %d and var2 = %d\n",
var1, var2);
Sanjoy Ghosh | 25 of 39
C Question Answer | Sanjoy Ghosh
var1, var2);
return 0;
}
Output
Values before swap are var1 = 50 and var2 = 60
Values after swap are var1 = 60 and var2 = 50
// Driver code
int main()
{
Palindrome("abba");
return 0;
}
Sanjoy Ghosh | 26 of 39
C Question Answer | Sanjoy Ghosh
Output
abba is a Palindrome
41. Write a program to print the factorial of a given number with the help of
recursion.
Factorial of a Number
C
// C program to find factorial
// of a given number
#include <stdio.h>
Sanjoy Ghosh | 27 of 39
C Question Answer | Sanjoy Ghosh
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
// Driver code
int main()
{
int num = 5;
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}
Output
Factorial of 5 is 120
// Driver code
int main()
{
int n;
int var = n;
int sum = 0;
Sanjoy Ghosh | 28 of 39
C Question Answer | Sanjoy Ghosh
Output
Enter Number
0 is an Armstrong number
// Driver code
int main()
{
int n, rev = 0;
return 0;
Sanjoy Ghosh | 29 of 39
C Question Answer | Sanjoy Ghosh
Output:
Enter Number to be reversed :
Number After reversing digits is: 321
Sanjoy Ghosh | 30 of 39
C Question Answer | Sanjoy Ghosh
File Operations in C
Sanjoy Ghosh | 31 of 39
C Question Answer | Sanjoy Ghosh
};
int isCircular(struct Node* head)
{
int main()
{
Sanjoy Ghosh | 32 of 39
C Question Answer | Sanjoy Ghosh
return 0;
}
Sanjoy Ghosh | 33 of 39
C Question Answer | Sanjoy Ghosh
/* UTILITY FUNCTIONS */
/* MoveNode() function takes the node
from the front of the source, and
move it to the front of the dest.
It is an error to call this with the
source list empty.
Sanjoy Ghosh | 34 of 39
C Question Answer | Sanjoy Ghosh
Sanjoy Ghosh | 35 of 39
C Question Answer | Sanjoy Ghosh
push(&b, 20);
push(&b, 3);
push(&b, 2);
return 0;
}
Output
Merged Linked List is:
2 3 5 10 15 20
Sanjoy Ghosh | 36 of 39
C Question Answer | Sanjoy Ghosh
50. What is the difference between getc(), getchar(), getch() and getche().
● getc(): The function reads a single character from an input stream and returns an integer
value (typically the ASCII value of the character) if it succeeds. On failure, it returns the
EOF.
● getchar(): Unlike getc(), gechar() can read from standard input; it is equivalent to
getc(stdin).
● getch(): It is a nonstandard function and is present in ‘conio.h’ header file which is mostly
used by MS-DOS compilers like Turbo C.
● getche(): It reads a single character from the keyboard and displays it immediately on
the output screen without waiting for the enter key.
Q51. What are the limitations of scanf() and how can it be avoided?
Q52. What are the valid places where the programmer can apply Break Control
Statement?
Ans: Break Control statement is valid to be used inside a loop and Switch control
statements.
Ans: To store a negative integer, we need to follow the following steps. Calculate the
two’s complement of the same positive integer.
Sanjoy Ghosh | 37 of 39
C Question Answer | Sanjoy Ghosh
Ans: The Parameters which are sent from main function to the subdivided function are
called as Actual Parameters and the parameters which are declared at the Subdivided
function end are called as Formal Parameters. Give example
Sanjoy Ghosh | 38 of 39
C Question Answer | Sanjoy Ghosh
Reference:
1. https://www.geeksforgeeks.org/
2. https://www.javatpoint.com/
3. https://stackoverflow.com/
4. https://www.edureka.co
Sanjoy Ghosh | 39 of 39