1. Answers the following questions:
(i) What are identifiers in C language?
Ans: Identifiers are user defined words. These are used to name the variables, functions, arrays, structures, etc. These names can be in uppercase letters, lowercase letter or a combination. Identifiers must be unique. They are created to give a unique name to an entity to identify it during the execution of the program.
For example:
int age;
int roll;
Here, age and roll are identifiers.
(ii) Explain pointers with a small example.
Ans: A pointer is a variable which stores the address of another variable. It is called pointer because it points to a particular location in memory by storing address of that location. This variable can be of type int, char, array, function, or any other pointer.
Example:
int *a; //pointer to integer type
float *b; //pointer to float type
char *c; //pointer to character type
(iii) Analyse the given code and find the output.
int main()
{
int x=5;
int y=x;
x=10;
printf(“x=%d\n”, x);
printf(“y=%d\n”, y);
return 0;
}
Ans:
- x is initially assigned the value 5.
- y is assigned the value of x, so y becomes 5.
- The value of x is then updated to 10.
- The first printf statement prints the current value of x, which is 10.
- The second printf statement prints the initial value of y, which is the initial value of x, i.e., 5.
Therefore, the output of the program will be:
x=10
y=5
(iv) Given two integers, A=8 and B=5, calculate the result of the bitwise AND operation.
Ans: The bitwise AND operation between two integers is performed by comparing each corresponding bit of the two numbers. If both bits are 1, the resulting bit will be 1; otherwise, it will be 0.
Here, given two integers, A=8 and B=5
A: 1000 (binary representation of 8)
B: 0101 (binary representation of 5)
—————-
Result: 0000 (bitwise AND operation)
(v) What are the variable naming rules in C?
Ans: Rules for Naming a variable in C:
There are some rules on choosing variable names. The following rules must be followed while naming variables:
- A variable name consists of letters (both uppercase and lowercase letters), digits, and the underscore.
- The first character must be a letter or an underscore. It can’t start with a digit.
- Variable names are case sensitive. The compiler treats the upper and lower case letters as different.
- No whitespace is allowed within the variable name.
- Special symbols such as period, semicolon, comma, slash etc. are not allowed other than underscore.
- Any reserved word (keyword) cannot be used as a variable name. e.g. int, float etc.
(vi) Analyse the given code and find the output.
int main()
{
int i;
for(int i=1;i<=5;i++)
{
if(i%2==0)
continue;
printf(“%d”,i);
if(i==3)
break;
}
return 0;
}
Ans: The output of this program will be: 13
(vii) What is the main difference between while and do-while loops?
Ans:
Basis for comparison | While loop | Do-while loop |
Controlling condition | The controlling condition appears at the beginning of the loop. | The controlling condition appears at the end of the loop. |
Checking condition | The condition is checked first and then loop body is executed | The loop body is executed first and then the condition is checked. |
Loop Type | It is entry-controlled loop | It is exit-controlled loop |
Semicolon | No semicolon at the end of while. while (condition) | Semicolon at the end of the while. while (condition); |
Loop execution | The loop body would be executed only if the given condition is true. | The loop body would be executed at least once even the given condition is false. |
Iteration | The iterations do not occur if, the condition at the first iteration appears false. | The iteration occurs at least once even if the condition is false at the first iteration. |
Syntax | while(condition) { statements; //Body of the loop } | do { statements; //Body of the loop } while(condition); |
(viii) What are the four fundamental Data Types in C?
Ans: In the C programming language, there are several data types, but the four fundamental or primary data types are:
- int (Integer): Represents integer values (whole numbers) without any fractional part. For example, int x=10;
- float (Floating-point): Represents real numbers with a fractional part. It is used for single-precision floating-point numbers. For example, float y=3.14;
- double (Double-precision floating-point): Similar to float but offers double precision, allowing for more significant digits. For example, double z=2.71828;
- char (Character): Represents a single character. For example, char c=’A’;
(ix) What is the similarity between a structure, union and enumeration? Select one option from below:
- All of them let you define new values.
- All of them let you define new data types.
- All of them let you define new pointers.
- All of them let you define new structures.
Ans: (b) All of them let you define new data types.
(x) Analyse the given code and find the output.
void main()
{
int i;
for(i=0;i<5;i++)
printf(“%d\n”,i);
}
Ans: The output of this program will be:
0
1
2
3
4
2. Answer all the following:
(a) Explain the different types of function calling with proper examples for each.
Ans: A function call is an important concept of the C programming language. Functions are called by their names. When a program calls a function, the program control is transferred to the called function. A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program.
If the function does not have any arguments, then to call a function we can directly use its name. But for functions with arguments, we can call a function in two different ways:
- Call by value
- Call by reference
Call by Value:
When a function is called by value, a copy of the actual parameter is created and sent to the formal parameter of the called function. The called function uses only the copy of the actual parameter. Therefore, the value of the actual parameter cannot be altered by the called function. Any changes made inside called function are not reflected in actual parameters of the calling function.
The parameters passed to function are called actual parameters whereas the parameters received by function are called formal parameters.
Example:

Output:

In the above program, copy of the parameters a and b is sent to the function swap() and hence output will be the original a and b values and not the swapped values. In order to change the value of the parameters a and b, the function has to be called by reference.
Call by Reference:
When a function is called by reference, the address of the actual parameters is sent to the formal parameter of called functions. If we make any change in the formal parameters, it shows the effect in the value of the actual parameter.
In order to enable the called function to change the variable of the calling function, the address of the actual parameter must be passed. When a parameter is passed by reference, we are not passing copy of its value, but we are passing the parameter itself to the called function. Any changes made inside the called functions are actually reflected in actual parameters of the calling function. The called function can alter the value of the actual parameters.
Example:

Output:

(b) Explain recursive function with an example.
Ans: A recursive function is a function that calls itself during its execution. In C, it is possible for the function to call itself.A recursive function is a function in code that refers to itself for execution.
Recursive functions typically have two main parts: the base case(s) and the recursive case(s). The base case is used to terminate the task of recurring. If base case is not defined, then the function will recur infinite number of times. Recursive case simplifies a bigger problem into a number of simpler sub-problems and then calls them.
Here’s a general working principle of a recursive function:
Base Case(s):
- The base case(s) are the simplest instances of the problem that can be solved directly without further recursion.
- Recursive functions must have one or more base cases to prevent infinite recursion. Without a base case, the function would keep calling itself indefinitely.
- When the base case is met, the function returns a specific value without making a recursive call.
Recursive Case(s):
- The recursive case(s) define how the problem is reduced into smaller subproblems.
- The function calls itself with modified arguments, typically moving closer to the base case.
- The result of the recursive call is often combined with other computations to solve the original problem.
Now, let’s illustrate the working principle with an example. Consider the factorial function, which calculates the factorial of a non-negative integer n. The factorial of n, denoted as n!, is the product of all positive integers up to n.
#include <stdio.h>
int main() {
int factorial(int n);
int n;
printf(“Enter a number:”);
scanf(“%d”,&n);
printf(“Factorial of %d is: %d\n”, n, factorial(n));
return 0;
}
int factorial(int n) {
if (n <=1) //base case
return 1;
else
return n * factorial(n – 1); //recursive case
}
- If n<=1, the base case is triggered, and the function returns 1.
- For any other positive n, the function calculates n * factorial(n – 1), where factorial(n – 1) is computed by making a recursive call.
- The recursion continues until the base case is reached (i.e., n == 1), at which point the function starts unwinding and returning values.
For example, calculating factorial(5)
factorial(5) = 5 * factorial(4)
= 5 * (4 * factorial(3))
= 5 * (4 * (3 * factorial(2)))
= 5 * (4 * (3 * (2 * factorial(1))))
= 5 * (4 * (3 * (2 * 1)))
= 5 * 4 * 3 * 2 * 1
= 120
(c) What is the difference between Structure and Union?
Ans: Structures and Unions are used to define custom data types that can store multiple variables of different types. While both structures and unions serve similar purposes, they differ in their memory allocation and usage. structures are used when you want to group variables of different types together, while unions are used when you want to save memory by sharing the same memory space for different types.
Here are the key differences between structures and unions:
Structure | Union |
The struct keyword is used to define a structure. | The union keyword is used to define a union. |
When the variables are declared in a structure, the compiler allocates memory to each variable’s member | When the variable is declared in the union, the compiler allocates memory to the largest size variable member. |
Every member within structure is assigned a unique memory location. | In union, all the data members share a memory location. |
Changing the value of one data member does not affect other data members in the structure. | Changing the value of one data member affects the value of other data members. |
We can access or retrieve any member at a time. | We can access or retrieve only one member at a time. |
We can initialize multiple members at a time. | We can initialize only one of the members at a time |
A structure’s total size is the sum of the size of every data member. | A union’s total size is the size of the largest data member. |
3. Answer all the following:
(a) Write a C program to read a number in a variable and then reverse it.
Ans: Program to read a number in a variable and then reverse it.

Output:

(b) What are the Storage classes in C? Explain each of them.
Ans: Storage classes are used to describe the features of a variable. Every variable in C has two attributes, type and storage class. Type refers to the data type of a variable, and storage class determines the scope or visibility, life time, memory location and initial value of a variable. In our programs, we didn’t mention storage class of the variables used. This is because of the fact that if the storage class is not given for a variable in its declaration, then the compiler will assume a storage class as the default.
A storage class describes the following:
- The location where the variable would be stored.
- The default initial value of the variable if it is not initialized
- The scope of the variable
- The life time of the variable, i.e. for how long will the variable exist.
Syntax of declaring storage classes is:
storage_class data_type variable_name;
There are four types of storage classes in C:
1. Auto (Automatic) storage class
2. Static storage class
3. Register storage class
4. Extern (External) storage class
Auto (Automatic) Storage Class:
The auto storage class is the default storage class for local variables. The variables defined using automatic storage class are called local variables because they are local to a function where they are defined. By default, they are assigned garbage value by the compiler. The keyword used to declare automatic storage class variable is auto.
Example:
#include <stdio.h>
int main() {
auto int num = 10; // ‘auto’ is optional, as it is the default storage class
printf(“Value of num: %d\n”, num);
return 0;
}
static Storage Class:
The static storage class is used to declare variables that retain their values between function calls. Static variables are initialized only once, and their values persist throughout the program’s execution. The static storage class instructs the compiler to keep the variable value until the end of program. These variables retain their values throughout the life-time of the program. Like automatic variables, static variables are also local to the function, in which they are defined.
The keyword used to declare static storage class variable is static.
Example:
#include <stdio.h>
int main() {
incrementStatic();
incrementStatic();
incrementStatic();
return 0;
}
void incrementStatic() {
static int counter = 0; // ‘static’ retains the value between function calls
counter++;
printf(“Static counter: %d\n”, counter);
}
register Storage Class:
The register storage class is used to suggest the compiler to store the variable in a CPU register for faster access. A value stored in CPU register can always be accessed faster than that of variable stored in memory. Frequently accessed variables are kept in register. Therefore, if a variable is used at many places in a program, it is better to declare its storage class as register.
Example:
#include <stdio.h>
int main() {
register int count = 0; // ‘register’ suggests the use of a register for faster access
while (count < 5) {
printf(“%d “, count);
count++;
}
return 0;
}
extern Storage Class:
The extern storage class is used to declare variables that are defined in another file. It is often used when we want to use a variable that is defined in another source file. External storage class informs the compiler that the variable defined as extern is defined elsewhere in any program. We use external variable for giving a reference of any global variable which have been already defined.
The external storage class is used when we have global variables which are shared between two or more files. These variables are accessible throughout the program.
Example:
// File: file1.c
int globalVar = 100;
// File: file2.c
#include <stdio.h>
extern int globalVar; // ‘extern’ declares the variable defined in another file
int main() {
printf(“Value of globalVar: %d\n”, globalVar);
return 0;
}
4. Answer all the following:
(a) Write a C program to read records from a file (student_records.txt which contains name and marks) and calculate the grade of each student.
grade=A, if marks>=80. grade=B, if marks>=60 and <80. grade=C, if marks<60.
Ans:


Output:

Note:
- The purpose of returning 0 to the operating system is that the program is terminated successfully without any error.
- And the purpose of returning 1 to the operating system is that the program is terminated due to some errors.
- exit(0) is same as return 0; it also tells that the program has compiled or run successfully without any error.
- exit(1) is same as return 1; it tells that program has terminated due to some errors or memory allocation.
(b) Using the concept of structure, write a C program to store information (Name, Age, Gender, Height, Marks in Math) of 100 students as input and display the same.
Ans:
#include <stdio.h>
// Define the structure for student information
struct student {
char name[20];
int age;
char gender;
float height;
int mathMarks;
};
int main()
// Declare an array of structures to store information for 100 students
struct student std[100];
// Input information for each student
for (int i = 0; i <100; i++) {
printf(“Enter details for student %d:\n”, i + 1);
printf(“Name: “);
scanf(“%s”, std[i].name);
printf(“Age: “);
scanf(“%d”, &std[i].age);
printf(“Gender (M/F): “);
scanf(” %c”, &std[i].gender);
printf(“Height (in ft.): “);
scanf(“%f”, &std[i].height);
printf(“Marks in Math: “);
scanf(“%d”, &std[i].mathMarks);
printf(“\n”);
}
// Display information for each student
printf(“Student Information:\n”);
for (int i = 0; i <100; i++) {
printf(“Student %d:\n”, i + 1);
printf(“Name: %s\n”, std[i].name);
printf(“Age: %d\n”, std[i].age);
printf(“Gender: %c\n”, std[i].gender);
printf(“Height: %f\n”, std[i].height);
printf(“Marks in Math: %d\n”, std[i].mathMarks);
printf(“\n”);
}
return 0;
}
Output:
Enter details for student 1:
Name: Akash
Age: 18
Gender (M/F): M
Height (in ft.): 5.4
Marks in Math: 60
Enter details for student 2:
Name: Bipul
Age: 18
Gender (M/F): M
Height (in ft.): 5.3
Marks in Math: 65
Student Information:
Student 1:
Name: Akash
Age: 18
Gender: M
Height: 5.400000
Marks in Math: 60
Student 2:
Name: Bipul
Age: 18
Gender: M
Height: 5.300000
Marks in Math: 65
5. Answer all the following:
(a) Write a C program to multiply two matrices (Your program code should whether multiplication is possible for the entered matrices).
Ans:
#include <stdio.h>
void input1stMatrix(int firstmatrix[10][10], int rows, int cols) {
printf(“Enter the elements of the 1st matrix:\n”);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf(“%d”, &firstmatrix[i][j]);
}
}
}
void input2ndMatrix(int secondmatrix[10][10], int rows, int cols) {
printf(“Enter the elements of the matrix:\n”);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf(“%d”, &secondmatrix[i][j]);
}
}
}
void multiplyMatrices(int firstMatrix[10][10], int secondMatrix[10][10], int result[10][10], int rows1, int cols1, int rows2, int cols2) {
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
result[i][j] = 0;
}
}
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
}
void displayMatrix(int result[10][10], int rows, int cols) {
printf(“Resultant Matrix:\n”);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf(“%d\t”, result[i][j]);
}
printf(“\n”);
}
}
int main() {
int firstMatrix[10][10], secondMatrix[10][10], resultMatrix[10][10];
int rows1, cols1,rows2,cols2;
printf(“Enter the number of rows and columns for the first matrix:”);
scanf(“%d %d”, &rows1, &cols1);
printf(“Enter the number of rows and columns for the second matrix:”);
scanf(“%d %d”, &rows2, &cols2);
if (cols1 != rows2) {
printf(“Multiplication is not possible.\n”);
return 1; // Exit with an error code
}
input1stMatrix(firstMatrix, rows1, cols1);
input2ndMatrix(secondMatrix, rows2, cols2);
multiplyMatrices(firstMatrix, secondMatrix, resultMatrix, rows1, cols1, rows2, cols2);
displayMatrix(resultMatrix, rows1, cols2);
return 0; // Exit successfully
}
Output-1:
Enter the number of rows and columns for the first matrix:2 3
Enter the number of rows and columns for the second matrix:2 1
Multiplication is not possible.
Output-2:
Enter the number of rows and columns for the first matrix:2 2
Enter the number of rows and columns for the second matrix:2 2
Enter the elements of the 1st matrix:
2 3
2 1
Enter the elements of the matrix:
2 1
3 2
Resultant Matrix:
13 8
7 4
(b) Write a C program to swap values between two variables, without using a third variable.
Ans: Program to swap values between two variables, without using a third variable.

