Learning C Programming in Code::Blocks
1. Introduction to C Programming
C is a powerful and widely used programming language for system programming, embedded
systems, and general-purpose applications. Code::Blocks is an Integrated Development
Environment (IDE) that makes writing, compiling, and debugging C programs easy.
---
2. Installing Code::Blocks
1. Go to the official website: www.codeblocks.org
2. Download the version with the MinGW compiler.
3. Install by following the on-screen instructions.
4. Open Code::Blocks and set up a new C project.
---
3. Algorithmic Thinking and Data Abstraction
Simple and Complex Data Types
```c
int age = 20;
float pi = 3.14;
char letter = 'A';
```
- int for whole numbers
- float for decimal numbers
- char for single characters
Reading and Displaying Data
```c
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("You entered: %d", number);
return 0;
}
```
String Data and Functions
```c
#include <stdio.h>
#include <string.h>
int main() {
char name[20];
printf("Enter your name: ");
scanf("%s", name);
printf("Hello, %s!", name);
return 0;
}
```
---
4. Functions in C
```c
#include <stdio.h>
void greet() {
printf("Hello from function!\n");
}
int main() {
greet(); // Function call
return 0;
}
```
---
5. Decisions in C
If-Else Statement
```c
#include <stdio.h>
int main() {
int num = 10;
if (num > 0) {
printf("Positive number");
} else {
printf("Negative number");
}
return 0;
}
```
Switch Case Statement
```c
#include <stdio.h>
int main() {
int choice = 2;
switch (choice) {
case 1:
printf("Choice is 1\n");
break;
case 2:
printf("Choice is 2\n");
break;
default:
printf("Invalid choice\n");
}
return 0;
}
```
---
6. Repetition Constructs
For Loop
```c
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
printf("Iteration: %d\n", i);
}
return 0;
}
```
While Loop
```c
#include <stdio.h>
int main() {
int count = 0;
while (count < 5) {
printf("Count: %d\n", count);
count++;
}
return 0;
}
```
---
7. Pointers in C
Introduction to Pointers
```c
#include <stdio.h>
int main() {
int a = 10;
int *ptr = &a;
printf("Value of a: %d\n", a);
printf("Address of a: %p\n", &a);
printf("Value using pointer: %d\n", *ptr);
return 0;
}
```
---
8. Structures in C
Defining and Using Structures
```c
#include <stdio.h>
struct Student {
char name[50];
int age;
float grade;
};
int main() {
struct Student s1 = {"Alice", 20, 85.5};
printf("Name: %s, Age: %d, Grade: %.2f\n", s1.name, s1.age, s1.grade);
return 0;
}
```
---
9. Arrays in C
One-Dimensional Arrays
```c
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
```
Multi-Dimensional Arrays
```c
#include <stdio.h>
int main() {
int matrix[2][2] = {{1, 2}, {3, 4}};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
```
---
10. Recursion in C
Factorial Calculation Using Recursion
```c
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
```
---
11. Debugging in Code::Blocks
- Set breakpoints by clicking on the left margin of the code editor.
- Press F8 to step through the code.
- Use Watches to monitor variable values.
- Check the Build Log for errors and warnings.
---
12. Memory Management in C
Dynamic Memory Allocation
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int*) malloc(5 * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
for (int i = 0; i < 5; i++) {
ptr[i] = i * 10;
}
for (int i = 0; i < 5; i++) {
printf("%d ", ptr[i]);
}
free(ptr);
return 0;
}
```
---
13. Study Tips for C Programming
1. Practice daily by writing simple programs.
2. Use debugging tools in Code::Blocks.
3. Solve problems on websites like LeetCode and CodeChef.
4. Read documentation and C programming books.
5. Work on small projects like a calculator or file reader.
---
14. Conclusion
C programming is a fundamental skill for any aspiring programmer. With consistent practice and
using Code::Blocks effectively, you can master C programming efficiently. Happy coding!
---