⬅ Previous Next ➡

Basic Structure & Fundamentals

Basic Structure & Fundamentals of C
  • Structure of a C Program includes: header files, global declarations, main() function, statements, and return.
  • Compilation Process: Source code (.c) is converted into executable using compiler (gcc/clang).
  • Execution Flow: Program starts from main(), executes statements in order, then returns.
  • Tokens are smallest elements in C: keywords, identifiers, constants, strings, operators, punctuators.
  • Keywords are reserved words (cannot be used as variable names): int, float, if, else, for, while, return, etc.
  • Identifiers are user-defined names (variables, functions): must start with letter/_ , no spaces, case-sensitive.
  • Variables store values and must be declared with a data type.
  • Constants are fixed values that do not change (e.g., 10, 3.14, 'A').
  • Data Types:
    • int (integer), float (decimal), double (more precision), char (single character)
    • void (no value), short, long, signed, unsigned (modifiers)
  • Input/Output Functions: printf() for output and scanf() for input (from stdio.h).
  • Comments:
    • // single-line comment
    • /* ... */ multi-line comment
#include <stdio.h>   // Header file (for printf, scanf)

// Multi-line comment example:
// This is a multi-line comment
// C Program: basic structure

int main() {

    // Variable declarations
    int age = 0;
    float marks = 0.0f;
    char grade = 'A';

    // Output
    printf("Enter age: ");
    scanf("%d", &age);

    printf("Enter marks: ");
    scanf("%f", &marks);

    // Execution flow: statements run line by line
    printf("\n--- Output ---\n");
    printf("Age = %d\n", age);
    printf("Marks = %.2f\n", marks);
    printf("Grade = %c\n", grade);

    return 0; // program ends here
}
// Compilation (GCC):
// 1) Preprocessing: handles #include, #define
// 2) Compilation: converts C to assembly
// 3) Assembly: converts to object code (.o)
// 4) Linking: links libraries to make executable
//
// Example commands:
// gcc program.c -o program
// ./program
⬅ Previous Next ➡