Sure, here's a basic overview of C syntax:
1. Comments: Comments in C are preceded by //
for single-line comments and enclosed between /*
and */
for multi-line comments.
2. Data Types: C has several basic data types such as int
, float
, char
, double
, etc.
int num = 10;
float pi = 3.14;
char letter = 'A';
double bigNum = 123456789.123456789;
3. Variables: Variables must be declared before use. They are declared by specifying the data type followed by the variable name.
4. Constants: Constants are declared using the const
keyword
5. Operators: C supports various operators including arithmetic, relational, logical, assignment, etc.
6. Control Structures: C provides control structures like if
, else
, switch
, while
, do-while
, for
, etc.
if (condition) {
// code block
} else if (another_condition) {
// code block
} else {
// code block
}
switch (expression) {
case constant1:
// code block
break;
case constant2:
// code block
break;
default:
// code block
break;
}
while (condition) {
// code block
}
do {
// code block
} while (condition);
for (initialization; condition; update) {
// code block
}
7. Functions: Functions in C are declared with a return type, function name, and parameters.
int add(int a, int b) {
return a + b;
}
8. Arrays: Arrays are collections of similar data items under one name and are indexed from
9. Pointers: Pointers are variables that store the memory address of another variable.
10. Structures and Unions: Structures allow you to group variables of different types under one name.
11. Dynamic Memory Allocation: C provides functions like malloc()
, calloc()
, realloc()
, and free()
for dynamic memory allocation.
These are the basics of C syntax. C is a powerful language with a relatively simple syntax compared to some modern languages, making it suitable for systems programming and other applications where performance and control are critical.