Basic Syntax of C
Comments
In C, you can add comments to your code to make it more readable and to provide explanations. There are two types of comments:
- Single-line comments start with two forward slashes (//) and continue until the end of the line.
- Multi-line comments start with /* and end with */. They can span multiple lines.
// This is a single-line comment
/*
This is a
multi-line comment
*/
Main Function
In C, every program must have a
main()
function. It serves as the entry point of the program.
#include <stdio.h>
int main() {
// Code goes here
return 0;
}
Semicolons
In C, statements must be terminated with a semicolon (;). The semicolon indicates the end of a statement.
#include <stdio.h>
int main() {
printf("Hello, World!"); // Statement ends with a semicolon
return 0;
}
Braces
Curly braces ({}) are used to define blocks of code. They are used to group multiple statements together.
#include <stdio.h>
int main() {
// Code block starts
int x = 5;
if (x > 0) {
printf("Positive");
}
// Code block ends
return 0;
}
Data Types
C supports various data types to represent different kinds of values, such as integers, floating-point numbers, characters, and more.
#include <stdio.h>
int main() {
int num = 10; // Integer
float pi = 3.14; // Floating-point number
char letter = 'A'; // Character
return 0;
}