Output:

6. Answer all the following:
(a) Write a C program to first read a Year from the user and then write the one-line code using the ternary operator to find whether input year is a Leap Year or not.
Ans:
#include <stdio.h>
int main() {
int year;
// Read the year from the user
printf(“Enter a year: “);
scanf(“%d”, &year);
// Check if it’s a leap year using the ternary operator
(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? printf(“%d is a Leap Year.\n”, year) : printf(“%d is not a Leap Year.\n”, year);
return 0;
}
Output:
Enter a year: 2020
2020 is a Leap Year.
Enter a year: 2001
2001 is not a Leap Year.
(b)
(i) What are bitwise operators in C? explain any two with examples.
Or
(ii) Write the codes for a function with the name mystrcmp(str1,str2), which should give the same output as the library function strcmp(str1,str2).
(i) Ans: For manipulating data at the bit level, C provides us special operators known as bitwise operators. The bitwise operators are the operators used to perform the operations on the data at the bit level. These operators are used to perform bitwise operations such as for testing the bits, shifting the bits to left or right, one’s complement of bits etc.
These operators help us to directly manipulate individual bits within a word. Bit level manipulations are used in setting a particular bit or group of bits to 1 or 0. It is mainly used in numerical computations to make the calculation process faster. These bitwise operators are useful for tasks such as manipulating individual bits, setting or clearing specific bits in a number, or checking the status of certain bits in a bitfield. They are commonly used in embedded systems programming, low-level optimizations, and networking applications.
Various bitwise operators in C:
Operators | Meanings |
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR (Exclusive OR) |
<< | Shift left |
>> | Shift right |
~ | one’s complement |
Two commonly used bitwise operators in C: Bitwise AND (&) and Bitwise OR (|
)
Bitwise AND (&):
The bitwise AND operator (&) compares each bit of two integers and produces a new integer where each bit is the result of the AND operation on the corresponding bits of the operands. The result is 1 only if both corresponding bits in the operands are 1.
Example:
#include <stdio.h>
int main() {
// Using bitwise AND to perform bitwise intersection
int a = 12; // binary: 1100
int b = 25; // binary: 11001
int result = a & b;
printf(“Result of %d & %d is %d\n”, a, b, result);
return 0;
}
Output:
Result of 12 & 25 is 8 // binary: 1000
In this example, the bitwise AND operation is performed on the binary representations of a and b, resulting in the binary value 1000, which is equivalent to decimal 8.
Bitwise OR (|
):
The bitwise OR operator (|
) compares each bit of two integers and produces a new integer where each bit is the result of the OR operation on the corresponding bits of the operands. The result is 1 if at least one of the corresponding bits in the operands is 1.
Example:
#include <stdio.h>
int main() {
// Using bitwise OR to perform bitwise union
int x = 5; // binary: 0101
int y = 10; // binary: 1010
int result = x | y;
printf(“Result of %d | %d is %d\n”, x, y, result);
return 0;
}
Output:
Result of 5 | 10 is 15 // binary: 1111
In this example, the bitwise OR operation is performed on the binary representations of x and y, resulting in the binary value 1111, which is equivalent to decimal 15.
(ii) Ans: the codes for a function with the name mystrcmp(str1,str2), which should give the same output as the library function strcmp(str1,str2).
#include <stdio.h>
int mystrcmp(const char *str1, const char *str2) {
while (*str1 && (*str1 == *str2)) {
str1++;
str2++;
}
return *(unsigned char *)str1 – *(unsigned char *)str2;
}
int main() {
char str1[] = “Hello”;
char str2[] = “Welcome”;
// Using mystrcmp function
int result = mystrcmp(str1, str2);
if (result == 0) {
printf(“Strings are equal.\n”);
} else if (result < 0) {
printf(“String 1 is less than String 2.\n”);
} else {
printf(“String 1 is greater than String 2.\n”);
}
return 0;
}
Output:
String 1 is less than String 2.
(c) Explain the switch statement with an example. How does the switch statement differ from the if-else statement.
Ans: In C programming, the switch statement is a control flow statement used to select one of many code blocks to be executed. It is often used as an alternative to a series of if-else statements when a variable or expression needs to be tested against multiple values.
Here’s an example to illustrate the use of the switch statement:
If-Else | Switch Case |
An if-else statement can test expression based on a range of values or conditions. | A switch statement tests expressions based only on a single integer, enumerated value or string object. |
Executes if or else block depending on the condition in the if statement | Executes code block corresponding to the matched case |
If statement is used to select among two alternatives | The switch statement is used to select among multiple alternatives. |
It evaluates all types of data, such as integer, float, string, character or Boolean etc. | It evaluates only integer or character value. |
Either if statement will be executed or else statement is executed. | The switch statement executes one case after another till a break statement is found or the end of the switch statement is reached. |
If the condition of if block is false, the statements inside the else block will execute | if none of the cases match, the statement of the default block would be executed |
The If-Else statement checks equality and logical expression | The switch statement checks only for equality |
7. Writes short notes with examples, where required (any five from below):
(a) Operators in C.
(b) Dynamic Memory Allocation and use of malloc() and calloc() in C.
(c) Preprocessor Directives in C.
(d) Loops in C
(e) File Management in C.
(f) Relation between Array and Pointers in C.
Ans:
(a) Operators in C:
An operator can be defined as a symbol that specifies an operation to be performed. C language supports a rich set of operators. Operators help the programmer to perform computation on values.
An operator is a symbol that tells the compiler to perform certain mathematical or logical computations. The data items on which the operators act upon are called operands. Some operators require a single operand to perform operation while others might require two operands.
Arithmetic operators are the most common. Other operators are used for comparison of values, combination of logical states, manipulation of individual binary digits etc.
Categories of operators:
Operators in C can be classified into a number of categories as follows:
Assignment Operator:
An assignment operator is used to assign a value to a variable or to assign the result of an expression to a variable. The = symbol is used as an assignment operator.
Or simply we can say, a data or value can be stored in a variable with the help of assignment operator. The assignment operator is used in assignment statement and assignment expression.
The syntax of an assignment is,
Variable name=expression;
For example, a=5;
Arithmetic Operators:
The arithmetic operators are used to perform mathematical calculations such as addition, subtraction etc. C supports all the common arithmetic operators.
Operator | Meaning |
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
Relational Operators:
These are used to compare the values two variable or constants. The relational operators are symbols that are used to test the relationship between variables or between a variable and a constant.
For example,
(salary==5000)
Here == is the operator that is used to test equality.
There are six relational operators provided by C. They are:
Operator | Meaning |
== | equal to |
!= | not equal to |
> | greater than |
< | less than |
>= | greater than or equal to |
<= | less than or equal to |
Logical Operator:
Logical operators are symbols that are used to combine two or more expressions containing relational operators. This situation will arise when we want to perform a statement block on a combined condition, like x>5 and x<10. Then we code this using an AND operator. The AND operator is represented as &&.
Example:
((x>5)&&(x<10))
C has three logical operators:
Operator | Meaning |
&& | AND |
|| | OR |
! | NOT |
Increment Decrement Operator:
The increment and decrement operators are two very useful operator used in C. Both increment and decrement operator are used on a single operand and variable, so it is called as a unary operator.
Increment operators are used to increase the value of a variable by 1. This operator is represented by ++ symbol. The increment operator can either increase the value of the variable by 1 before assigning it to the variable or can increase the value of the variable by 1 after assigning the variable.
We use decrement operators in C to decrement the given value of a variable by 1. This operator is represented by – – symbol.
Conditional Operators:
Sometimes we need to choose between two expressions based on the truth value of a third logical expression. The conditional operator can be used in such cases. It sometimes called as ternary operator. It required three expressions as operand and it is represented as ? :
Syntax: exp1 ? exp2 : exp3;
Here exp1 is first evaluated. It is true then value return will be exp2 . If false then exp3.
Example: c=(a>b)?a:b;
Bitwise Operator:
For manipulating data at the bit level, C provides us special operators known as bitwise operators. The bitwise operators are the operators used to perform the operations on the data at the bit level. These operators are used to perform bitwise operations such as for testing the bits, shifting the bits to left or right, one’s complement of bits etc.
These operators help us to directly manipulate individual bits within a word. Bit level manipulations are used in setting a particular bit or group of bits to 1 or 0. It is mainly used in numerical computations to make the calculation process faster.
Various bitwise operators in C:
Operators | Meanings |
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR (Exclusive OR) |
<< | Shift left |
>> | Shift right |
~ | one’s complement |
(b) Dynamic Memory Allocation and use of malloc() and calloc() in C:
The process of allocating and deallocating (freeing) memory at the time of execution of a program (at runtime) is called dynamic memory allocation.
The advantage of this method is that only as much memory as is needed at any time is reserved, thus avoiding unnecessary blocking of memory as in static memory allocation. Two types of problem may occur in static memory allocation. If number of values to be stored is less than the size of memory, there would be wastage of memory. If we would want to store more values by increase in size during the execution on assigned size then it fails. Allocation and release of memory space can be done with the help of some library functions.
Memory allocation and Deallocation Function:
Functions | Purpose |
malloc() | Allocates the memory of requested size. |
calloc() | Allocates multiple block of requested memory. |
free() | Frees the previously allocated memory. |
realloc() | Reallocates the memory (modifies the size of previously allocated memory) |
Malloc function():
The malloc or memory allocation function is used to allocate a block of memory of the specified size (in bytes). It returns a pointer to the first byte of allocated space. The type of the pointer it returns is void, which can be cast into a pointer of any type. It returns NULL, if there is not enough memory available. The malloc() function does not initialize memory at execution time, so it has garbage value initially.
Syntax:
ptr=(cast_type*)malloc(size_in_bytes)
Example:
int *x;
x=(int*)malloc(sizeof(int));
The above statement allocates 2 bytes of main memory and returns the address of the memory allocated which is stored in x. The (int *) before the malloc function casts the integer type on malloc, hence malloc returns a pointer to the integer.
Calloc() Function:
The calloc or contiguous allocation function works similar to malloc() function except for the fact that it requires two parameters or arguments as against one parameter needed by malloc(). It allocates multiple block of requested memory.
Syntax:
ptr=(cast_type*)calloc(n, size_in_bytes);
Here, n is the number of blocks and size_in_bytes specifies size of each block
calloc() initializes each block with a default value 0. It returns NULL, if there is not enough memory available.
Example:
float *y;
y=(float *) calloc(5, sizeof(float));
This statement allocates contiguous space in memory for 5 elements of type float.
(c) Preprocessor Directives in C:
A preprocessor directive is a command that begins with a hash symbol (#). The directives can be placed anywhereina program but most often placed at the beginning of the program, before the function definition. Preprocessor directives are processed by the preprocessor before actual compilation.
For example, #define is a directive that defines a macro. Some other preprocessor directives are: #include, #define, #undef etc.
Preprocessor directives are lines included in the code preceded by a hash sign (#
). These lines are not program statements but directives for the preprocessor. The preprocessor examines the code before actual compilation of code begins, and resolves all these directives. No semicolon (;
) is expected at the end of a preprocessor directive.
The main purposes of preprocessor directives is to provide instructions to the preprocessor for conditional compilation, file inclusion, macro definition, and other tasks.
The commonly used preprocessor directives and their functions are given in the table below:
Directive | Function |
#define | Substitutes a preprocessor macro. |
#undef | Undefines a preprocessor macro |
#include | Specifies the file to be included |
#ifdef | Tests for a macro definition |
#if | Tests a compile-time condition |
#else | Specifies alternatives when #if test fails |
#endif | Specifies the end of #if |
#ifndef | Test whether a macro is not defined |
Preprocessor directives can be divided into three categories:
- Macro substitution directive
- File inclusion directive
- Conditional Compilation directive
Macro Substation Directives:
The macro substitution is refers to the text replacement facility that is offered by the C preprocessor. #define directive is used by the preprocessor to perform this task and is also known as Macro definition.
The general syntax of using the #define directive is as follows:
#define identifier replacement_text
File Inclusion Directives:
An external file containing functions or macro definitions can be included as part of a program to avoid rewriting those functions or macros. This can be done by using the #include directive.
The general syntax of this directive is as follows:
#include <filename> OR #include “filename”
Conditional Compilation Directives:
The C preprocessor provides certain directives that control which part of the code are seen by the compiler and which are not. This process of selectively compiling parts of code is known as conditional compilation.
The preprocessor has a conditional statement similar to if else. It can be used to selectively include statements in a program. There are a number of preprocessor directive related to conditional compilation of code. The keywords for conditional selection are: #ifdef, #ifndef, #else and #endif.
(d) Loops in C:
Loop in C is a technique in which a set of statements are repeatedly executed until the condition specified becomes false. So looping is based on a condition. A loop is a repeated (iterated) sequence of statements. The repetition continues while a condition is true. When condition becomes false, the loop ends. Then control passes to the statements following the loop.
Looping Statements in C executes the sequence of statements many times until the stated condition becomes false. A loop in a program consists of two parts, one part is known as body of the loop and the other part is known as control statement. The control statement consists of certain conditions which are checked during execution and depending on the condition the statements within the body of the loop are executed repeatedly.
Categories of loops:
There are three types of loops available in C:
- For loop
- While loop
- Do…While loop
It is an entry controlled loop. Generally, for loop is used to execute a set of statements a specific number of times.
Syntax:
for(initialization; test condition; modifier expression)
{
body of the loop;
}
While Loop:
The simplest of all the looping statements in C is the while loop. The while loop is an Entry Controlled loop. In a while loop, the condition is checked before executing the body of the loop. If condition is true only then the body of the loop is executed otherwise not. After execution of the body of the loop, the control again goes back to test condition. The condition is checked, if it is true the body of the loop is executed once again. This process is repeated until the test condition becomes false. The loop will be continued until the test condition is true and terminates as soon as test condition becomes false. Once the control goes out of the loop the statements which are immediately after the loop is executed.
Syntax:
while (test condition)
{
body of the loop
}
Do-While Loop
It is similar to the while loop except that it tests the condition at the end of the loop body. It is an exit controlled loop statement. In do…while loop, the condition is always checked after the body of the loop.
In some cases, we have to execute a body of the loop at least once even if the condition is false. The while and for do not support such action. This type of operation can be achieved by using the do while statement.
In do…while loop, the body of the loop is always executed at least once. After the body is executed, then it checks the condition. If the condition is true, then it will again execute the body of the loop otherwise control is transferred out of the loop. Once the control goes out of the loop the statements which are immediately after the loop is executed.
Syntax:
do
{
body of the loop
}
while (condition);
(e) File Management in C:
File management in C involves operations related to reading from and writing to files. The standard input/output library in C, stdio.h, provides functions for file operations. File management involves various functions and concepts for creating, reading, writing, and manipulating files.
File management in C involves the following:
- Creating a file
- Opening an existing file
- Reading data from a file
- Writing data to a file
- Moving to a specific location
- Closing a file
Here are some common file-related functions and operations in C:
Opening and Closing Files:
fopen: Opens a file with a specified mode ( r for reading, w for writing, a for appending, etc.).
General format for defining and opening a File:
FILE *fptr;
fptr = fopen(“filename”, “mode”);
The first statement declares the variable fptr as “a pointer to the data type FILE”.
The second statement opens the file filename and assigns anidentifier to the FILE type pointer fptr. It also specifies the purpose of opening this file. The mode does this job.
Example:
FILE *fptr;
fptr = fopen(“students.txt”, “r”);
fclose: Closes the opened file. A file must be closed as soon as all operations on it have been completed. This would close the file associated with the file pointer.
Syntax for closing a file:
fclose(fptr);
Reading and Writing Files:
fprintf and fscanf: Used for writing and reading formatted data to/from a file. The fprintf and fscanf functions are identical to printf and scanf functions except that they work on files. The first argument of theses functions is a file pointer which specifies the file to be used.
The general form of fprintf is:
fprintf(fp,”control string”, list);
Where fp is a file pointer associated with a file that has been opened for writing. The control string is file output specifications list may include variable, constant and string.
Example:
fprintf(fptr, “This is a number: %d”, 42);
fscanf(fptr, “This is a number: %d”, &num);
fputc and fgetc: Used for writing and reading a single character to/from a file. getc is used to read a character from a file that has been opened, while putc is used to write a character to the file that has been opened.
Example:
fputc(‘A’, fptr);
char ch = fgetc(fptr);
fputs and fgets: Used for writing and reading strings to/from a file.
fputs(“Hello, File!”, fptr);
fgets(buffer, sizeof(buffer), fptr);
Binary File Operations:
fwrite and fread: Used for reading and writing binary data.
fwrite(data, sizeof(data_type), num_elements, fptr);
fread(data, sizeof(data_type), num_elements, fptr);
File Positioning:
fseek and ftell: fseek() function is used to move the file position to a desired location within the file.
The general format of fseek function is a s follows:
fseek(file_pointer,offset, position);
File_pointer is a pointer to the file concerned. Offset is a number or variable of type long, and position in an integer number. Offset specifies the number of positions (bytes) to be moved from the location specified the position. The position can take the 3 values.
0 = Beginning of the file
1 =Current position
2 =End of the file.
Example:
fseek(fptr, offset, SEEK_SET); // Move to ‘offset’ from the beginning of the file
ftell() : Th ftell function returns a long integer which is the current position of the file pointer from the beginning of the file in terms of bytes.
Example:
long position = ftell(fptr); // Get the current position in the file
Checking for the End of File (EOF):
feof: Checks if the end of the file has been reached.
if (feof(fptr)) {
// End of file reached
}
Error Handling:
ferror: Checks for a file error.
if (ferror(fptr)) {
// File error occurred
}
(f) Relation between Array and Pointers in C:
In C, arrays and pointers are closely related, and understanding the relationship between arrays and pointers is crucial for effective programming. Here are the key points illustrating the relationship between arrays and pointers:
Array Name as a Pointer:
In C, there is a close relationship between array names and pointers. The name of the array is a pointer to the first element of the array. That means it holds the address of the very first element of the array. An array name is a constant pointer to the first element of the array.
For example:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr; // Equivalent to int *ptr = &arr[0];
Array Access using Pointers:
We can use pointers to access array elements.
For example:
int x = *ptr; // Equivalent to int x = arr[0];
int y = *(ptr + 1); // Equivalent to int y = arr[1];
Pointer Arithmetic:
Pointer arithmetic can be used to navigate through array elements. Incrementing or decrementing an array name increments or decrements the memory address by the size of the array’s elements.
For example:
int *p = arr;
for (int i = 0; i < 5; ++i) {
printf(“%d “, *p);
p++; // Move to the next element
}
Array Parameters in Functions:
When we pass an array to a function, we are effectively passing the address of its first element.
For example:
void printArray(int *arr, int size) {
for (int i = 0; i < size; ++i) {
printf(“%d “, arr[i]);
}
}
int main() {
int arr[5] = {1, 2, 3, 4, 5};
printArray(arr, 5); // Passing the address of the first element
return 0;
}
Dynamic Memory Allocation:
Arrays can be dynamically allocated using pointers, especially with functions like malloc, calloc, and realloc.
For example:
int *dynamicArray = (int *)malloc(5 * sizeof(int));
Pointer to Arrays:
Pointers can also point to entire arrays.
For example:
int arr[5] = {1, 2, 3, 4, 5};
int (*ptrToArray)[5] = &arr;
Woah! I’m really enjoying the template/theme of this website.
It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and appearance.
I muwt say you’ve done a fantastic job with this.
Also, the blog loads extremely quick for mee on Safari.
Exceptional Blog! https://WWW.Waste-NDC.Pro/community/profile/tressa79906983/
Thank you so much for your nice comments…
Woah! I’m really enjoying the template/theme off this website.
It’s simple, yet effective. A lot of times it’s very hard to
get that “perfect balance” between superb usability and appearance.
I must say you’ve done a fantastic job with this. Also, the blog loads extremely quick for mme on Safari.
Exceptional Blog! https://WWW.Waste-NDC.Pro/community/profile/tressa79906983/