Menu

Chapter 5: Programming Concepts and the C Programming Language

Computer Science - Class 11

This chapter introduces core programming concepts, including language types, translators, errors, control structures, and design tools. It then dives into the C programming language, covering its syntax, data types, operators, control statements, arrays, strings, and essential library functions.

No MCQ questions available for this chapter.

Chapter 5: Programming Concepts and the C Programming Language

5.1 Programming Concept

5.1.1 Introduction to Programming Languages

A programming language is a set of instructions that enables humans to communicate with computers. Its primary purposes are problem solving, automation of repetitive tasks, and application development.

5.1.2 Low-level, High-level, and 4GL

  • Low-level languages: Directly interact with hardware.
    • Machine language: Binary code (0s and 1s).
    • Assembly language: Mnemonics representing machine instructions.
    Characteristics: Fast execution, hardware‑dependent, difficult to debug and maintain.
  • High-level languages: Human‑readable syntax (e.g., C, Java, Python). Characteristics: Portable across platforms, easier to write and debug, require a compiler or interpreter.
  • Fourth Generation Languages (4GL): Very high‑level, closer to natural language (e.g., SQL, report generators). Characteristics: Minimal coding needed, focus on what to do rather than how.

Comparison Table

Type Examples Abstraction Level Execution Speed Portability Ease of Use
Low‑level Machine code, Assembly Closest to hardware Fastest Low (hardware‑specific) Difficult
High‑level C, Java, Python Medium Moderate High Easy
4GL SQL, RPG Highest (near natural language) Variable (often slower) High Very easy

5.1.3 Compiler, Interpreter, and Assembler

  • Compiler: Translates the entire source code into machine code before execution (e.g., C, C++).
    • Produces an example
    • Faster runtime performance.
  • Interpreter: Translates and executes code line‑by‑line (e.g., Python, JavaScript).
    • No separate executable file.
    • Easier debugging; errors reported immediately.
  • Assembler: Converts assembly language mnemonics into machine code.

Compiler vs Interpreter

Aspect Compiler Interpreter
Translation Entire program at once Line by line
Output Executable file No executable
Execution Speed Fast Slower
Debugging Harder (errors after compile) Easier (immediate feedback)
Examples C, C++, Go Python, JavaScript, PHP

5.1.4 Syntax, Semantic, and Runtime Errors

  • Syntax errors: Violations of language grammar (e.g., missing semicolon, mismatched brackets). Detected at compile time.
  • Semantic errors: Logical mistakes; program compiles but yields wrong results (e.g., using wrong formula, incorrect variable).
  • Runtime errors: Occur during execution (e.g., division by zero, accessing invalid memory). Cause program crash or exception.

5.1.5 Control Structures

Control structures dictate the flow of execution.

  • Sequence: Statements executed in the order they appear (top‑to‑bottom).
  • Selection: Conditional branching using if, if‑else, switch.
  • Iteration: Repetition via loops (while, for, do‑while).

Flowchart Diagram (description)

A flowchart uses symbols: Terminal (start/end), Input/Output (parallelogram), Process (rectangle), Decision (diamond), and Connector (circle). Arrows show flow direction.

5.1.6 Program Design Tools

  • Algorithm: Step‑by‑step procedure to solve a problem. Characteristics: finite, definite, clear input/output.
  • Flowchart: Graphical representation of an algorithm using standard symbols.
  • Pseudocode: Informal, English‑like description of logic, not bound to any language syntax.

Comparison of Tools

Tool Representation Advantages Disadvantages
Algorithm Textual steps Language‑independent, easy to modify No visual flow
Flowchart Graphical symbols Visual clarity, good for presentations Time‑consuming to draw, hard to update
Pseudocode Structured English Bridge between algorithm and code, concise No standard syntax, may be ambiguous

5.1.7 Absolute Binary, BCD, ASCII, Unicode

  • Absolute Binary: Direct binary representation of a number (e.g., decimal 13 → 1101).
  • BCD (Binary Coded Decimal): Each decimal digit encoded in 4 bits.
    Example: 92 → 1001 0010 (9 = 1001, 2 = 0010).
  • ASCII: 7‑bit code, 128 characters.
    Examples: 'A' = 65, 'a' = 97, '0' = 48.
  • Unicode: 16‑ or 32‑bit encoding covering virtually all written symbols.
    Examples: '€' (Euro sign) = U+20AC, '你好' (Chinese) = U+4F60 U+597D.

5.2 C Programming Language

5.2.1 Introduction and Features of C

Developed by Dennis Ritchie at Bell Labs in 1972, C is a structured, portable, fast language offering low‑level memory access and a rich standard library. It underpins operating systems (Linux, Windows kernels), embedded systems, and databases.

