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:
#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)
gcc hello.c -o hello hello.exe
Linux/macOS (Terminal)
gcc hello.c -o hello ./hello
Output:
Hello, World!
2. Basic C Syntax
Variables & Data Types
| Data Type | Example | Description |
|---|---|---|
int | int x = 10; | Integer |
float | float pi = 3.14; | Floating-point number |
char | char letter = 'A'; | Single character |
double | double bigNum = 12345.6789; | High-precision float |
Input & Output
#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
int num = 10; if (num > 0) { printf("Positive\n"); } else if (num < 0) { printf("Negative\n"); } else { printf("Zero\n"); }
Loops
forLoopfor (int i = 0; i < 5; i++) { printf("%d ", i); // Output: 0 1 2 3 4 }
whileLoopint i = 0; while (i < 5) { printf("%d ", i++); // Output: 0 1 2 3 4 }
Switch-Case
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
#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
int numbers[5] = {1, 2, 3, 4, 5}; printf("%d", numbers[0]); // Output: 1
Strings (Array of char)
char name[] = "Alice"; printf("%s", name); // Output: Alice
6. Pointers (Basics)
Pointers store memory addresses.
int x = 10; int *ptr = &x; // Pointer to x printf("%d", *ptr); // Output: 10 (value at address)
7. Structures
Structures group related data.
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
#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