3.1 Introduction to C Programming

C is a general-purpose, procedural, and middle-level programming language used for developing computer software, system programming, applications, games, and more. Known for its simplicity and efficiency, C is an excellent choice for beginners as it provides a strong foundation in programming concepts. C was developed by Dennis M. Ritchie at Bell Laboratories in 1972.

1. C Tokens

  • C Tokens are the building blocks of a C program. They are categorized into:

    • Keywords: Reserved words (e.g., int, if, while, return).

    • Identifiers: Names for variables, functions, and arrays (e.g., main, num).

    • Constants: Fixed values used in a program (e.g., 10, 'A').

    • Strings: Sequence of characters enclosed in double quotes (e.g., "Hello").

    • Operators: Symbols that perform operations (e.g., +, -, *, /).

    • Special Characters: Punctuation marks with specific functions (e.g., {, }, ;).


2. Operators

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

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

  • Logical Operators: &&, ||, !.

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

  • Assignment Operators: =, +=, -=, *=, /=, %=.

  • Increment/Decrement: ++, --.


3. Input and Output (Formatted and Unformatted)

  • Formatted I/O:

    • Input: scanf() (e.g., scanf("%d", &num);)

    • Output: printf() (e.g., printf("Value: %d", num);)

  • Unformatted I/O:

    • Input: getchar(), gets() (e.g., char c = getchar();)

    • Output: putchar(), puts() (e.g., puts("Hello");)


4. Control Statements

  • Decision-Making:

    • if, if-else, else if, switch.

    • Example:

      if (a > b) {
          printf("A is greater");
      } else {
          printf("B is greater");
      }
  • Looping:

    • For Loop: Executes a block of code a fixed number of times.

      for (int i = 0; i < 10; i++) {
          printf("%d\n", i);
      }
    • While Loop: Executes as long as a condition is true.

      codeint i = 0;
      while (i < 10) {
          printf("%d\n", i);
          i++;
      }
    • Do-While Loop: Executes at least once, then continues based on a condition.

      int i = 0;
      do {
          printf("%d\n", i);
          i++;
      } while (i < 10);

5. Functions

  • User-Defined Functions:

    • Functions created by the user for modularity and reusability.

    • Syntax:

      return_type function_name(parameters) {
          // Function body
          return value;
      }
    • Example:

      int add(int a, int b) {
          return a + b;
      }
  • Recursive Functions:

    • Functions that call themselves, typically used for tasks like calculating factorials or solving Fibonacci series.

    • Example:

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

6. Arrays

  • 1D Arrays:

    • Syntax:

      int arr[10];
    • Example:

      int arr[5] = {1, 2, 3, 4, 5};
      for (int i = 0; i < 5; i++) {
          printf("%d ", arr[i]);
      }
  • 2D Arrays:

    • Syntax:

      int matrix[3][3];
    • Example:

      int matrix[2][2] = {{1, 2}, {3, 4}};
      for (int i = 0; i < 2; i++) {
          for (int j = 0; j < 2; j++) {
              printf("%d ", matrix[i][j]);
          }
      }
  • Multi-Dimensional Arrays:

    • Arrays with more than two dimensions (rarely used).


7. String Manipulations

  • String Functions (from <string.h>):

    • strlen(): Finds the length of a string.

    • strcpy(): Copies one string to another.

    • strcat(): Concatenates two strings.

    • strcmp(): Compares two strings.

    • Example:

      char str1[10] = "Hello";
      char str2[10];
      strcpy(str2, str1);
      printf("%s", str2); // Output: Hello

Conclusion

C is a versatile and efficient language that lays the groundwork for programming concepts. Its features, such as tokens, operators, control statements, arrays, functions, and string manipulations, empower developers to write structured and robust code. C’s simplicity and modularity make it an ideal choice for beginners and a powerful tool for developing a wide range of applications, from system software to games.

Last updated