24ESE1212-A : C Programming

Author Published
Gokulnath G 04-01-2025

Module I

Syllabus

Character Set – Tokens – Identifiers - Keywords - Constants – Variables. Operators - Arithmetic Operators - Relational Operators - Logical Operators -Assignment Operators Increment and Decrement Operators - Conditional Operator - Bitwise Operators Special Operators Operator Precedence and Associativity - Expressions and their evaluation. Datatypes - Statements – Formatted I/O statements – Comments Basic Structure of a C Program. Conditional Statements – if statements - switch -Iterative Statements – for – while - do- while.

Brief Notes

Character Set

The set of valid characters used in a programming language, including letters, digits, special symbols, and white spaces.

Tokens

The smallest unit in a program, such as keywords, identifiers, constants, operators, and special symbols.

Identifiers

Names used to identify variables, functions, or other user-defined elements in a program.

Keywords

Predefined reserved words with special meaning in C, such as int, return, if, etc.

Constants

Fixed values in a program that do not change during execution, such as numbers, characters, and strings.

Variables

Memory locations used to store data that can be modified during program execution.

Operators

Operator Precedence and Associativity

Define the order in which operators are evaluated in an expression.

Expressions and Evaluation

Combine variables, constants, and operators to compute values.

Data Types

Define the type of data a variable can hold, such as int, float, char, double.

Statements

Instructions in a program that perform specific actions. Types include expression statements, conditional statements, and iterative statements.

Formatted I/O Statements

Functions like printf and scanf for formatted input and output in C.

Comments

Annotations in code to improve readability, ignored during execution:

Basic Structure of a C Program

  1. Preprocessor directives (#include).
  2. Main function (int main()).
  3. Variable declarations.
  4. Program logic within { }.
  5. Return statement (return 0;).

Conditional Statements

Control flow based on conditions:

Iterative Statements

Used for loops to repeat blocks of code:

Module II

Syllabus

Need for functions - Library Functions – User-defined Functions - Elements of User-defined Functions - Definition of Functions - Return Values and their Types - Function Calls - Function Declaration - Category of Functions - No Arguments and No Return Values - Arguments but no return values - Arguments with return values - No Arguments but Returns a Value – Recursion – Scope - Visibility and Lifetime of variables - Nested Functions

Functions in C

Need for Functions

Functions help in modular programming by breaking a program into smaller, manageable parts. They improve code readability, reusability, and maintainability.

Library Functions

These are pre-defined functions available in standard C libraries, such as printf(), scanf(), sqrt(), and strlen().

User-defined Functions

Functions created by the user to perform specific tasks. They are defined using the return_type function_name(parameters) { } syntax.

Elements of User-defined Functions

Return Values and Their Types

Functions can return values of different data types, such as int, float, or void (no return value).

Category of Functions

Recursion

A function calling itself to solve a problem in a repetitive manner. Example:

            int factorial(int n) {
                 if (n == 0) return 1;
                 return n * factorial(n - 1);
            }
        

Scope, Visibility, and Lifetime of Variables

Nested Functions

A function defined within another function. In C, true nested functions are not allowed, but function calls within functions are commonly used.

Module III

Syllabus

One-dimensional Arrays: Declaration of One-dimensional Arrays - Initialization of One dimensional Arrays - Example programs - Bubble sort - Linear search - Binary search. Two-dimensional Arrays: Declaration of Two-dimensional Arrays - Initialization of Two dimensional Arrays - Example programs - Matrix Multiplication - Transpose of a matrix. Strings in C: character arrays and string manipulation functions - Example Programs (with and without using built-in string functions) - Passing arrays and strings to functions

Arrays and Strings in C

One-dimensional Arrays

Arrays store multiple elements of the same type in contiguous memory locations. A one-dimensional array is declared as:

int arr[10];

Initialization of One-dimensional Arrays

Arrays can be initialized during declaration:

int arr[5] = {1, 2, 3, 4, 5};

Example Programs

Two-dimensional Arrays

A two-dimensional array is declared as:

int matrix[3][3];

Initialization of Two-dimensional Arrays

They can be initialized during declaration:

int matrix[2][2] = {{1, 2}, {3, 4}};

Example Programs

Strings in C

Strings are character arrays that end with a null character (\0).

char str[] = "Hello";

String Manipulation Functions

Example Programs

Module IV

Syllabus

Structures: Introduction -Defining a structure - declaring structure variables - Accessing structure members - Structure initialization - Array of structures - Nested Structure. Introduction to pointers and their significance - Pointer arithmetic and pointer operations. Dynamic memory allocation (malloc, calloc, realloc, free) - Pointer and array relationship - Pointers to functions and their applications - Call by value and call by reference. File input/output operations (fopen,fclose,fread,fwrite) - Error handling with file operations.

Structures, Pointers, and File I/O in C

Structures in C

A structure is a user-defined data type that allows grouping related data of different types.

Defining a Structure

            struct Student {
            char name[50];
            int age;
            float marks;
            };
        

Declaring Structure Variables

        struct Student s1;
        

Accessing Structure Members

            s1.age = 20;
            printf("Age: %d", s1.age);
        

Structure Initialization

            struct Student s2 = {"John", 21, 85.5};
        

Array of Structures

        struct Student students[3];
        

Nested Structures

        struct Address {
            char city[50];
            int zip;
        };
        struct Student {
            char name[50];
            struct Address addr;
        };
        

Pointers in C

Pointers store memory addresses, enabling efficient data manipulation and dynamic memory allocation.

Pointer Declaration

        int *ptr;
        

Pointer Arithmetic

        ptr++;
        ptr--;
        

Dynamic Memory Allocation

Allocating memory dynamically at runtime using functions from <stdlib.h>.

Memory Allocation Functions

Example of Dynamic Memory Allocation

        int *arr = (int*)malloc(5 * sizeof(int));
        free(arr);
        

Pointers and Arrays

Pointers can be used to access array elements efficiently.

        int arr[] = {1, 2, 3};
        int *ptr = arr;  // Points to first element
        printf("%d", *(ptr + 1)); // Access second element
        

Pointers to Functions

Pointers can store addresses of functions, allowing function calls through pointers.

        void hello() { printf("Hello, World!"); }
        void (*funcPtr)() = hello;
        funcPtr(); // Calls hello()
        

Call by Value and Call by Reference

Functions can receive arguments either by value (copy) or by reference (address).

Call by Value Example

        void increment(int x) {
            x++;
        }
        

Call by Reference Example

        void increment(int *x) {
            (*x)++;
        }
                

File Input/Output Operations

Files in C are handled using the FILE pointer and functions from <stdio.h>.

Opening and Closing a File

        FILE *fp = fopen("data.txt", "r");
        fclose(fp);
        

Reading and Writing Files

            // Writing to a file
            FILE *fp = fopen("data.txt", "w");
            fprintf(fp, "Hello, File!");
            fclose(fp);

            // Reading from a file
            char buffer[100];
            fp = fopen("data.txt", "r");
            fgets(buffer, 100, fp);
            printf("%s", buffer);
            fclose(fp);
        

Error Handling with File Operations

Checking if a file operation was successful to prevent errors.

            FILE *fp = fopen("data.txt", "r");
            if (fp == NULL) {
                printf("Error opening file!");
            } else {
                fclose(fp);
            }
        

Join the Discussion

Click here to participate in discussions