0% found this document useful (0 votes)
6 views

C LYQ

The document provides an overview of various programming concepts in C, including data structures, recursion, pointers, and file handling. It explains differences between structures and unions, iteration and recursion, as well as various types of loops and memory allocation techniques. Additionally, it covers topics like type conversion, macros, and the role of compilers, offering definitions and examples for clarity.

Uploaded by

su0162004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

C LYQ

The document provides an overview of various programming concepts in C, including data structures, recursion, pointers, and file handling. It explains differences between structures and unions, iteration and recursion, as well as various types of loops and memory allocation techniques. Additionally, it covers topics like type conversion, macros, and the role of compilers, offering definitions and examples for clarity.

Uploaded by

su0162004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

*What is stack?

>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);
}

>

b) What is the difference between while and do-while statement?


>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 control loop, whereas do while is exit controlled loop.
c) What is pre-processor?
> A software program that process the source file before sending it to actual compilation is called pre-processor.
d) What is recursion?
> Recursion is the technique of making a function call itself. This technique provides a way to beak complicated problems down into
simple problems down into simple problems which are easier to solve.
e) What is data structure?
> A data structure is a storage that is used to store and organize data. It is a way of arranging data on a computer so that it can be
accessed and updated efficiently.
f) Write down the general syntax of malloc() statement.
> ptr(cast_type*)malloc(byte_size)
h) What is strictly binary tree? Give example.
>A binary tree in which every node has either two or zero number of children is called strictly binary tree.
1. What is array?
>An array is a variable that can store similar type multiple values. For example, if you want to store 100 integers, you can create an
array for it. int data[100];
2.What is static variable?
>In programming, a static variable is the one allocated “statically,” which means its lifetime is throughout the program run. It is
declared with the 'static' keyword and persists its value across the function calls.
3.What is data type?
>A data type specifies the type of data that a variable can store such as integer, floating, character, etc.
4.Difference between ++x and x++.
> ++X is the prefix increment operator; this means that the value of x is first incremented and then used in the program. X++ is the
postfix increment operator; this means that the value of x is first used in the program and then incremented.
5.What is pointer?
> A pointer is a variable that stores the address of another variable.
Ex. int x;

int *y=&x;

Here the address of x will be assigned to pointer ‘y’ variable.

6.What is pseudo code?


