0% found this document useful (0 votes)
40 views

Basics of C Programming

The basics of C programming! To get better, practice writing small programs.

Uploaded by

Poornima Eg
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Basics of C Programming

The basics of C programming! To get better, practice writing small programs.

Uploaded by

Poornima Eg
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

1: Introduction to 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.

2: What You Need to Start


To start programming in C, you'll need:
A computer.
A code editor (where you write your code).
A C compiler (to turn your code into something the computer can understand).

You can download a C compiler like Code::Blocks or use an online compiler.

3: Writing Your First Program


A simple program in C always starts with a main function. Here's how we write a program to say "Hello,
World!":

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.

4: Variables and Data Types


In C, we use variables to store information, like numbers or letters. Each variable has a data type that tells
the computer what kind of information it holds.

`int`: stores whole numbers (like 1, 2, 3).


`float`: stores decimal numbers (like 1.5, 2.75).
`char`: stores a single letter (like 'A', 'B', 'C').

Example:
int age = 10;
float height = 4.5;
char grade = 'A';

5: Doing Math with C


You can do math in C with simple operations:
`+` for addition
`` for subtraction
`` for multiplication
`/` for division

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++;
}

This program prints steps 1 to 5!


8: Functions
Functions are like miniprograms inside your program. You can write a function to perform a task and
then use it whenever you need.

Example:
void sayHello() {
printf("Hello!\n");
}

int main() {
sayHello();
return 0;
}

Here, `sayHello` is a function that prints "Hello!" when called.

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

You might also like