💻

Introduction to C Programming Language

Aug 24, 2024

C Programming Language Lecture Notes

Introduction to C Language

  • Invented by Dennis Ritchie at AT&T Bell Laboratories in the early 1970s.
  • Influenced by earlier B language and its ancestors.
  • Middle Level Language: Used for system-level programming (like OS) and application development (games).

Characteristics of C Language

  • Structured Programming: Programs organized into functions and modules.
  • Object-Oriented Programming: C++ is the extension of C for OOP.

Getting Started with C

  • Compiler Required: Install GCC or Clang.
  • File Extension: C files end with .c.
  • Main Function: Every C program requires a mandatory main() function.
  • Header Files: Use #include to include other C files, ending with .h (e.g., #include <stdio.h>).

Basic Concepts

Keywords and Data Types

  • Keywords: 32 keywords in C language.
  • Primary Data Types:
    • char: Characters.
    • int: Integer values.
    • float: Decimal values (up to 6 decimal places).
    • double: Decimal values (up to 15 decimal places).
    • void: No value (often used in functions).

Variables

  • Declare variable: specify data type and variable name.
  • Rules for Declaring Variables: Refer to specific rules given in the course.
  • Assigning Values: Can be done upon declaration or later.
  • Statements End with Semicolon.

Input and Output

  • Printing Output: Use printf() with format specifiers.
    • Example: printf("%d", variable_name);
  • Reading Input: Use scanf().
    • Example: scanf("%d", &variable_name); (use & for address).

Operators

  • Arithmetic Operators: For basic calculations.
  • Relational Operators: For comparing values.
  • Logical Operators: Combine expressions (e.g., &&, ||).

Control Structures

Conditional Statements

  • if Statement: Syntax: if (condition) { // statements }
  • else Statement: Follows if to execute if the condition is false.
  • Nested if-else: Allows multiple conditions.
  • Switch Statement: Selects block of code based on variable value.
    • Syntax: switch(variable) { case value: // statements; break; }

Loops

  • Types of Loops:
    • while Loop: Checks condition before executing.
    • do-while Loop: Executes at least once before checking the condition.
    • for Loop: More control over iterations.
      • Syntax: for (initialization; condition; increment/decrement) { // code }

Functions

  • Definition: Block of code performing a specific task.
  • Declaration: Return type, function name, parameters.
  • Calling a Function: Use the function name with arguments.

Arrays and Pointers

Arrays

  • Collection of data items of the same type.
  • Syntax: data_type array_name[size];
  • Access elements using index (starting from 0).

Pointers

  • Variables that store memory addresses.
  • Declare using * before the variable name.
  • Use & to get the address and * to dereference.

Strings and Structures

Strings

  • Strings are arrays of characters.
  • Use char string_name[size];
  • Format specifier for strings: %s.

Structures

  • Definition: Collection of different data items.
  • Declaration: struct structure_name { // items };
  • Access elements using dot notation.

Unions

  • Allows storing different data types in the same memory location.
  • Only one member can hold a value at a time.
  • Declaration similar to structures but use union.

Comments

  • Single Line Comments: Use //.
  • Multi-Line Comments: Start with /* and end with */.

Compiling C Programs

  • Use GCC: gcc filename.c -o output_filename

Conclusion

  • Understanding these basics is crucial for effective programming in C.