Wednesday, April 30, 2025

C Programming Tutorial for Beginners

 

C Programming Tutorial for Beginners

Introduction to C

C is a powerful, general-purpose programming language developed by Dennis Ritchie in 1972. It is widely used for system programming, embedded systems, game development, and more.

Why Learn C?

✅ Foundation for other languages (C++, Java, Python)
✅ High performance & efficiency
✅ Low-level memory access
✅ Used in operating systems (Linux, Windows) and embedded systems


1. Setting Up a C Environment

Install a C Compiler

  • Windows: Install MinGW or use Visual Studio with C support.

  • Linux/macOS: Use gcc (usually pre-installed).

Writing Your First C Program

Create a file named hello.c:

c
Copy
Download
#include <stdio.h>  // Standard Input/Output library

int main() {        // Main function (entry point)
    printf("Hello, World!\n");  // Print to console
    return 0;       // Exit program successfully
}

Compiling & Running

  • Windows (Command Prompt)

    sh
    Copy
    Download
    gcc hello.c -o hello
    hello.exe
  • Linux/macOS (Terminal)

    sh
    Copy
    Download
    gcc hello.c -o hello
    ./hello

Output:

Copy
Download
Hello, World!

2. Basic C Syntax

Variables & Data Types

Data TypeExampleDescription
intint x = 10;Integer
floatfloat pi = 3.14;Floating-point number
charchar letter = 'A';Single character
doubledouble bigNum = 12345.6789;High-precision float

Input & Output

c
Copy
Download
#include <stdio.h>

int main() {
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);  // Read input
    printf("You are %d years old.\n", age);
    return 0;
}

Operators

  • Arithmetic+-*/% (modulus)

  • Comparison==!=><>=<=

  • Logical&& (AND), || (OR), ! (NOT)


3. Control Structures

If-Else Statement

c
Copy
Download
int num = 10;
if (num > 0) {
    printf("Positive\n");
} else if (num < 0) {
    printf("Negative\n");
} else {
    printf("Zero\n");
}

Loops

  • for Loop

    c
    Copy
    Download
    for (int i = 0; i < 5; i++) {
        printf("%d ", i);  // Output: 0 1 2 3 4
    }
  • while Loop

    c
    Copy
    Download
    int i = 0;
    while (i < 5) {
        printf("%d ", i++);  // Output: 0 1 2 3 4
    }

Switch-Case

c
Copy
Download
int day = 3;
switch (day) {
    case 1: printf("Monday"); break;
    case 2: printf("Tuesday"); break;
    default: printf("Invalid day");
}

4. Functions

Functions help in code reusability.

Example: Add Two Numbers

c
Copy
Download
#include <stdio.h>

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

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

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

5. Arrays & Strings

Arrays

c
Copy
Download
int numbers[5] = {1, 2, 3, 4, 5};
printf("%d", numbers[0]);  // Output: 1

Strings (Array of char)

c
Copy
Download
char name[] = "Alice";
printf("%s", name);  // Output: Alice

6. Pointers (Basics)

Pointers store memory addresses.

c
Copy
Download
int x = 10;
int *ptr = &x;  // Pointer to x
printf("%d", *ptr);  // Output: 10 (value at address)

7. Structures

Structures group related data.

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

int main() {
    struct Person p1 = {"John", 25};
    printf("Name: %s, Age: %d", p1.name, p1.age);
    return 0;
}

8. File Handling

c
Copy
Download
#include <stdio.h>

int main() {
    FILE *file = fopen("test.txt", "w");  // Open in write mode
    fprintf(file, "Hello, File!");  // Write to file
    fclose(file);  // Close file
    return 0;
}

No comments:

Post a Comment