Introduction to C - Basic Syntax and Structure
1. Introduction
The C programming language is a versatile and powerful language that is widely used in various applications, from system programming to game development. In this tutorial, we will cover the basic syntax and structure of a C program, helping you understand how to write and execute simple C programs.
2. Basic Structure of a C Program
Every C program consists of one or more functions, and the most common function is the main() function. The main() function is the entry point of any C program. Here is a simple C program:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Let's break down this program:
- #include <stdio.h> - This is a preprocessor directive that includes the Standard Input Output library, which is necessary for using the printf() function.
- int main() - This is the main function where the program execution begins.
- printf("Hello, World!\n"); - This line prints the text "Hello, World!" to the console.
- return 0; - This line indicates that the program ended successfully.
3. Comments
Comments are used to explain code and are ignored by the compiler. There are two types of comments in C:
- Single-line comments: These start with // and continue to the end of the line.
- Multi-line comments: These start with /* and end with */.
// This is a single-line comment
/*
This is a
multi-line comment
*/
4. Data Types and Variables
In C, variables are used to store data. Each variable must be declared with a specific data type. Common data types include:
- int - Integer type
- float - Floating point type
- char - Character type
Here is an example of declaring and using variables:
#include <stdio.h>
int main() {
int age = 25;
float height = 5.9;
char initial = 'A';
printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Initial: %c\n", initial);
return 0;
}
Age: 25
Height: 5.9
Initial: A
5. Operators
C supports various operators to perform operations on variables and values. Some common operators include:
- Arithmetic Operators: +, -, *, /, %
- Relational Operators: ==, !=, >, <, >=, <=
- Logical Operators: &&, ||, !
- Assignment Operators: =, +=, -=, *=, /=, %=
Here is an example using some arithmetic operators:
#include <stdio.h>
int main() {
int a = 10, b = 20;
int sum = a + b;
int diff = a - b;
int prod = a * b;
int div = b / a;
int mod = b % a;
printf("Sum: %d\n", sum);
printf("Difference: %d\n", diff);
printf("Product: %d\n", prod);
printf("Division: %d\n", div);
printf("Modulus: %d\n", mod);
return 0;
}
Sum: 30
Difference: -10
Product: 200
Division: 2
Modulus: 0
6. Control Structures
Control structures are used to control the flow of a program. Common control structures in C include:
- Conditional Statements: if, if-else, switch
- Loops: for, while, do-while
Here is an example using an if-else statement:
#include <stdio.h>
int main() {
int num = 10;
if (num > 0) {
printf("The number is positive.\n");
} else {
printf("The number is not positive.\n");
}
return 0;
}
The number is positive.
7. Functions
Functions are blocks of code that perform a specific task. They help to make code modular and reusable. Here is an example of a simple function:
#include <stdio.h>
void greet() {
printf("Hello, World!\n");
}
int main() {
greet();
return 0;
}
Hello, World!
8. Conclusion
This tutorial covered the basic syntax and structure of a C program. We discussed the basic structure, comments, data types, variables, operators, control structures, and functions. With this knowledge, you should be able to write and understand simple C programs. Practice writing your own programs to become more proficient in C.
