Basics of C Programming
Basics of C Programming
Programming is like giving instructions to a computer to make it do things for us. We write these
instructions using a special language that the computer can understand. One of these languages is called
C.
In C, we write code to create programs that can do cool things, like solve problems or play games.
include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
`include <stdio.h>` tells the computer we want to use some helpful code that is already written.
`int main()` is where the program starts.
`printf` is used to display text on the screen.
`return 0;` means the program ended successfully.
Example:
int age = 10;
float height = 4.5;
char grade = 'A';
Example:
int a = 5;
int b = 10;
int sum = a + b; // This adds 5 and 10
6: If Else Statements
Sometimes, we want our program to make decisions. We use ifelse statements to tell the computer what
to do depending on different conditions.
Example:
int age = 15;
if (age >= 18) {
printf("You are an adult.\n");
} else {
printf("You are not an adult.\n");
}
7: Loops in C
Sometimes, we want the computer to do something many times. That's where loops come in. A while
loop repeats a block of code as long as a condition is true.
Example:
```c
int count = 1;
while (count <= 5) {
printf("This is step %d\n", count);
count++;
}
Example:
void sayHello() {
printf("Hello!\n");
}
int main() {
sayHello();
return 0;
}
9: Arrays
An array is a collection of similar data types. For example, if you want to store 5 numbers, you can use an
array.
Example:
int numbers[5] = {1, 2, 3, 4, 5};
printf("%d", numbers[0]); // Prints the first number in the array