logo

Unit 4: Programming in C (12 Hours)

Computer Science - Class 12

MCQ questions

Unit 4: Programming in C (12 Hours)

4.1 Review of C Programming Concepts

C is a foundational programming language that provides control over hardware and system resources. Key concepts include:

  • Basic Structure: A typical C program consists of:
    • Preprocessor directives (e.g., #include <stdio.h>)
    • The main() function, which is the entry point of the program.
    • Variable declarations and function calls.
  • Data Types: Fundamental types include:
    • int, float, char, double, etc.
  • Control Structures: Decision-making (if, switch) and looping (for, while, do-while) structures control the flow of execution.
  • Syntax and Semantics: Correct syntax is crucial for successful compilation and execution.

4.2 Functions

Functions are blocks of code designed to perform specific tasks, promoting code reuse and modularity.

4.2.1 Concept of Library and User-Defined Functions and Advantages
  • Library Functions: Predefined functions in C libraries (like printf(), scanf()) that can be used directly without definition.
  • User-Defined Functions: Functions created by the programmer to perform specific tasks.
    • Advantages:
      • Code Reusability: Functions can be reused in different programs.
      • Easier Maintenance: Changes can be made in one place without affecting the whole code.
      • Improved Readability: Programs become easier to understand.
4.2.2 Function Definition, Prototype, Call, and Return Statements
  • Function Definition: Specifies the function's return type, name, parameters, and body.
    c
    returnType functionName(parameterType parameterName) { // function body }
  • Function Prototype: Declaration of a function that informs the compiler about its name, return type, and parameters before its actual definition.
    c
    returnType functionName(parameterType);
  • Function Call: The process of invoking a function using its name and passing arguments.
    c
    functionName(arguments);
  • Return Statement: Used to exit a function and optionally send a value back to the calling function.
    c
    return value;
4.2.3 Accessing a Function by Passing Values

Functions can access and manipulate data passed to them:

  • Call by Value: A copy of the actual parameter is passed, leaving the original variable unchanged.
    c
    void function(int x) { x = x + 1; // changes x only inside this function }
  • Call by Reference: A reference (address) of the actual parameter is passed, allowing modifications to the original variable.
4.2.4 Concept of Storage: Automatic and External
  • Automatic Storage: Local variables declared within functions are automatically allocated and deallocated. Their storage duration is tied to the function call.
  • External Storage: Global variables, declared outside functions, have a lifetime equal to the program's execution. They retain their values throughout.
4.2.5 Concept of Recursion: Factorial and Fibonacci Problems

Recursion is a programming technique where a function calls itself to solve a problem.

  • Base Case: The condition under which recursion terminates.
  • Recursive Case: The part of the function where the function continues to call itself.
  • Example: Factorial:
    c
    int factorial(int n) { if (n == 0) return 1; // base case return n * factorial(n - 1); // recursive case }
  • Example: Fibonacci:
    c
    int fibonacci(int n) { if (n <= 1) return n; // base case return fibonacci(n - 1) + fibonacci(n - 2); // recursive case }

4.3 Structures and Unions

Structures and unions are user-defined data types that allow grouping related variables.

4.3.1 Structure: Definition, Declaration, Initialization, and Size of Structure
  • Definition: A structure groups different data types under a single name.
    c
    struct Student { char name[50]; int roll_no; float marks; };
  • Declaration: Creating a structure variable.
    c
    struct Student s1;
  • Initialization: Assigning values to a structure.
    c
    struct Student s1 = {"Alice", 1, 85.0};
  • Size of Structure: The size can be determined using the sizeof operator.
    c
    printf("%lu", sizeof(s1)); // prints the size of the structure
4.3.2 Accessing Member of Structure

Structure members are accessed using the dot operator (.).

c
s1.marks = 90.0; // Assigning value to member printf("%s", s1.name); // Accessing member
4.3.3 Array of Structure

An array can be created to store multiple structures.

c
struct Student students[100]; // array of 100 students
4.3.4 Union: Definition, Declaration
  • Definition: A union allows storing different data types in the same memory location. Only one member can store a value at a time.
    c
    union Data { int intValue; float floatValue; char charValue; };
  • Declaration: Creating a union variable.
    c
    union Data data;
4.3.5 Difference Between Union and Structure
  • Memory Allocation:
    • Structure: Allocates memory for all members.
    • Union: Allocates memory for the largest member only.
  • Access:
    • Structure: All members can be accessed simultaneously.
    • Union: Only one member can be accessed at a time.

4.4 Pointers

Pointers are variables that store memory addresses, allowing for dynamic memory management.

4.4.1 Definition of Pointer

A pointer is declared using the asterisk (*) symbol.

c
int *ptr; // ptr is a pointer to an integer
4.4.2 Address (&) and Indirection (*) Operator
  • Address Operator (&): Used to get the address of a variable.
    c
    int a = 10; ptr = &a; // ptr now holds the address of a
  • Indirection Operator (*): Used to access the value at the address stored in a pointer.
    c
    int value = *ptr; // value now holds the content of a
4.4.3 Pointer Expression and Assignment

Pointers can be used in expressions and can be assigned to other pointers.

c
int b = 20; int *ptr2 = &b; // ptr2 points to b
4.4.4 Call by Value and Call by Reference
  • Call by Value: A copy of the variable is passed.
  • Call by Reference: The address of the variable is passed, allowing modifications to the original variable.

4.5 Working with Files

C provides functions to perform operations on files, allowing data to be saved and retrieved.

4.5.1 Concept of Data File

A data file is used for storing data that can be read and written by a program.

4.5.2 Sequential and Random File
  • Sequential File: Data is accessed in a linear sequence.
  • Random File: Data can be accessed in any order.
4.5.3 File Manipulation Functions
  • putw(): Writes an integer to a file.
  • getw(): Reads an integer from a file.
  • putc(): Writes a character to a file.
  • getc(): Reads a character from a file.
  • fscanf(): Reads formatted data from a file.
  • fprintf(): Writes formatted data to a file.
4.5.4 Opening, Reading, Writing, and Appending Data File
  • Opening a File: Use fopen() with modes like "r" (read), "w" (write), and "a" (append).
    c
    FILE *fp = fopen("file.txt", "w");
  • Reading Data: Use functions like fscanf() or fgets() to read data.
  • Writing Data: Use fprintf() or fputs() to write data to a file.
  • Appending Data: Open the file in append mode ("a") to add data without overwriting.