Basic Syntax and Structure
C programs are structured into functions, with the main
function serving as the entry point. Each C program must have a main
function.
Structure of a C Program
#include <stdio.h>
int main() {
// Your code here
return 0;
}
This simple template includes:
#include <stdio.h>
- This preprocessor directive includes the standard input-output header file, which is necessary for functions likeprintf
.int main()
- The main function where program execution begins.return 0;
- This statement indicates that the program ended successfully.
Variables and Data Types
In C, variables are used to store data. You must declare a variable before using it, specifying its data type.
Common Data Types
int
- For integer values.float
- For floating-point numbers (decimals).double
- For double-precision floating-point numbers.char
- For single characters.
Example of variable declaration:
int age;
float salary;
char grade;
Operators
C provides several operators to perform arithmetic, comparison, and logical operations:
- Arithmetic Operators: +, -, *, /, %
- Comparison Operators: ==, !=, >, <, >=, <=
- Logical Operators: && (logical AND), || (logical OR), ! (logical NOT)
Example of using operators:
int a = 10;
int b = 20;
int sum = a + b; // Arithmetic operator
if (a > b) {
printf("a is greater than b");
} else {
printf("a is not greater than b");
}
Control Flow Statements
C provides several control flow statements to make decisions and repeat tasks:
- if - Executes a block of code if a condition is true.
- else - Executes a block of code if the condition in the
if
statement is false. - for - Loops through a block of code a specified number of times.
- while - Loops through a block of code while a condition is true.
- do-while - Loops through a block of code at least once and then repeats as long as a condition is true.
Example of Control Flow
// if-else statement
int number = 10;
if (number % 2 == 0) {
printf("Number is even");
} else {
printf("Number is odd");
}
// for loop
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
// while loop
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
// do-while loop
int j = 0;
do {
printf("%d\n", j);
j++;
} while (j < 5);
Functions
Functions are blocks of code that perform a specific task. They help in breaking down complex programs into smaller, manageable parts.
Defining a Function
return_type function_name(parameters) {
// body of the function
return value;
}
Example of a function that adds two numbers:
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 3);
printf("The sum is %d", result);
return 0;
}
Input and Output
C uses functions from the stdio.h
library to handle input and output. The printf
function is used for output, while scanf
is used for input.
Example of Input and Output
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("You are %d years old.\n", age);
return 0;
}
Arrays
Arrays are used to store multiple values of the same type in a single variable. They are indexed starting from 0.
Example of an Array
int numbers[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("%d\n", numbers[i]);
}
Conclusion
Understanding the basics of C programming is essential for building a solid foundation in software development. With knowledge of variables, data types, operators, control flow, functions, and arrays, you're well on your way to writing effective C programs.
In the next section, we will cover more advanced topics such as pointers, data structures, and memory management.