Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Chapter 3: Core Concepts

3.1 Data types and variables

In C, every variable has a defined type, and you have to declare it explicitly. Python variables are dynamically typed and can change type while the program runs.

Common data types in C
  • int: Integer type
  • float: Single-precision floating-point type
  • double: Double-precision floating-point type
  • char: Single character type
  • void: Represents the absence of type

Python:

x = 10          # Integer
y = 20.5        # Float
name = "Alice"  # String

C:

#include <stdio.h>

int main() {
    int x = 10;           // Integer
    float y = 20.5;       // Float
    double z = 30.15;     // Double
    char ch = 'A';        // Character

    printf("Integer: %d\n", x);
    printf("Float: %.2f\n", y);
    printf("Double: %.2lf\n", z);
    printf("Character: %c\n", ch);

    return 0;
}

Explanation:

  • In Python, you create a variable by assigning a value to it, and the type is inferred at runtime.
  • In C, you declare a variable with a specific type before you use it, and the type is fixed at compile time.

3.2 Operators and expressions

Like Python, C has a large set of operators for working with variables, though some of them behave differently.

3.2.1 Arithmetic operators
  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • %: Modulus (remainder)

Python:

a = 10
b = 3
print(a + b)  # 13
print(a - b)  # 7
print(a * b)  # 30
print(a / b)  # 3.3333333333333335
print(a % b)  # 1

C:

#include <stdio.h>

int main() {
    int a = 10, b = 3;
    printf("a + b = %d\n", a + b);
    printf("a - b = %d\n", a - b);
    printf("a * b = %d\n", a * b);
    printf("a / b = %d\n", a / b);  // Integer division
    printf("a %% b = %d\n", a % b); // Modulus

    return 0;
}

Explanation:

  • In Python, / performs floating-point division by default.
  • In C, / performs integer division when both operands are integers. For floating-point division, at least one operand must be a float or double.
3.2.2 Comparison operators
  • ==: Equal to
  • !=: Not equal to
  • >: Greater than
  • <: Less than
  • >=: Greater than or equal to
  • <=: Less than or equal to

Python:

a = 5
b = 10
print(a == b)  # False
print(a != b)  # True
print(a > b)   # False
print(a < b)   # True
print(a >= b)  # False
print(a <= b)  # True

C:

#include <stdio.h>

int main() {
    int a = 5, b = 10;
    printf("a == b: %d\n", a == b);
    printf("a != b: %d\n", a != b);
    printf("a > b: %d\n", a > b);
    printf("a < b: %d\n", a < b);
    printf("a >= b: %d\n", a >= b);
    printf("a <= b: %d\n", a <= b);

    return 0;
}

Explanation:

  • Python and C use similar comparison operators. In C, a comparison gives 0 (false) or 1 (true), and you can use that result directly in arithmetic expressions.
3.2.3 Logical operators
  • &&: Logical AND
  • ||: Logical OR
  • !: Logical NOT

Python:

a = True
b = False
print(a and b)  # False
print(a or b)   # True
print(not a)    # False

C:

#include <stdio.h>

int main() {
    int a = 1, b = 0; // In C, 1 is true and 0 is false
    printf("a && b: %d\n", a && b);
    printf("a || b: %d\n", a || b);
    printf("!a: %d\n", !a);

    return 0;
}

Explanation:

  • Python’s logical operators are the words and, or, and not.
  • C uses the symbols &&, ||, and !.

3.3 Control structures

Control structures direct the flow of execution in a program. Python and C have similar ones, written with different syntax.

3.3.1 Conditionals
  • if
  • else if (elif in Python)
  • else

Python:

x = 10

if x > 0:
    print("Positive")
elif x == 0:
    print("Zero")
else:
    print("Negative")

C:

#include <stdio.h>

int main() {
    int x = 10;

    if (x > 0) {
        printf("Positive\n");
    } else if (x == 0) {
        printf("Zero\n");
    } else {
        printf("Negative\n");
    }

    return 0;
}

Explanation:

  • For additional conditions, Python uses elif and C uses else if.
3.3.2 Loops
  • for
  • while
  • do-while (only in C)

Python:

# For loop
for i in range(5):
    print(i)

# While loop
i = 0
while i < 5:
    print(i)
    i += 1

C:

#include <stdio.h>

int main() {
    // For loop
    for (int i = 0; i < 5; i++) {
        printf("i = %d\n", i);
    }

    // While loop
    int i = 0;
    while (i < 5) {
        printf("i = %d\n", i);
        i++;
    }

    // Do-while loop
    i = 0;
    do {
        printf("i = %d\n", i);
        i++;
    } while (i < 5);

    return 0;
}

Explanation:

  • Python’s for loop iterates over sequences. C’s for loop is more general: you write the initialization, condition, and increment statements yourself.
  • C also has a do-while loop, whose body always runs at least once.

3.4 Functions

3.4.1 Function definition

A function is a reusable block of code that performs a specific task. Python and C both have functions, but the syntax differs, and so do some details of how you use them.

Python:

def add(a, b):
    return a + b

result = add(3, 4)
print("Sum:", result)

C:

#include <stdio.h>

// Function declaration
int add(int a, int b);

int main() {
    int result = add(3, 4);
    printf("Sum: %d\n", result);
    return 0;
}

// Function definition
int add(int a, int b) {
    return a + b;
}

Explanation:

  • In Python, you define a function with the def keyword and don’t declare types for its parameters or return value.
  • In C, a function must be declared before it is used (with a prototype or a definition), and you must specify the types of its parameters and return value.
3.4.2 Function prototypes

A function prototype is a declaration that gives a function’s return type and parameters. Prototypes usually go at the beginning of the file, before the main function.

Python: Function definitions can appear in any order, as long as each function is defined before it is called.

C:

#include <stdio.h>

// Function prototypes
void greet();
int multiply(int a, int b);

int main() {
    greet();
    int result = multiply(3, 4);
    printf("Product: %d\n", result);
    return 0;
}

void greet() {
    printf("Hello, World!\n");
}

int multiply(int a, int b) {
    return a * b;
}
3.4.3 Scope and lifetime of variables

Where you declare a variable in C determines its scope and lifetime.

Python:

global_var = 10  # Global variable

def print_global():
    print("Global variable:", global_var)

def main():
    local_var = 20  # Local variable
    print("Local variable:", local_var)
    print_global()

main()

C:

#include <stdio.h>

// Global variable
int globalVar = 10;

void printGlobalVar() {
    printf("Global variable: %d\n", globalVar);
}

int main() {
    // Local variable
    int localVar = 20;
    printf("Local variable: %d\n", localVar);

    printGlobalVar();
    return 0;
}

Both programs print:

Local variable: 20
Global variable: 10

Explanation:

  • In Python, a variable defined outside any function is global, and one defined inside a function is local.
  • C follows the same rules, but a local variable’s lifetime is limited to the block in which it is defined.