>The pseudocode in C is an informal way of writing a program for better human understanding. It is written in simple English, making
the complex program easier to understand. Pseudocode cannot be compiled or interpreted.
8.What is type casting?
>Type casting is the process in which the compiler automatically converts one data type in a program to another one. Type conversion
is another name for type casting.
9.What is function prototype?
> A function prototype is simply the declaration of a function that specifies function's name, parameters and return type. It doesn't
contain function body. A function prototype gives information to the compiler that the function may later be used in the program.
10.What is modular programming?
>Modular programming consists of separating implementation from interface and hiding information in the implementation. In C this is
achieved by placing the interface definition in a header file and the implementation in a source file.
11.What do you mean by pointer to a function?
>A pointer to a function points to the address of the executable code of the function. You can use pointers to call functions and to pass
functions as arguments to other functions.
12.What is dynamic memory allocation?
>The concept of dynamic memory allocation in c language enables the C programmer to allocate memory at runtime. Dynamic memory
allocation in c language is possible by 4 functions of stdlib.h header file.
i) malloc() ii) calloc() iii) realloc() iv) free()
13.What do you mean by procedure in the context of C ?
> A procedure is a small section of a program that performs a specific task. Procedures can be used repeatedly throughout a program.
14.What is the role of a compiler for a programming language?
> A compiler is a special program that translates a programming language's source code into machine code, bytecode or another
programming language.
15.Define variable in the context of C language.
> Variables are containers for storing data values, like numbers and characters. In C, there are different types of variables (defined with
different keywords), for example: int - stores integers (whole numbers), without decimals, such as 123 or -123.
16.What is the role of decision symbol of a flowchart?
> Decision symbol indicates a step that decides the next step in process. This is commonly a Yes/No or True/False question.
17. Define bit, byte, nibble and word.
> Bit- The smallest unit of data on a binary computer or digital system is a single bit. Bit can hold either 0 or 1.
Nibble- A Nibble is a collection of four bits.
Byte- A group of 8 bits is called a byte.
Word- A word is group of 16 bits.
18.Why do we use puts() in C language?
> The puts() function in C is used to write a line or string to the output stream. It prints the passed string with a newline and returns an
integer value.
19.Define ‘homogeneous’ in the context of array in C language.
> Arrays are classified as Homogeneous Data Structures because they store elements of the same type.
20. int **p; what do you mean by this C language statement?
> Here p is a pointer to pointer variable which stores an address of another pointer variable.
21.What is the difference between getch(), and getchar() in C?
> The getchar() is capable of reading from the standard input. Hence, getchar() becomes equivalent to the getc(stdin). The getch() is
capable of reading a single character from any given keyboard.
22.What is typedef in C language?
> The typedef is a keyword that is used in C programming to provide existing data types with a new name. typedef keyword is used to
redefine the name already the existing name.
23.Difference between compiler and interpreter.
> The major difference between a compiler and an interpreter is that a compiler takes the entire program in one go while an
interpreter, takes a single line at a time.
24.Difference between array and structure.
>One major difference between both of them is that- in an Array, the elements are of the same data type while a structure has
elements of different data types.
25.What is pointer to pointer in C.
> A pointer to a pointer is a a chain of pointers. Normally, a pointer contains the address of a variable. When we define a pointer to a
pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value.
26.What is pre-processor in C ?
> The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual
compilation.
27.Why do we include stdio.h in a C program ?
> The keyword stdio.h in C is a header file. The reason why we use stdio.h in C is that this header file imports different variables,
macros, and functions to perform input and output operations. To perform input and output operations in our C program, we need to
import the stdio.h header file into our program.
28.What do you mean by binary operator?
> Binary Operators in the C programming language are ones that operate on two operands. Arithmetic Operators, Relational
Operators, Logical Operators, Assignment Operators, and Bitwise Operators fall under the category of Binary Operators.
29.Write two built in functions in C.
> strlen() : It returns the string's length. strcmp(): It compares two strings and returns 0 if the strings are the same. strcat(): It
concatenates two strings and returns the concatenated string.
30.What is preincrement in C?
> A pre-increment operator is used to increment the value of a variable before using it in an expression. In the Pre-Increment, value is
first incremented and then used inside the expression.
31.Why flowchart is used for problem solving?
> A flowchart is a diagram depicting a process, a system or a computer algorithm. It is a diagrammatic representation of the solution to
a given problem but, more importantly, it provides a breakdown of the essential steps to solving the problem.
32.Explain precedence rule in C.
> The precedence of operators in C dictates the order in which the operators will be evolved in an expression. Associativity, on the
other hand, defines the order in which the operators of the same precedence will be evaluated in an expression. Also, associativity can
occur from either right to left or left to right.
33.What do you mean by void pointer?
> The void pointer in C is a pointer that is not associated with any data types. It points to some data location in the storage. This means
that it points to the address of variables. It is also called the general purpose pointer.
34. What is macro?
> A macro is a piece of code in a program that is replaced by the value of the macro. Macro is defined by #define directive.
35.What is File in C?

> 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

applications that contain a large amount of data.

36.What is file pointer?

> 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.

37.Discuss file pointer.

>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.

38. What is string in C?

>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 :

*What is recursive? Explain it with a suitable example. (3+4)

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

recursive_fun(); //recursive call


}
int main()
{
recursive_fun(); //function call
}

Example: Fibonacci series using recursion.

* Write advantages and disadvantages of macros over function.


> Adavntages : (i) The main benefit of using macros is faster execution time. During preprocessing, a macro is expanded (replaced by
its definition) inline each time it's used. A function definition occurs only once regardless of how many times it's called.

