C LYQ
C LYQ
>Stack is a linear data structure that follows a particular order in which the operations are performed.
c) Differentiate between iteration and recursion?
> Recursion is when a function calls itself within its code, thus repeatedly executing the instructions present inside it. Iteration is when
a loop repeatedly executes the set of instructions like "for" loops and "while" loops.
g) What is difference between structure and union?
> A structure is a custom data type that holds multiple members of different data type under a single unit where union is a user defined
data type that compile object of different data type in the exact memory location.
h) What is pointer to pointer? Give example.
> A pointer is a variable which stores the address of another variable. A pointer to pointer is a form of multiple indirection, or a chain of
pointers.
i) Assume y=10. What will be the value of x after execution of the expression x=5*y++;
>
j) What is the output of the following program segment?
{
int x=2, y=3, z=4;
x= y ==z;
printf("%d",x);
}
>
int *y=&x;
> A collection of data which is stored on a secondary device like a hard disk is known as a file. A file is generally used as real-life
> A file pointer stores the current position of a read or write within a file. All operations within the file are made with reference to the
pointer. The data type of this pointer is defined in stdio. h and is named FILE.
>A file pointer stores the current position of a read or write within a file. All operations within the file are made with reference to the
pointer. The data type of this pointer is defined in stdio. h and is named FILE.
>The string can be defined as the one-dimensional array of characters terminated by a null ('\0'). The character array
or the string is used to manipulate text such as word or sentences. Each character in the array occupies one byte of
memory, and the last character must always be 0.
39.Discuss the difference between ‘A’ and “A” in C.
> 'A' - Means a character 1 byte (8 bits) long containing A.
"A" - Means a string which is 2 bytes (16 bits) long which holds an A and a NULL character.\
40. The what are the primitive data types in C language.
> The primitive data types in the C language are the inbuilt data types provided by the C language itself. Thus, all C
compilers provide support for these data types. There are about seven primitive data types in C. These data types are
: short, int, long, char, float, double and a few of their variants.
LONG ANSWERS :
Recursion, in general, can be defined as the repetition of a process in a similar way until the specific condition reaches. In C
Programming, if a function calls itself from inside, the same function is called recursion. The function which calls itself is called a
recursive function, and the function call is termed a recursive call. While using recursion, make sure that it has a base (exit) condition;
otherwise, the program will go into the infinite loop.
Basic syntax of recursion:
void recursive_fun() //recursive function
{
Base_case; // Stopping Condition
(ii) When writing macros for functions, they saves a lot of time that is spent by the compiler for invoking / calling the functions.
Disadvantages : When the code is small then it is better to use macro. But when code is large then function
should be used.
*Compare and contrast and while and for loop with suitable examples.
o Initialization, condition checking, and increment or decrement of iteration variables are all done explicitly in the for loop
syntax only. In contrast, in the while loop, we can only initialise and check the condition in the loop syntax.
o When we know the number of iterations that must occur in a loop execution, we use the for loop. On the other hand, if we do
not know how many iterations must occur in a loop, we use a while loop.
o If you do not include a condition statement in the for loop, the loop will loop indefinitely. In contrast, failing to include a
o The initialization statement in the for loop syntax is only executed once at the beginning of the loop. If the while loop's syntax
includes an initialization statement, the initialization statement will be executed each time the loop iterates.
o The iteration statement in the for loop will run after the body of the for loop. On the contrary, because the iteration
statement can be written anywhere in the body of the while loop, there may be some statements that execute after the
iteration statement in the body of the while loop is executed.
o Example of for loop: Print the odd numbers within a given range. *Example of while loop: Reverse a number.
> Type casting refers to changing an variable of one data type into another. The compiler will automatically change
one type of data into another if it makes sense. For instance, if you assign an integer value to a floating-point
variable, the compiler will convert the int to a float. Casting allows you to make this type conversion explicit, or to
force it when it wouldn’t normally happen.
When the type conversion is performed automatically by the compiler without programmers intervention, such type
of conversion is known as implicit type conversion or type promotion.
int x;
for(x=97; x<=122; x++)
{
printf("%c", x); /*Implicit casting from int to char thanks to %c*/
}
The type conversion performed by the programmer by posing the data type of the expression of specific type is
known as explicit type conversion. The explicit type conversion is also known as type casting.
(data_type)expression;
where, data_type is any valid c data type, and expression may be constant, variable or expression.
For example,
int x;
for(x=97; x<=122; x++)
{
printf("%c", (char)x); /*Explicit casting from int to char*/
}
The following rules have to be followed while converting the expression from one type to another to avoid the loss
of information:
Example
If we want to get the exact value of 7/5 then we need explicit casting from int to float:
>Break : The break is a keyword in C which is used to bring the program control out of the loop. The break statement
is used inside loops or switch statement. The break statement breaks the loop one by one, i.e., in the case of nested
loops, it breaks the inner loop first and then proceeds to outer loops.
Example: int i;
Example : #include<stdio.h>
void main(){
char ch;
printf("B: Breakfast");
printf("L: Lunch");
printf("D: Dinner");
printf("E: Exit");
printf("Enter your choice:");
do{
ch = getchar();
switch (ch){
case 'B' :
printf ("time for breakfast");
break;
case 'L' :
printf ("time for lunch");
break;
case 'D' :
printf ("time for dinner");
break;
case 'E' :
exit(0); /* return to operating system */
}
} while (ch != 'B' && ch != 'L' && ch != 'D');
return 0;
}
Recursion Iteration
Infinite recursion occurs if the step in recursion doesn't reduce the problem to a An infinite loop occurs when the
smaller problem. It also becomes infinite recursion if it doesn't convert on a condition in the loop doesn't
specific condition. This specific condition is known as the base case. become False ever.
The system crashes when infinite recursion is encountered. Iteration uses the CPU cycles
again and again when an infinite
loop occurs.
Recursion terminates when the base case is met. Iteration terminates when the
condition in the loop fails.
Recursion is slower than iteration since it has the overhead of maintaining and Iteration is quick in comparison to
updating the stack. recursion. It doesn't utilize the
stack.
Recursion uses more memory in comparison to iteration. Iteration uses less memory in
comparison to recursion.
Recursion reduces the size of the code. Iteration increases the size of the
code.
#include<stdio.h>
#include<conio.h>
void main(){
int y;
clrscr();
scanf(“%d”,&y);
if(y%100==0){
if(y%400==0)
printf(“Leap year”);
else
else {
if(y%4==0)
printf(“Leap year”);
else
getch();
*What is the difference between system defined and user defined function in C.
> Library functions are the built-in functions i.e., they are predefined in the library of the C.On the other hand
user defined functions are designed by the user when they are writing any program because for every task we do
not have a library of functions where their definitions are predefined. To perform the according to
the requirement of user the user have to develop some functions by itself.
The difference between system defined and user defined functions are-
There is no such kind of requirement In this if the user wants to use a particular library function then the user
User-defined Functions Library Functions
have to add the particular library of that function in header file of the
to add the particular library. program.
#include <stdio.h>
int main () {
return 0;
}
void swap(int *x, int *y) {
int temp;
return;
}
While loop checks the condition first and then executes the statement(s), whereas do while loop will execute
the statement(s) at least once, then the condition is checked.
While loop is entry controlled loop, whereas do while is exit controlled loop.
In the while loop, we do not need to add a semicolon at the end of a while condition, but we need to add a
semicolon at the end of the while condition in the do-while loop.
While loop statement(s) is executed zero times if the condition is false, whereas the do-while statement is
executed at least once.
While loop allows initialization of counter variable before starting the body of a loop, whereas do while loop
allows initialization of counter variable before and after starting the body of a loop.
Example of while-
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1; //initializing the variable with value 1
while(num<=4) //while loop with condition
{
printf("%d\n",num);
num++; //incrementing operation
}
return 0;
}
Example of do while-
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1; //initializing the variable with value 1
do //do-while loop
{
printf("%d\n",2*num);
num++; //incrementing operation
} while(num<=4);
return 0;
}
What is getchar()?
The primary difference between the getchar() and getc() is that the getc() is capable of reading from any input
scheme, while the getchar() is capable of reading from the standard input. Hence, getchar() becomes equivalent to
the getc(stdin).
Here, Syntax:
int getchar(void);
What is getch()?
The getch() is a type of non-standard function which is present in the file of conio.h. Mostly, the MS-DOS compilers
utilize it, like the Turbo C. It does not fall into a part of the standard library of C or ISO C. It is also not defined by the
POSIX.
It is capable of reading a single character from any given keyboard. Since it doesn’t make use of any buffer, the
entered character returns immediately without having to wait for the enter key.
Here, Syntax:
int getch();
What is getche()?
Just like gentch(), the getche() is also a non-standard type of function that is present in the conio.h file. It is capable
of reading one character from any given keyboard and displaying it immediately on the output screen without the
character having to wait for the enter key.
Here, Syntax:
int getche(void);
Here are the differences between getc(), getchar(), getch() and getche():
Basics The getc() reads one The getchar() is The getch() is The getche() is capable
character from any input capable of reading capable of reading a of reading one
and then returns the from the standard single character character from any
corresponding value of the input. Hence, from any given given keyboard and
integer on success getchar() becomes keyboard. displaying it
(typically, the ASCII value equivalent to the Since it doesn’t immediately on the
of the read character). On getc(stdin). make use of any output screen without
failure, it then returns the buffer, the entered the character having to
EOF. character returns wait for the enter key.
immediately
without having to
wait for the enter
key.