C Basic Syntax
C - Basic Syntax
Section titled “C - Basic Syntax”Having seen a basic C program structure, let’s dive into the fundamental building blocks and syntax rules of the C language.
Tokens in C
Section titled “Tokens in C”A C program is composed of basic elements called tokens. The compiler parses the source code into these tokens. The main categories of tokens are:
- Keywords: Reserved words with special meaning (e.g.,
int,if,else,while,return). - Identifiers: Names given to variables, functions, types, etc. (e.g.,
main,age,calculateSum). - Constants: Fixed values (e.g.,
10,3.14,'A',"Hello"). - String Literals: Sequences of characters enclosed in double quotes (e.g.,
"Hello, World!\n"). - Operators: Symbols that perform operations (e.g.,
+,-,*,/,=,==,&,*). - Punctuators (Separators): Symbols like commas, semicolons, parentheses, braces, etc. (e.g.,
,,;,(,),{,}).
Consider this statement:
printf("Value = %d\n", count);The tokens here are:
`printf` (Identifier)`(` (Punctuator)`"Value = %d\n"` (String Literal)`,` (Punctuator)`count` (Identifier)`)` (Punctuator)`;` (Punctuator)Semicolons (;)
Section titled “Semicolons (;)”In C, the semicolon acts as a statement terminator. Each individual statement (a complete instruction) must end with a semicolon. It signals the end of one logical command to the compiler.
Example of two separate statements:
int age = 25;printf("Age is %d\n", age);Comments
Section titled “Comments”Comments are explanatory text within your code that are ignored by the compiler. They are essential for making code understandable to humans.
C supports two styles of comments:
- Multi-line comments: Start with
/*and end with*/. They can span multiple lines. - Single-line comments (C99+): Start with
//and continue to the end of the current line.
/* This is a multi-line comment. It can span several lines. Useful for longer explanations. */
int temperature = 20; // This is a single-line comment.
// Another single-line comment explaining the next line.float price = 99.95; /* You can even put multi-line comments here */Note: You cannot nest multi-line comments (/* ... /* ... */ ... */ is invalid). Comments do not occur inside string or character literals (e.g., "Hello /* not a comment */ world").
Identifiers
Section titled “Identifiers”An identifier is a name you give to entities like variables, functions, structures, enums, typedefs, etc. Rules for identifiers:
- Must begin with a letter (A-Z, a-z) or an underscore (
_). - Subsequent characters can be letters, digits (0-9), or underscores.
- Cannot be a C keyword.
- Are case-sensitive (
myVarandmyvarare different identifiers). - There’s a standard-defined minimum significant length, but modern compilers typically support very long names.
Avoid starting identifiers with an underscore, as such names are often reserved for implementation or standard library use.
Valid identifiers:
countuser_name_temporaryprocessDataitem1MAX_VALUEInvalid identifiers:
1stPlace (starts with a digit)user name (contains a space)my-value (contains a hyphen)int (is a keyword)rate$ (contains invalid character '$')Keywords
Section titled “Keywords”Keywords are words reserved by the C language with predefined meanings. You cannot use them as identifiers.
Standard C Keywords (including C99 and C11):
| auto | break | case | char | const | continue | | default | do | double | else | enum | extern | | float | for | goto | if | inline (C99) | int | | long | register | restrict (C99) | return | short | signed | | sizeof | static | struct | switch | typedef | union | | unsigned | void | volatile | while | _Alignas (C11) | _Alignof (C11) | | _Atomic (C11) | _Bool (C99) | _Complex (C99) | _Generic (C11) | _Imaginary (C99) | _Noreturn (C11) | | _Static_assert (C11) | _Thread_local (C11) | | | | |
Whitespace in C
Section titled “Whitespace in C”Whitespace refers to characters like spaces, tabs, and newlines. The C compiler generally uses whitespace to separate tokens but ignores extra whitespace.
For example, whitespace is required to separate int from age:
int age; // Correct: whitespace separates keyword and identifier// intage; // Incorrect: Compiler sees 'intage' as one unknown tokenHowever, extra whitespace is usually ignored:
int count = 0 ; // Valid, though poorly formatted
x=a+b; // Valid, but less readablex = a + b; // Valid and more readableUse whitespace (spaces, tabs, blank lines) consistently to format your code for readability. Most projects follow specific coding style guidelines.