What is C syntax like? How are identifiers in C? Let's find out with Quantrimang.com!
Tokens in C
The C language includes many different tokens and a token can be a keyword, an identifier, a constant, a string or a character. For example, the C command line below includes the following 5 tokens:
printf("Hello, World! \n");
The individual tokens are as follows:
printf ( "Hello, World! \n" ) ;
Semicolon ; in C
C program, semicolon is part of the end of the command. In fact, every command in C must end with a semicolon. It announces the end of a logical attribute.
For example, below are 2 commands:
printf("Hello, World! \n"); return 0;
Comments in C
Comments are like help in C programs and are ignored by the compiler. It starts with /* and ends with the character */ as shown below:
/* Day la chuong trinh C dau tien */
You cannot have an additional comment section inside this comment section.
Identifier in C
An identifier in C is a name used as a variable, function, and user-defined element. An identifier can begin with the characters A to Z, a to z and an underscore (_) and the numbers 0 to 9.
C does not allow signs such as @, $, and % in identifier names. C is a differentiated language lower case – upper case. Therefore, QTM and qtm are two different identifiers in C. Here are a few valid identifier examples:
nam hoangminh abc ha_noi a_123
sinhvien _hocphi j d23b5 nhanVien
Keywords in C
Below is a list of reserved keywords in the C language. Identifiers, variables, and constants cannot be named the same as the keywords below, otherwise the program will report an error.
auto | else. else | long. long | switch. switch |
break. break | enum | register | typedef |
case. case | extern | return | union. union |
char | float. float | short | unsigned |
const | for | signed | void. void |
continue | goto | sizeof | volatile |
default | if | static. static | while |
due | int | struct | _Packed |
double |
Whitespace in C
A line may contain whitespace, which may be comment lines, known as blank lines as they are ignored by the compiler when compiling.
A space in C can be a blank paragraph, tab, newline (new line) or comment. A space divides one part of a command into multiple parts and helps the compiler distinguish between one element in a command, such as int, the end of one element, and the beginning of the next element as in the following command:
int diemthi;
There must be at least one space character between int and diemthi for the compiler to understand and distinguish between them. Otherwise, see the command below:
luong = luongcoban + phucap; // tinh tong luong
There is no need for a space between luong and the = sign, or between the = sign and luongcoban.
Example of basic C syntax
#include
int main() {
printf("Chào mừng tới Quantrimang.com!");
return 0;
}
Detailed explanation
Line 1: #include
is a header file library, allowing programmers to work with input and output functions, such as printf() (used in line 4). File Header adds functionality to C programs.
Don't worry if you don't understand how #include
work. Just think that it is an ingredient that often appears in the program.
Line 2: A blank line C ignores whitespace. However, for example, use it to make the code easier to read.
Line 3: Another element, always present in C programs is main()
. It is called a function. Any code inside curly braces will be deployed.
Line 4: printf() is a function used to output/print text on the screen. In the above example, “Welcome to Quantrimang.com” will appear or be output.
Note:
- Every command in C ends with a semicolon.
- The body part of C is also rewritten as:
int main(){printf("Chào Quantrimang.com!");return 0;}
- Remember that the compiler ignores whitespace. However, more lines make the code easier to read.
Line 5: return )
end main() function.
Line 6: Don't forget to add a closing curly brace to end the function main
.
Previous lesson: Basic C program structure
Next lesson: Data types in C programming