C Programing Tutorial

Enhance your skills with real-world coding challenges and comprehensive word problem solutions for effective learning.

From Basics to Advanced

1. Introduction to C

What is C Programming?

  • History of C.

  • Importance and applications (e.g., operating systems, embedded systems).

Key Features of C:

  • Low-level access to memory.

  • Simple and efficient.

  • Platform-independent (through compilers).

2. Setting Up the Environment

Installing C Compiler:

  • GCC (GNU Compiler Collection) setup.

  • IDE options: Code::Blocks, Dev-C++, or VS Code.

Writing Your First Program:

#include <stdio.h>

int main() {

printf("Hello, World!");

return 0;

}

3. Basics of C Programming

  • Structure of a C Program

    • Preprocessor directives, main() function, and code blocks.

  • Data Types and Variables

    • Basic types: int, float, char, double.

    • Declaration and initialization.

    • Example:

int age = 25;

float height = 5.9;

char grade = 'A';

  • Input and Output

    • Using scanf() and printf().

    • Example:

int num;

printf("Enter a number: ");

scanf("%d", &num);

printf("You entered: %d\n", num);

4. Operators and Expressions

  • Arithmetic, relational, logical, bitwise, assignment, and miscellaneous operators.

  • Example:

int a = 5, b = 3;

printf("Sum: %d\n", a + b);

printf("Is a greater? %d\n", a > b);

5. Control Statements

  • Conditional Statements:

    • if, if-else, and switch-case.

    • Example:

int num = 10;

if (num > 0)

printf("Positive number\n");

else

printf("Negative number\n");

  • Loops:

    • for, while, do-while.

    • Example:

for (int i = 1; i <= 5; i++) {

printf("Count: %d\n", i);

}

6. Arrays and Strings

  • Arrays:

    • Declaration, initialization, and accessing elements.

    • Example:

int arr[5] = {1, 2, 3, 4, 5};

for (int i = 0; i < 5; i++) {

printf("%d ", arr[i]);

}

  • Strings:

    • Introduction to char arrays and string functions (strlen, strcpy).

    • Example:

char name[20];

printf("Enter your name: ");

scanf("%s", name);

printf("Hello, %s\n", name);

7. Functions

  • Defining and Calling Functions

    • Example:

int add(int a, int b) {

return a + b;

}

int main() {

int sum = add(5, 3);

printf("Sum: %d\n", sum);

return 0;

}

  • Pass by Value and Pass by Reference.

  • Recursion.

8. Pointers

  • Basics of Pointers:

    • Definition, syntax, and usage.

    • Example:

int a = 10;

int *p = &a;

printf("Value: %d, Address: %p\n", *p, p);

  • Pointer Arithmetic and Applications.

9. Structures and Unions

  • Defining and Using Structures:

struct Student {

int id;

char name[50];

float marks;

};

  • Unions and Differences from Structures.

10. Dynamic Memory Allocation

  • Using malloc, calloc, realloc, and free.

  • Example:

int *ptr;

ptr = (int*)malloc(5 * sizeof(int));

if (ptr != NULL) {

for (int i = 0; i < 5; i++)

ptr[i] = i * 10;

free(ptr);

}

11. File Handling

  • Reading and writing files using fopen, fclose, fprintf, fscanf.

  • Example:

FILE *fptr;

fptr = fopen("data.txt", "w");

fprintf(fptr, "Hello, File!");

fclose(fptr);