TUTORIAL 4

C Programming Tutorial

Table of Contents

Introduction to C

C is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, while a static type system prevents unintended operations. C has been standardized by the ANSI since 1989 (ANSI C) and by the International Organization for Standardization (ISO).

Back ⇧

Basic Syntax

Basic syntax in C includes statements, expressions, variables, keywords, and comments.

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}
Back ⇧

Variables and Data Types

C supports various data types such as int, float, char, double, etc., and variables are declared using these data types.

Example:

#include <stdio.h>

int main() {
    int num = 10;
    float pi = 3.14;
    char letter = 'A';
    return 0;
}
Back ⇧

Operators

Operators in C are used to perform operations on variables and values. C supports a variety of operators, including arithmetic, relational, logical, bitwise, assignment, and others.

Example:

#include <stdio.h>

int main() {
    int a = 10;
    int b = 20;
    int sum = a + b;
    printf("Sum: %d\n", sum);
    return 0;
}
Back ⇧

Control Structures

Control structures in C allow you to control the flow of the program. Common control structures include if-else statements, loops (for, while, do-while), and switch statements.

Example:

#include <stdio.h>

int main() {
    int num = 10;

    if (num > 0) {
        printf("Positive number\n");
    } else {
        printf("Non-positive number\n");
    }

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

    return 0;
}
Back ⇧

Functions

Functions in C are blocks of code that perform a specific task. Functions help to modularize the code, making it easier to read, maintain, and debug.

Example:

#include <stdio.h>

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

int main() {
    greet();
    return 0;
}
Back ⇧

Arrays

Arrays in C are used to store multiple values of the same type in a single variable. They are useful for handling collections of data.

Example:

#include <stdio.h>

int main() {
    int numbers[5] = {1, 2, 3, 4, 5};

    for (int i = 0; i < 5; i++) {
        printf("Number: %d\n", numbers[i]);
    }

    return 0;
}
Back ⇧

Pointers

Pointers in C are variables that store the memory address of another variable. They are a powerful feature of C that allows for efficient manipulation of arrays and structures.

Example:

#include <stdio.h>

int main() {
    int num = 10;
    int *ptr = #

    printf("Value: %d\n", num);
    printf("Address: %p\n", ptr);
    printf("Value via pointer: %d\n", *ptr);

    return 0;
}
Back ⇧

Strings

Strings in C are arrays of characters ending with a null character '\0'. They are used to store and manipulate text.

Example:

#include <stdio.h>

int main() {
    char greeting[] = "Hello, World!";
    printf("%s\n", greeting);
    return 0;
}
Back ⇧

Structures

Structures in C are used to group different data types into a single unit. They are useful for representing complex data structures.

Example:

#include <stdio.h>
#include <string.h>

struct Person {
    char name[50];
    int age;
};

int main() {
    struct Person person;
    strcpy(person.name, "John");
    person.age = 30;

    printf("Name: %s\n", person.name);
    printf("Age: %d\n", person.age);
    return 0;
}
Back ⇧

File Handling

File handling in C allows you to create, open, read, write, and close files. It provides functions such as fopen, fread, fwrite, and fclose to perform these operations.

Example:

#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "w");
    if (file == NULL) {
        printf("Error opening file!\n");
        return 1;
    }

    fprintf(file, "Hello, World!\n");
    fclose(file);

    return 0;
}
Back ⇧

Preprocessors

Preprocessors in C are directives that are processed before the compilation of the code. Common preprocessor directives include #include, #define, #ifdef, and others.

Example:

#include <stdio.h>
#define PI 3.14

int main() {
    printf("Value of PI: %f\n", PI);
    return 0;
}
Back ⇧