C tutorial for Beginners
1. Introduction to C Programming:
C is a versatile programming language known for its efficiency and portability. It's widely used in system programming, embedded systems, and application development.
2. Hello, World! Program:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
This is a basic program that prints "Hello, World!" to the console.
3. Variables and Data Types:
#include <stdio.h>
int main() {
int age = 25;
float height = 5.9;
char grade = 'A';
return 0;
}
Variables store data. Common data types include `int`, `float`, and `char`.
4. Input and Output:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("You entered: %d\n", num);
return 0;
}
`scanf` is used to input values, and `printf` is used for output.
5. Operators:
#include <stdio.h>
int main() {
int a = 10, b = 5;
int sum = a + b;
int product = a * b;
int remainder = a % b;
return 0;
}
Operators perform arithmetic and logical operations.
6. Conditional Statements:
#include <stdio.h>
int main() {
int num = 10;
if (num > 0) {
printf("Positive\n");
} else if (num < 0) {
printf("Negative\n");
} else {
printf("Zero\n");
}
return 0;
}
`if`, `else if`, and `else` control program flow based on conditions.
7. Loops:
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
printf("\n");
int j = 1;
while (j <= 5) {
printf("%d ", j);
j++;
}
return 0;
}
`for` and `while` loops are used for repetitive tasks.
8. Arrays:
#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
printf("Second element: %d\n", numbers[1]);
return 0;
}
Arrays store multiple elements of the same data type.
9. Functions:
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(3, 4);
printf("Sum: %d\n", result);
return 0;
}
Functions are reusable code blocks that perform specific tasks.
10. Pointers:
#include <stdio.h>
int main() {
int num = 5;
int *ptr = #
printf("Value: %d\n", *ptr);
return 0;
}
Pointers store memory addresses, allowing manipulation of data indirectly.
11. Strings:
#include <stdio.h>
#include <string.h>
int main() {
char name[] = "John";
printf("Length: %d\n", strlen(name));
return 0;
}
Strings are arrays of characters, terminated by a null character (`'\0'`).
12. Structures:
#include <stdio.h>
struct Person {
char name[20];
int age;
};
int main() {
struct Person person1;
strcpy(person1.name, "Alice");
person1.age = 30;
printf("Name: %s, Age: %d\n", person1.name, person1.age);
return 0;
}
Structures group related data members.
13. Enumerations:
#include <stdio.h>
enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
int main() {
enum Day today = Wednesday;
printf("Today is day %d\n", today);
return 0;
}
Enumerations define a set of named constants.
14. File Handling:
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w");
if (file != NULL) {
fprintf(file, "Hello, File!");
fclose(file);
}
return 0;
}
File handling is used to read from and write to files.
15. Preprocessor Directives:
#include <stdio.h>
#define PI 3.14159
int main() {
float radius = 5.0;
float area = PI * radius * radius;
printf("Area: %f\n", area);
return 0;
}
Preprocessor directives start with `#` and are processed before compilation.
16. Memory Allocation:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *numPtr = (int *)malloc(sizeof(int));
*numPtr = 10;
printf("Value: %d\n", *numPtr);
free(numPtr);
return 0;
}
Dynamic memory allocation is used to allocate memory at runtime.
17. Function Pointers:
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int (*funcPtr)(int, int) = add;
int result = funcPtr(3, 4);
printf("Sum: %d\n", result);
return 0;
}
Function pointers can point to functions and allow for dynamic function invocation.
18. Recursion:
#include <stdio.h>
int factorial(int n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int num = 5;
printf("Factorial of %d: %d\n", num, factorial(num));
return 0;
}
Recursion is a technique where a function calls itself.
19. Command Line Arguments:
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Number of arguments: %d\n", argc);
for (int i = 0; i < argc; i++) {
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}
Programs can accept command line arguments passed during execution.
20. Typedef:
#include <stdio.h>
typedef unsigned int
uint;
int main() {
uint age = 25;
printf("Age: %d\n", age);
return 0;
}
`typedef` is used to create custom type aliases.
21. Bit Manipulation:
#include <stdio.h>
int main() {
int num = 5;
int result = num << 2; // Left shift by 2 bits
printf("Result: %d\n", result);
return 0;
}
Bit manipulation involves operations at the bit level.
22. Unions:
#include <stdio.h>
union Data {
int num;
float f;
};
int main() {
union Data data;
data.num = 10;
printf("Number: %d\n", data.num);
data.f = 3.14;
printf("Float: %f\n", data.f);
return 0;
}
Unions allow storage of different data types in the same memory location.
23. Pointers and Arrays:
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("First element: %d\n", *ptr);
return 0;
}
Arrays and pointers are closely related in C.
24. Multidimensional Arrays:
#include <stdio.h>
int main() {
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
printf("Element at (1,2): %d\n", matrix[1][2]);
return 0;
}
Arrays can have multiple dimensions, forming matrices.
25. Enumerated Data Types:
#include <stdio.h>
enum Month { January = 1, February, March, April, May, June, July, August, September, October, November, December };
int main() {
enum Month birthMonth = April;
printf("Birth month: %d\n", birthMonth);
return 0;
}
Enums provide a way to create user-defined data types.
26. Type Casting:
#include <stdio.h>
int main() {
float num = 3.14;
int intPart = (int)num;
printf("Integer part: %d\n", intPart);
return 0;
}
Type casting is used to convert one data type to another.
27. Header Files:
// math_operations.h
#ifndef MATH_OPERATIONS_H
#define MATH_OPERATIONS_H
int add(int a, int b);
int subtract(int a, int b);
#endif
// math_operations.c
#include "math_operations.h"
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
// main.c
#include <stdio.h>
#include "math_operations.h"
int main() {
int result = add(5, 3);
printf("Result: %d\n", result);
return 0;
}
Header files are used to declare function prototypes and macros.
28. Constants and Macros:
#include <stdio.h>
#define MAX_NUM 100
int main() {
const int MIN_NUM = 0;
printf("Minimum: %d, Maximum: %d\n", MIN_NUM, MAX_NUM);
return 0;
}
Constants and macros are used to define values that don't change during program execution.
29. Error Handling:
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main() {
FILE *file = fopen("nonexistent.txt", "r");
if (file == NULL) {
printf("Error opening file: %s\n", strerror(errno));
}
return 0;
}
Error handling involves handling situations where unexpected errors occur.
30. Memory Management:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *numArray = (int *)malloc(5 * sizeof(int));
if (numArray != NULL) {
for (int i = 0; i < 5; i++) {
numArray[i] = i * 2;
}
free(numArray);
}
return 0;
}
Memory management is crucial to allocate and release memory efficiently.