(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

condition statement in the while loop will result in a compilation error.

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.

*What are the different opening modes of a file? Explain.

1. r: opens a text file in reading mode.


2. w: opens or creates a text file in writing mode.
3. a: opens a text file in append mode.
4. r+: opens a text file in both reading and writing mode. The file must exist.
5. w+: opens a text file in both reading and writing mode. If the file exists, it's truncated
first before overwriting. Any old data will be lost. If the file doesn't exist, a new file will
be created.
6. a+: opens a text file in both reading and appending mode. New data is appended at
the end of the file and does not overwrite the existing content.
7. rb: opens a binary file in reading mode.
8. wb: opens or creates a binary file in writing mode.
9. ab: opens a binary file in append mode.
10. rb+: opens a binary file in both reading and writing mode, and the original content is
overwritten if the file exists.
11. wb+: opens a binary file in both reading and writing mode and works similar to the w+
mode for binary files. The file content is deleted first and then new content is added.
12. ab+: opens a binary file in both reading and appending mode and appends data at the
end of the file without overwriting the existing content.

*What do you mean by type conversion. Explain which suitable examples.

> 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.

Type conversion in c can be classified into the following two types:

1. Implicit Type Conversion

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*/
}

2. Explicit Type Conversion

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.

Type casting in c is done in the following form:

(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:

1. All integer types to be converted to float.


2. All float types to be converted to double.
3. All character types to be converted to integer.

Example

Consider the following code:

int x=7, y=5 ;


float z;
z=x/y; /*Here the value of z is 1*/

If we want to get the exact value of 7/5 then we need explicit casting from int to float:

int x=7, y=5;


float z;
z = (float)x/(float)y; /*Here the value of z is 1.4*/

*Define Break, Continue, Exit switch in the context of C language.

>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;

for (i = 0; i < 10; i++) {


if (i == 4) {
break;
}
printf("%d\n", i);
}
Continue : The continue statement in C language is used to bring the program control to the beginning of the loop.
The continue statement skips some lines of code inside the loop and continues with the next iteration. It is mainly
used for a condition so that we can skip some code for a particular condition.
Example : int i;

for (i = 0; i < 10; i++) {


if (i == 4) {
continue;
}
printf("%d\n", i);
}
Exit : The exit () function is used to break out of a loop. This function causes an immediate termination of the entire
program done by the operation system.

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;
}

*What is the difference between iteration and recursion?


> Recursion is defined as a process in which a function calls itself repeatedly. On the other hand Iteration is defined
as the repetition of computational or mathematical procedure that continues until the controlling condition
becomes false.
The following table highlights all the important differences between recursion and iteration −

Recursion Iteration

Recursion uses the selection structure. Iteration uses the repetition


structure.

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.

*Give example of nested if in C language.

> Check whether a year is leap year or not:


Program code-

#include<stdio.h>

#include<conio.h>

void main(){

int y;

clrscr();

printf(“Enter any year\n”);

scanf(“%d”,&y);

if(y%100==0){

if(y%400==0)

printf(“Leap year”);

else

printf(“Not a leap year”);

else {

if(y%4==0)

printf(“Leap year”);

else

printf(“ Not a leap year”);

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-

User-defined Functions Library Functions

These functions are not predefined


in the Compiler. These functions are predefined in the compiler of C language.

These function are created by user


as per their own requirement. These functions are not created by user as their own.

User-defined functions are not


stored in library file. Library Functions are stored in special library file.

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.

Execution of the program begins


from the user-define function. Execution of the program does not begin from the library function.

Example: sum(), fact(),…etc. Example: printf(), scanf(), sqrt(),…etc.

*What is pass by reference? Give example.


>The call by reference method of passing arguments to a function copies the address of an argument into the formal
parameter. Inside the function, the address is used to access the actual argument used in the call. It means the
changes made to the parameter affect the passed argument.
Example: Swapping two numbers

#include <stdio.h>

int main () {

/* local variable definition */


int a = 100;
int b = 200;

printf("Before swap, value of a : %d\n", a );


printf("Before swap, value of b : %d\n", b );

/* calling a function to swap the values */


swap(&a, &b);

printf("After swap, value of a : %d\n", a );


printf("After swap, value of b : %d\n", b );

return 0;
}
void swap(int *x, int *y) {

int temp;

temp = *x; /* save the value of x */


*x = *y; /* put y into x */
*y = temp; /* put temp into y */

return;
}

*With suitable examples differentiate between while and do while loop.

>Key Differences between while and do-while loop in C

 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;
}

*Differentiate among getch(), getche(), getchar().

> What is getc()?


The getc() reads one character from any input and then returns the corresponding value of the integer on success
(typically, the ASCII value of the read character). On failure, it then returns the EOF.
Here, Syntax:
int getc(FILE *stream);

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():

Parameters getc() getchar() getch() 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy