Skip to main content

C Language Model Question solution of 2022 with programs .

                                                                    Group "A'"                                                                 

 

Q. No 1 .Define loop.

Loop is used to execute the block of code several times according to the condition given in the loop. It means it executes the same code multiple times so it saves code and also helps to traverse the elements of an array.


Q. No. 2.Write the rules of naming identifier.

Rules
  • Identifier names are unique.
  • Cannot use a keyword as identifiers.
  • Identifier has to begin with a letter or underscore (_).
  • It should not contain white space.


Q. No. 3.Write the use of fseek()function with it's complete argument.

The fseek() function is used to set the file pointer to the specified offset. It is used to write data into file at desired location. 


Q. No. 4.List any two logical operator in c.

  • Logical NOT (!)
  • Logical OR (||)


Q. No. 5.List out the advantages of function.

  • i)Makes the program easy to understand.
  • ii)Enables reusability and reduce redundancy


Q.No.6.What is the difference between a variable and constant.

Variable is dynamic in nature which value can be changed .
  
Constant is static in nature which value can't be changed after the initialization.


Q. No. 7.What is the task of preprocessor.

 The preprocessor provides the ability for the inclusion of header files, micro expansions, conditional compilation, and line control.

In many C implementations, it is a separate program invoked by the compiler as the first part of translation.


Q. No.8.Can the main()function left empty?

 Yes, Depending on which compiler is used, it may require a return statement to avoid the function being invalid.


Q. No. 9.How unions help to save memory?

You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple-purpose.


Q. No.10. Give an example of multidimensional array.

In C programming, you can create an array of arrays. These arrays are known as multidimensional arrays. For example, float x[3][4].


                                                                    Group "B'"                                                                 

Q. No. 11.Write a program to find the greater number using function and pass the number as argument.





Q. No. 12.Write a program to print the following pattern.
     *
     **
     ***
     ****
     *****


Q. No. 13.Explain different logical operators in c with details.

In C, logical operators are used to combine and manipulate logical expressions. These operators are used to evaluate whether a certain condition is true or false. The following are the logical operators available in C:

1. && (Logical AND Operator)
The && operator returns true if and only if both of its operands are true. If either operand is false, the entire expression evaluates to false.

Example:
```
int x = 5;
int y = 10;
if (x > 0 && y > 0) {
    printf("Both x and y are greater than 0");
}
```
Output: Both x and y are greater than 0

2. || (Logical OR Operator)
The || operator returns true if either of its operands is true. If both operands are false, the entire expression evaluates to false.

Example:
```
int x = 5;
int y = 10;
if (x > 0 || y < 0) {
    printf("Either x is greater than 0 or y is less than 0");
}
```
Output: Either x is greater than 0 or y is less than 0

3. ! (Logical NOT Operator)
The ! operator is a unary operator that returns the opposite of its operand. If the operand is true, the operator returns false. If the operand is false, the operator returns true.

Example:
```
int x = 5;
if (!(x > 10)) {
    printf("x is not greater than 10");
}


Q. No. 14.What do you mean by dynamic memory allocation? Explain the use of malloc() and free() function with suitable example.

Dynamic memory allocation refers to the allocation of memory at execution not the compile time. Dynamic memory allocation (DMA) has several advantages in memory management and reduce the wastage of memory.
   
                 There are mainly three types of dynamic memory allocation:
i)malloc()
ii)calloc()
iii)realloc()


The uses of malloc() function is presented below:
i)it is used to allocate the memory at run time.
ii)Helps in file input output operations.
iii)Implementing dynamic data structure.

      The uses of free() function is presented below:
i)free() is used to free the memory after use .
ii)Avoiding memory leaks.
iii)It is extremely used in deallocation of memory.



Here is the simple program example of malloc() and free()function.







Q. No. 15.What is structure? How is it different from union? Create structure "student" having data members name, roll no, and percentage. write a program to display the name of all students having percentage greater than or equal to 80


In C programming, a structure is a user-defined data type that groups together related data under a single name. Each member of a structure can be of a different data type, and structures can be used to represent complex data structures such as linked lists, trees, and graphs.

A union is also a user-defined data type, but it differs from a structure in that all members of a union share the same memory location. This means that a union can only hold one value at a time, and changing the value of one member will affect the values of all other members.

Here's an example of how to define a structure in C and use it to store information about students:

c
#include <stdio.h>

// Define the student structure
struct student {
    char name[50];
    int roll_no;
    float percentage;
};

int main() {
    // Create an array of student structures
    struct student students[] = {
        {"Alice", 1, 90.0},
        {"Bob", 2, 75.0},
        {"Charlie", 3, 85.0},
        {"David", 4, 95.0},
        {"Eve", 5, 80.0},
    };
    
    int i, num_students = sizeof(students) / sizeof(struct student);
    
    printf("Students with percentage >= 80:\n");
    
    // Loop through the array of students and print the names of those with percentage >= 80
    for (i = 0; i < num_students; i++) {
        if (students[i].percentage >= 80.0) {
            printf("%s\n", students[i].name);
        }
    }
    
    return 0;
}


In this program, the `student` structure is defined with three members: `name`, `roll_no`, and `percentage`. The program creates an array of `student` structures with five elements, and initializes each element with a name, roll number, and percentage.

The program then loops through the array of students and prints the names of those with a percentage greater than or equal to 80.

Note that structures are typically used to group together related data that belongs to a single entity (in this case, a student), whereas unions are used when only one of several possible data types can be stored in a single variable.


Q. No. 16.Why looping is used in programming ? Explain different looping statement with appropriate example.

Looping is an essential concept in programming that allows a program to execute a block of code repeatedly, based on a condition or a set of conditions. Loops are commonly used to perform a specific task multiple times, process large sets of data, or iterate over arrays and lists.

In C programming, there are three main types of loops: the `while` loop, the `do-while` loop, and the `for` loop.

The `while` loop is used to execute a block of code repeatedly, as long as a certain condition is true. Here's an example:

c
int i = 0;
while (i < 5) {
    printf("%d\n", i);
    i++;
}


This code will print the numbers 0 through 4 to the console, because the condition `i < 5` is true for those values of `i`. Once `i` reaches 5, the loop will exit.

The `do-while` loop is similar to the `while` loop, but it executes the block of code at least once, even if the condition is initially false. Here's an example:

c
int i = 0;
do {
    printf("%d\n", i);
    i++;
} while (i < 5);


This code will also print the numbers 0 through 4 to the console, but it will always execute the `printf()` statement at least once, because the condition is checked at the end of the loop.

The `for` loop is used to execute a block of code a specific number of times, based on a set of conditions. Here's an example:

c
for (int i = 0; i < 5; i++) {
    printf("%d\n", i);
}


This code is functionally equivalent to the first `while` loop example, and it will print the numbers 0 through 4 to the console.

In all of these examples, the loop executes the same block of code multiple times, but the loop condition and the iteration logic are different in each case. The choice of loop type depends on the specific requirements of the program and the nature of the data being processed.
















Comments

Popular posts from this blog

Write a program in java to print your basic information.

 

Write a program in java to swap two numbers without using third variable.

 

How to convert a string to uppercase in C?