Welcome to the C Programming Tutorial! This guide will help you learn the basics of C programming and build a strong foundation.
Introduction to C
C is a powerful general-purpose programming language. It is widely used for system programming, developing operating systems, and embedded systems.
Setting Up the Environment
To start programming in C, you need:
- A text editor (e.g., VS Code, Sublime Text)
- A C compiler (e.g., GCC, Clang)
Basic Syntax
Here is a simple "Hello, World!" program in C:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Data Types and Variables
C supports various data types such as int
, float
, char
, and more. Example:
int age = 25;
float pi = 3.14;
char grade = 'A';
Control Structures
Control structures like if
, for
, and while
are used to control the flow of the program. Example:
if (age > 18) {
printf("You are an adult.\n");
}
Functions
Functions allow you to organize code into reusable blocks. Example:
int add(int a, int b) {
return a + b;
}
Pointers
Pointers are variables that store memory addresses. Example:
int x = 10;
int *ptr = &x;
printf("Value of x: %d\n", *ptr);
Memory Management
C provides functions like malloc
and free
for dynamic memory allocation. Example:
int *arr = (int *)malloc(5 * sizeof(int));
free(arr);
File Handling
C allows you to work with files using functions like fopen
, fwrite
, and fread
. Example:
FILE *file = fopen("example.txt", "w");
fprintf(file, "Hello, File!");
fclose(file);
Conclusion
C is a versatile language that provides low-level access to memory and is widely used in various domains. Keep practicing to master it!