ICT.Ed.416-Programming Concept with C

RB Sharma
By -
0
TRIBHUVAN UNIVERSITY

2078


Bachelors/Education /1stSemester.                                                  Full Marks: 40                                                                                                                 Time: 3 hrs.

ICT.Ed.416-Programming Concept with C




Regarding your request, it seems to be a list of different questions and topics related to the C programming language. Here are some brief explanations on each of these topics:

1. Rules of naming identifiers in C: Identifiers are names given to variables, functions, etc. in a C program. The rules for naming identifiers in C are:
- An identifier must start with a letter or an underscore (_).
- After the first character, an identifier may contain letters, digits, or underscores.
- Upper and lowercase letters are distinct in C.
- A reserved keyword cannot be used as an identifier.

Unary operators are those that work with a single operand, such as increment (++), decrement (--), and logical negation (!). Arithmetic binary operators are those that work with two operands and perform mathematical operations, such as addition (+), subtraction (-), multiplication (*), division (/), and modulo (%).

2. Variable and data types in C: A variable is a named data storage location in memory that can hold a value, which may change during program execution. C supports different data types, such as int (integer), float (floating-point), char (character), double (double-precision floating-point), short, long, etc.

While loop and do while loop are used for repetitive execution of a block of code in C, with the difference that the do while loop executes its block of code at least once, regardless of the condition. Switch case statement is used to select one of several alternatives based on the value of an expression.

3. Function in C: A function is a named block of code that performs a specific task and may return a value. Functions help to organize code, make it reusable, and improve modularity and maintainability. Here's an example of a function in C that checks whether a given number is odd or even:

```
#include <stdio.h>

int isOdd(int num) {
    if (num % 2 == 0) {
        return 0; // Even
    } else {
        return 1; // Odd
    }
}

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    if (isOdd(num)) {
        printf("%d is odd.\n", num);
    } else {
        printf("%d is even.\n", num);
    }
}
```

4. String and array in C: A string is a sequence of characters that is terminated by a null character ('\0'). In C, a string can be represented as an array of characters. Here's an example of a C program that stores the age of 10 students in an array and then finds and displays their average age:

```
#include <stdio.h>

int main() {
    int ages[10];
    int sum = 0;
    float avg;

    printf("Enter the ages of 10 students:\n");
    for (int i = 0; i < 10; i++) {
        scanf("%d", &ages[i]);
        sum += ages[i];
    }

    avg = (float) sum / 10;
    printf("Average age = %.2f\n", avg);

    return 0;
}
```

5. Structure in C: A structure is a user-defined data type in C that groups together related data elements of different data types into a single unit. The elements of a structure are called members. An array of structures is a collection of multiple instances of the same structure type. Here's an example of a C program that declares a structure and an array of structures, and then initializes and displays their values:

```
#include <stdio.h>

struct student {
    char name[50];
    int age;
    float gpa;
};

int main() {
    struct student class[3] = {
        {"John Doe", 20, 3.5},
        {"Jane Smith", 19, 3.2},
        {"Tom Lee", 21, 3.8}
    };

    for (int i = 0; i < 3; i++) {
        printf("Student #%d:\n", i+1);
        printf("Name: %s\n", class[i].name);
        printf("Age: %d\n", class[i].age);
        printf("GPA: %.2f\n", class[i].gpa);
        printf("\n");
    }

    return 0;
}
```

6. File handling and pointers in C: File handling is the process of reading from and writing to files on the disk. C provides different file opening modes, such as "r" (read), "w" (write), "a" (append), etc. And pointers are variables that store memory addresses of other variables. Pointer arithmetic refers to performing arithmetic operations on pointers to navigate through memory. Here's an example of a C program that writes some personal information into a file using file handling and pointers:

```
#include <stdio.h>

int main() {
    FILE *fp;
    char name[50];
    int roll, age;
    char sex;

    fp = fopen("student.txt", "w");

    printf("Enter your name: ");
    gets(name);
    fprintf(fp, "Name: %s\n", name);

    printf("Enter your roll number: ");
    scanf("%d", &roll);
    fprintf(fp, "Roll Number: %d\n", roll); 

    printf("Enter your age: ");
    scanf("%d", &age);
    fprintf(fp, "Age: %d\n", age);

    printf("Enter your sex: ");
    scanf(" %c", &sex);
    fprintf(fp, "Sex: %c\n", sex);

    fclose(fp);

    printf("Data written to file successfully.\n");

    return 0;
}
```
Tags:

Post a Comment

0Comments

Thank You for connoting our Site

Post a Comment (0)