C-programming notes

Chapter 1: Introduction to C

1. Pseudocode

  • Definition: Informal list of steps in English-like statements intended for human reading.

  • Usage: Used in planning computer program development to sketch program structure before coding.

Advantages of Pseudocode:

  1. Easy to write.

  2. Readable and understandable.

  3. Simple to modify.

Disadvantages of Pseudocode:

  1. No standard format exists.

  2. Variations exist for the same problem statement.

Examples:

  • Addition of two numbers:

    1. Begin

    2. Read two numbers a, b

    3. Calculate sum = a + b

    4. Display sum

    5. End

  • Area of rectangle:

    1. Begin

    2. Read length, breadth

    3. Calculate area = l * b

    4. Display area

    5. End

2. History of C

  • Overview: C is a general-purpose, procedural, structured programming language developed by Dennis Ritchie in 1972 at AT&T Bell Labs.

  • Development: Initially designed to write UNIX system software.

  • Predecessor: Successor to the B programming language.

  • C Standards: Includes K&R C, ANSI C, ISO C.

Characteristics of C:

  • Easy to learn.

  • General-purpose language.

  • Procedural and structured.

  • Portable and extensible.

Examples of C Applications:

  • Operating systems.

  • Language compilers.

  • Assemblers.

  • Text editors.

  • Databases.

3. Basic C Characters

  • Definition: C character set defines valid characters in a source program.

  • Components:

    1. Letters:

      • Uppercase: A, B, C, ..., Z

      • Lowercase: a, b, c, ..., z

    2. Digits: 0, 1, 2, ..., 9

    3. Special Characters: !, ., #, $, (, ), {, }, etc.

    4. White Spaces: Blank space, horizontal tab, carriage return, new line character ( ), form feed.

4. C Tokens

  • Definition: Smallest individual unit in a C program, forming the building blocks of the program.

  • Types of C Tokens: i. Keywords ii. Identifiers iii. Constants iv. Strings v. Operators vi. Special Symbols

4.1 Keywords:

  • Definition: Reserved words with predefined meanings. Must be in lowercase and cannot be identifiers.

  • Examples: auto, double, int, struct, break, do, else, long, switch, goto, sizeof, if, case, enum, register, typedef, default, char, extern, return, union, volatile, const, float, short, unsigned, while, continue, for, signed, void, static.

5. Identifiers

  • Definition: Names for objects such as variables, functions, and macros.

  • Rules:

    1. Must contain alphabets (A-Z, a-z), digits (0-9), and underscores (_).

    2. Must start with an alphabet or underscore.

    3. No special symbols except for underscore.

    4. No spaces allowed.

    5. Cannot be keywords.

    6. Case sensitive with a maximum length of 31 characters.

  • Valid/Invalid Examples:

    • Valid: integer, minimum, sum_total, row1, _cpps

    • Invalid: float (keyword), 123_Abc (starts with number), N1 + n2 (contains symbol).

6. Constants

  • Definition: Values that do not change during program execution.

  • Types:

    • Numeric Constants: Integer, Real constants.

    • Character Constants: Single and String constants.

6.1 Numeric Constants:

  • Integer Constant: Whole numbers without decimals. Can be decimal, octal (preceded by 0), or hexadecimal (preceded by 0x).

  • Real Constant: Decimal numbers, also known as floating-point constants, can be expressed in exponential form.

6.2 Character Constants:

  • Single Character Constant: Enclosed within single quotes, has corresponding ASCII values.

  • String Constant: Group of characters enclosed within double quotes, ends with NULL character ( ).

7. Operators in C

  • Definition: Symbols that tell the compiler to perform specific operations.

  • Types of Operators:

    1. Arithmetic Operators: +, -, *, /, %

    2. Relational Operators: <, >, <=, >=, ==, !=

    3. Logical Operators: NOT (!), AND (&&), OR (||)

    4. Assignment Operators: =, +=, -=, etc.

    5. Bitwise Operators: &, |, ^, ~, <<, >>

    6. Unary Operators: ++, -- (increment/decrement)

    7. Conditional Operator: ?:

    8. Special Operators: sizeof, comma operator.

7.1 Arithmetic Operators Example:

  • Examples:

    • Addition:

      • Syntax: a = b + c

    • Subtraction:

      • Syntax: a = b - c

    • Multiplication:

      • Syntax: a = b * c

8. Expressions

  • Definition: Combination of operands and operators.

  • Precedence and Associativity of operators determines the order of evaluation.

9. Data Types

  • Definition: Defines types of data stored in a variable.

  • Types:

    1. Primary: int, float, double, char, void.

    2. Derived: arrays, structures.

    3. User-defined: enum, typedef.

10. Variables

  • Definition: Named memory location whose value can change.

  • Declaration:

    • Syntax: datatype variable_list;

  • Initialization:

    • Syntax: datatype variable_name = value;

11. Type Conversion

  • Definition: Converting one data type to another.

  • Types:

    1. Automatic (Implicit) Conversion

    2. Manual (Explicit) Conversion

12. Structure of a C Program

  • Components:

    1. Documentation Section: Comments about the program.

    2. Link Section: Links library functions.

    3. Definition Section: Defines constants.

    4. Global Declaration Section: Declares global variables.

    5. Main Section: Contains executable code.

    6. Subprogram Section: Additional user-defined functions.

13. Example Programs

  • Example 1: Area and Perimeter of Rectangle:

    #include <stdio.h>
    void main() {
        int l, b, area, peri;
        printf("Enter length and breadth\n");
        scanf("%d%d", &l, &b);
        area = l * b;
        peri = 2 * (l + b);
        printf("The area is %d\n", area);
        printf("The perimeter is %d\n", peri);
    }
  • Example 2: Simple Interest:

    #include <stdio.h>
    void main() {
        float p, t, r, si;
        printf("Enter principle, time and rate\n");
        scanf("%f%f%f", &p, &t, &r);
        si = p * t * r / 100;
        printf("The Simple Interest is %f", si);
    }

14. Managing Input and Output Operations

  • Reading/Writing Data: Using functions such as scanf and printf for data input and output.

  • Character Functions: Using getchar for single character input and output with putchar.

15. Decision Making and Branching

  • Decision Making: Using if, switch, goto statements for control flow.

  • Example of if statement:

    if (condition) {
        // code block if condition is true
    } else {
        // code block if condition is false
    }
  • Example of switch statement:

    switch (variable) {
        case x:
            // code block
            break;
        default:
            // default code block
    }

16. Loops

  • Definition: Control structures for executing statements repeatedly based on a condition.

  • Types of Loops:

    1. while loop: Pre-test loop.

    2. do...while loop: Post-test loop.

    3. for loop: Entry-controlled loop.

17. Arrays

  • Definition: Collection of homogeneous elements accessed using subscript/index.

  • Types:

    1. One-Dimensional Arrays

    2. Two-Dimensional Arrays

    3. Multi-Dimensional Arrays

18. Strings

  • Definition: Group of characters enclosed in double quotes, ending with NULL character.

19. User Defined Functions

  • Definition: Custom functions created by programmers to perform specific tasks.

  • Example:

    void exampleFunction() {
        // function body
    }

20. Structures

  • Definition: Collection of heterogeneous elements of different data types.

  • Syntax: struct structureName { datatype member1; datatype member2; ... };

  • Array of Structures: Allow storage of multiple records.

21. Pointers

  • Definition: Variable that stores the address of another variable.

  • Usage: For dynamic memory management and efficient array handling.

22. Conclusion

  • C programming provides foundational constructs for structured programming, data management, and memory handling essential for building efficient software applications.