5.2.2 Structure of a C Program

  1. Header section (#include directives).
  2. Main function: int main().
  3. Variable declarations.
  4. Statements and expressions.
  5. Return statement (return 0;).

Example Complete C Program

#include <stdio.h>

int main() {
    printf("Hello, World!\\n");
    return 0;
}

5.2.3 C Preprocessor and Header Files

The preprocessor handles directives before actual compilation.

  • #include <stdio.h> – includes standard I/O header from system directories.
  • #include "myfile.h" – includes user‑defined header from the current directory.
  • Macro definition: #define PI 3.14 replaces PI with 3.14 throughout the code.

5.2.4 Character Set

  • Letters: A‑Z, a‑z.
  • Digits: 0‑9.
  • Special characters: + - * / = ; { } ( ) and others.
  • White space: space, tab (\\t), newline (\\n).

5.2.5 Use of Comments

  • Single‑line: // This is a comment
  • Multi‑line: /* This is a multi‑line comment */
  • Purpose: Documentation, debugging, temporarily disabling code.

5.2.6 Identifiers, Keywords, and Tokens

  • Identifiers: Names for variables, functions; must start with a letter or underscore, case‑sensitive.
  • Keywords: Reserved words (int, float, char, if, else, while, for, return, etc.).
  • Tokens: Smallest units of the language (keywords, identifiers, operators, constants, string literals).

5.2.7 Basic Data Types in C

Type Size (typical) Range / Precision
int 2 or 4 bytes -32,768 to 32,767 (2‑byte) or larger (4‑byte)
float 4 bytes 6 decimal digits precision
double 8 bytes 15 decimal digits precision
char 1 byte ASCII character (0‑255)
short 2 bytes Smaller integer range
long 4 or 8 bytes Larger integer range
signed / unsigned Modifiers Allow negative values or double the positive range

5.2.8 Constants and Variables

  • Constants: Fixed values.
    Using const: const int MAX = 100;
  • Using #define: #define PI 3.14
  • Variables: Named storage locations.
    Declaration: int age;
  • Initialization: int age = 25;

5.2.9 Types of Specifier (for printf/scanf)

  • %d – decimal integer
  • %f – float
  • %c – character
  • %s – string
  • %lf – double
  • %u – unsigned integer

5.2.10 Simple and Compound Statements

  • Simple statement: Ends with a semicolon, e.g., x = 10;
  • Compound statement: Block of statements enclosed in braces { }, e.g.,
    {
        int a = 5;
        int b = 10;
        printf("%d\\n", a + b);
    }
    
  • 5.2.11 Operators and Expressions

    • Arithmetic: + - * / %
    • Relational: == != < > <= >=
    • Logical: &&& || !
    • Assignment: = += -= *= /= %=
    • Unary: ++ -- sizeof
    • Conditional (ternary): condition ? expr1 : expr2

    Operator Precedence (high → low)

    1. Postfix: () [] -> . ++ --
    2. Unary: + - ! ~ ++ -- (type)* sizeof
    3. Multiplicative: * / %
    4. Additive: + -
    5. Shift: << >>
    6. Relational: < > <= >=
    7. Equality: == !=
    8. Bitwise AND: &
    9. Bitwise XOR: ^
    10. Bitwise OR: |
    11. Logical AND: &&
    12. Logical OR: ||
    13. Conditional: ? :
    14. Assignment: = += -= *= /= %= &= ^= |= <<= >>=
    15. Comma: ,

    5.2.12 Input/Output Functions

    • printf(): Formatted output.
      Example: printf("Value: %d\\n", x);
    • scanf(): Formatted input.
      Example: scanf("%d", &x);
    • getchar() / putchar(): Single character I/O.
    • gets() / puts(): String I/O (note: gets is unsafe).

    5.2.13 Selection Control Statements

    • if statement:
      if (condition) { /* statements */ }
    • if‑else:
      if (condition) { /* true block */ } else { /* false block */ }
    • if‑else‑if ladder:
      if (cond1) { … } else if (cond2) { … } else { … }
    • Nested if: An if inside another if block.
    • switch: Multi‑way branching.
      switch (expression) { case const1: …; break; case const2: …; break; default: …; }
    • Use of break to exit switch or loops; default handles unspecified cases.

    5.2.14 Iteration Control Statements

    • while (pre‑test):
      while (condition) { /* body */ }
    • do‑while (post‑test):
      do { /* body */ } while (condition);
    • for (compact):
      for (init; condition; update) { /* body */ }
    • Nested loops: Loop inside another loop (e.g., for matrix processing).
    • break: Exits the nearest enclosing loop or switch.
    • continue: Skips the remainder of the current iteration and proceeds with the next.

    5.2.15 Arrays

    • Array: Contiguous collection of same‑type elements.
    • 1‑D Array:
      int arr[5] = {1, 2, 3, 4, 5};
    • 2‑D Array:
      int matrix[3][3];
    • Matrix addition (element‑wise):
      C[i][j] = A[i][j] + B[i][j];
    • Matrix subtraction:
      C[i][j] = A[i][j] - B[i][j];
    • Array indexing is 0‑based: first element at index 0.

    5.2.16 Strings

    • A string is an array of characters terminated by the null character '\\0'.
    • Key functions from <string.h>:
      • strlen(s) – returns length (excluding null).
      • strcat(dest, src) – concatenates src to dest.
      • strcmp(s1, s2) – compares two strings (0 if equal).
      • strrev(s) – reverses string (non‑standard, often implemented manually).
      • strcpy(dest, src) – copies src to dest.
      • strlwr(s) – converts to lowercase.
      • strupr(s) – converts to uppercase.
    • Example usage:
      char name[20] = "Alice";
      printf("Length: %zu\\n", strlen(name));
      strlwr(name);
      printf("Lowercase: %s\\n", name);
      

    Summary

    This chapter laid the theoretical foundation of programming languages, translators, errors, control flow, and design tools, then transitioned into practical C programming—covering syntax, data types, operators, I/O, control statements, arrays, and strings. Mastery of these concepts enables students to write efficient, correct C programs and prepares them for more advanced topics.