Basics of C, Variables & Data Types

Expert Answer & Key Takeaways

Deep dive into the foundational elements of the C programming language: history, compilation process, tokens, variables, data types, and operators.

Basics of C, Variables & Data Types

C is a foundational, middle-level, procedural programming language developed by Dennis Ritchie at Bell Labs in 1972. It was originally created to develop the UNIX operating system. Because of its incredible speed and close-to-hardware capabilities, C is often called the 'mother of all programming languages'.

1. The Structure of a C Program

A standard C program follows a specific structure:
// 1. Preprocessor Directives #include <stdio.h> // 2. Main Function int main() { // 3. Variable Declaration & Execution printf("Hello, World!"); // 4. Return Statement return 0; }
  • #include <stdio.h>: Instructs the preprocessor to include the Standard Input/Output header file, which contains definitions for printf() and scanf().
  • int main(): The entry point of every C program. Execution always begins here.
  • printf(): A built-in function used to print output to the console.
  • return 0;: Indicates that the program terminated successfully. A non-zero return indicates an error.

2. Compilation Process in C

C is a compiled language. The process of converting human-readable source code (.c file) into machine code (.exe file) involves 4 steps:
  1. Preprocessing: Removes comments, expands macros (#define), and includes header files. Output is an expanded .i file.
  2. Compilation: Converts the expanded code into Assembly code (.s file).
  3. Assembly: Converts the assembly code into pure machine code/object code (.o or .obj file).
  4. Linking: Combines one or more object files and library files to generate the final executable file (.exe).

3. C Tokens

A token is the smallest individual unit in a C program. The C compiler breaks the source code into tokens.
  1. Keywords: 32 reserved words with predefined meanings (e.g., int, if, while, return).
  2. Identifiers: Names given to variables, functions, and arrays by the programmer. Rules:
    • Must start with a letter or underscore (_).
    • Cannot contain spaces or special symbols (except _).
    • Cannot be a keyword.
  3. Constants: Fixed values that do not change during execution (e.g., 10, 'A', 3.14).
  4. Strings: A sequence of characters enclosed in double quotes (e.g., "Hello").
  5. Operators: Symbols that perform mathematical or logical operations (e.g., +, -, &&).
  6. Special Symbols: Characters with special syntactic meaning (e.g., {}, (), ;, ,).

4. Data Types

Data types specify the type and size of data that a variable can hold.

4.1 Primary (Primitive) Data Types

  • int: Used for integers (whole numbers).
    • Size: 2 or 4 bytes (depending on compiler/architecture).
    • Format Specifier: %d or %i
  • char: Used for single characters. Internally stored as ASCII integer values.
    • Size: 1 byte.
    • Format Specifier: %c
  • float: Used for single-precision decimal numbers.
    • Size: 4 bytes. Precision: 6 decimal places.
    • Format Specifier: %f
  • double: Used for double-precision decimal numbers.
    • Size: 8 bytes. Precision: 15 decimal places.
    • Format Specifier: %lf
  • void: Means 'no value'. Used to indicate a function returns nothing.

4.2 Derived & User-Defined Types

  • Derived: Arrays, Pointers, Functions.
  • User-Defined: struct, union, enum, typedef.

5. Variables & Constants

  • Variable: A named memory location whose value can change during execution. Declaration: int age = 25;
  • Constant: A value that cannot be altered. Created using the const keyword or #define macro.
    • Example: const float PI = 3.14;

6. Operators in C

  1. Arithmetic: +, -, *, /, % (Modulus: returns remainder. Cannot be used with floats).
  2. Relational: ==, !=, >, <, >=, <= (Returns 1 for true, 0 for false).
  3. Logical:
    • && (Logical AND): True if both operands are true.
    • || (Logical OR): True if at least one operand is true.
    • ! (Logical NOT): Reverses the logical state.
  4. Assignment: =, +=, -=, *=, /=. (e.g., a += 5 is a = a + 5).
  5. Increment/Decrement: ++, --.
    • Pre-increment (++a): Increase first, then use.
    • Post-increment (a++): Use first, then increase.
  6. Bitwise: Operates on bits (&, |, ^, ~, <<, >>).
  7. Ternary (Conditional): condition ? expression1 : expression2; (Shorthand for if-else).

Course4All Editorial Board

Verified Expert

Subject Matter Experts

Comprising experienced educators and curriculum specialists dedicated to providing accurate, exam-aligned preparation material.

Pattern: 2026 Ready
Updated: Weekly