Every programming language have some basic syntax. Each and every program must follow those syntax. If you have seen the basic structure of c program, it would be easy for you to understand the c language syntax. C language syntax specify rules for sequence of characters to be written in c language.
Tokens in C
According to the syntax rule, it is decided that how the characters will grouped together to form token. In c, Smallest individual unit is called token. C tokens are of six types –
A. Keywords : Examples are int, while, float, char etc.
B. Identifiers : Examples are main, total etc.
C. Constants : Examples are 30, 55, 45 etc.
D. Strings : Examples are “count”, “hi”, “you”, “name” etc.
E. Special Symbols : Examples are (), {} etc.
F. Operators : Examples are +, -, * , / etc.
Semicolons in C
In c language, semicolon represents the end of statement. It acts as statement terminator. You must include semicolon to terminate any c statement. For example,
int i=0; float j=10;
In above example, we have 2 statements. First is int i=0; Second is float j=10; Note that there is semicolon (;) at the end of each statement.
Comments in C
In any programming language, comments are used to provide extra information. You can write single line comment or multi line comment. It also increases readability of the program. Compiler ignore everything written in comments. These are just to provide informations to the reader. You can write single line comment using //. For example
// This is single line comment
You can write multi-line comment using /* and */. /* is treated as start of the multi line comment. */ is treated as end of multiline comment. You can write anything between /* and */. Compiler will treat them as comments. For example,
/* This is multiline comment You can write anything here.... */
Note that multiline comment comment can not be nested i.e. /* This is comment */ is allowed. But, /* This is /*This is inside another comment */ comment */ is not allowed.
Identifiers in C
Identifier is used to identify a variable, function, or any other user-defined term. While creating any identifier you must keep following things in your mind.
A. An identifier can start with letter (A-Z) or (a-z) or underscore(_) followed by zero or more letters, underscores, and digits (0 to 9).
B. You can not use punctuation characters or special symbol like @, #, $, % within identifier.
C. C is case sensitive language. So, Name and name are two different identifiers.
Some valid identifiers are –
name, Name, total, _name, _name1, first_name, name1, _name2 etc.
Keywords in C
Keywords are reserved words that have special meaning. They may not be used as constants or variables or any other identifiers. Some keywords are –
auto double int struct break else switch long case enum register typedef extern char return union const float short unsigned continue for signed void default sizeof goto volatile do if static while
Whitespace in C – very important thing in c language syntax
In C, blanks, tabs, newline characters and comments are termed as whitespace. Whitespace also separates one part of the statement from another. It also enables compiler to distinguish where one element in a statement, such as int, ends and the next element begins. For example, char c;
Whitespace between char and c helps compiler to distinguish between char and c. i.e. Due to whitespace, compiler became able to recognise char as keyword and c as identifier.
However, you do not need whitespace always in c statement. For example,
int c; c = 5 + 4;
In the first statement, you need whitespace between int and c. This will enable compiler to distinguish int as keyword and c as variable name. But, in second statement, you do not need whitespace. You can write the same statement as
c=5+4;
Sometimes, we also include whitespace just to increase readability.
Conclusion
Here, we discussed about c language syntax. Every programming language follows some basic structure and syntax. Every program, written in c, follows that syntax. If you have any questions regarding this topic, please feel free to contact us.