C Basic Syntax
\\nSource:
\\n\\nC is a general-purpose programming language widely used in system programming, embedded development, and high-performance computing.
\\nFeaturing efficiency, flexibility, and strong portability, C serves as the foundation for many other programming languages (like C++, Java, and Go) and remains a mainstream choice for operating system, driver, and low-level library development.
\\n\\nTokens
\\nIn C, tokens are the basic building blocks of a program. The compiler performs lexical analysis on the source code to break it down into individual tokens. Tokens are the smallest meaningful units the compiler can recognize; an entire C program is composed of these tokens.
\\nC tokens mainly include the following types:
\\n- \\n
- Keywords: Reserved words with special meaning, such as
int,if,return. \\n - Identifiers: User-defined names for variables, functions, arrays, etc. \\n
- Constants: Fixed values in a program, such as the integer
42or the floating-point number3.14. \\n - String Literals: Sequences of characters enclosed in double quotes, like
"Hello". \\n - Operators: Symbols used to perform operations, such as
+,==,&&. \\n - Separators: Symbols used to separate statements and code blocks, such as
;,{}. \\n
Here is a simple C program that outputs "Hello, World!":
\\nExample
\\n#include <stdio.h>\\n\\nint main(){\\n printf("Hello, World!n");\\n return 0;\\n}\\n\\n The structure of the above code is as follows:
\\n- \\n
- Preprocessor Directives: Such as
#includeand#define, processed by the preprocessor before compilation. \\n - Main Function: Every C program has one and only one
main()function, which is the program's execution entry point. \\n - Variable Declarations: Declare variables used in the program. C89 requires that declarations must be at the beginning of a code block. \\n
- Function Definitions: Define functions used in the program, describing their specific implementation logic. \\n
Explanation of a more complex C program structure (each knowledge point will be expanded upon in later chapters):
\\nExample
\\n#include <stdio.h> // Header file inclusion\\n\\n#define PI 3.14159 // Macro definition\\n\\n// Function declaration\\nint add(int a, int b);\\n\\nint main(){// Main function\\n // Variable declaration\\n int num1, num2, sum;\\n\\n // User input\\n printf("Enter two integers: ");\\n scanf("%d %d", &num1, &num2);\\n\\n // Function Invocation\\n sum = add(num1, num2);\\n\\n // Output result\\n printf("Sum: %dn", sum);\\n return 0;// Return 0 indicates successful program execution\\n}\\n\\n// Function Definition\\nint add(int a, int b){\\n return a + b;\\n}\\n\\n Header File Inclusion
\\n- \\n
- Header files are usually included at the beginning of a program using the
#includedirective. They provide declarations for functions and libraries, such as the standard I/O library<stdio.h>and the standard library<stdlib.h>. They define functions, macros, and constants that allow the program to use predefined library functions. \\n - System header files use angle brackets
<>, while custom header files use double quotes"". Their search paths differ. \\n - Example:
#include <stdio.h>(system header file),#include "myheader.h"(custom header file) \\n
Macro Definitions
\\n- \\n
- Macros are symbolic constants or code snippets defined using the
#definedirective. Macros undergo text replacement by the preprocessor before compilation, do not consume runtime memory, and are often used to define constants or simple inline code logic. \\n - Macros do not have type checking; use them with caution. For constants with type requirements, it is recommended to use
constvariables instead. \\n - Example:
#define PI 3.14159,#define MAX(a, b) ((a) > (b) ? (a) : (b))\\n
Function Declaration
\\n- \\n
- In C, a function declaration (also called a prototype) must appear before the function is defined or called. The declaration provides the function's return type, name, and parameter list, allowing the compiler to perform type checking at the call site. \\n
- Function declarations can be placed in header files to be shared among multiple source files. \\n
- Example:
int add(int a, int b);\\n
Main Function
\\n- \\n
- The
main()function is the entry point of a C program. Every C program must contain amain()function. Execution starts frommain(), and its return value is usually0to indicate successful execution; a non-zero value indicates an error. \\n - The
main()function can also accept command-line arguments:int main(int argc, char *argv[]), whereargcis the argument count andargvis an array of argument strings. \\n - Example:
int main() { ... }\\n
Variable Declarations
\\n- \\n
- In a C program, all variables must be declared with their type before use. Variables can be declared inside the
main()function (local variables) or globally outside functions (global variables). \\n - Initialization can occur at the time of declaration. Uninitialized local variables contain indeterminate (garbage) values, while global and static variables are automatically initialized to
0. \\n - Example:
int count = 0;,float price = 9.99;,char grade = 'A';\\n
Statements and Expressions
\\n- \\n
- A statement is the basic execution unit of a C program, typically a function call, assignment, control statement (like
if,for), or expression. An expression is a code fragment composed of variables, operators, and constants that evaluates to a value. \\n - In C, an assignment is itself an expression with a value (the assigned value), allowing chain assignments like
a = b = c = 0;. \\n - Example:
printf("Enter two integers: ");,sum = add(num1, num2);\\n
Control Flow Statements
\\n- \\n
- Control flow statements manage the order of program execution, including conditional branching (
if/else,switch) and loops (for,while,do-while). There are also jump statements:break,continue,goto, andreturn. \\n - Proper use of control flow statements is key to writing logically clear programs. Avoid overusing
gototo maintain code readability. \\n - Example:
if (num1 > num2) { printf("num1 is greater than num2"); }\\n
Function Definition
\\n- \\n
- A function definition contains the complete function body, describing its specific implementation. A function consists of a return type, name, parameter list, and function body. Good function design follows the "single responsibility principle"βeach function should do one thing. \\n
- C does not support function overloading; each function name must be unique within the same scope. Functions can call themselves recursively. \\n
- Example:
int add(int a, int b) { return a + b; }\\n
Return Statement
\\n- \\n
- The
returnstatement terminates the execution of a function and transfers control, along with a return value, back to the caller. Avoidfunction can usereturn;(without a value) to exit early, or thereturnstatement can be omitted. \\n - The return value of the
main()function is passed to the operating system.0typically indicates success. Standard macrosEXIT_SUCCESSandEXIT_FAILURE(defined in<stdlib.h>) can be used. \\n - Example:
return 0;,return EXIT_SUCCESS;\\n
Separators
\\nSeparators are used to delimit statements, expressions, and code blocks, helping the compiler understand the program's structure. Common separators include:
\\n- \\n
- Comma ,: Used to separate multiple elements in variable declarations, function arguments, or initializer lists. \\n
- Semicolon ;: Used to terminate a statement. It is the most common separator in C. \\n
- Parentheses:\\n
- \\n
- Parentheses
(): Used for function call argument lists, conditions in control flow statements, and to enforce evaluation order in expressions. \\n - Braces
{}: Used to define function bodies, control flow code blocks, and for initialization of compound literals and arrays/structs. \\n - Square Brackets
[]: Used for array declaration and element access (subscript operator). \\n
\\n - Parentheses
In a C program, the semicolon ; is a statement terminator. Every statement must end with a semicolon, indicating the end of a logical entity. A missing semicolon is one of the most common syntax errors for beginners.
For example, these are two separate statements:
\\nprintf("Hello, World! n");return 0;\\n A single semicolon can also act as an empty statement, doing nothing, and is often used in certain loop structures:
\\nwhile (condition); // Empty loop body, waits for condition to change\\n\\n Comments
\\nC has two commenting styles:
\\n// Single-line comment\\n Single-line comments starting with // (introduced in C99). This type of comment runs from // to the end of the line and can occupy a line on its own or be placed at the end of a code line.
/* Single-line comment *//* Multi-line comment */\\n The /* */ format can be single-line or multi-line and is suitable for longer comment scenarios like function descriptions or module headers.
Example
\\n// This is a Single-line comment\\n\\n/*\\n This is a multi-line comment\\n It can span multiple lines\\n */\\n\\nint main(){\\n // Print a message\\n printf("Hello, World!n");\\n return 0;\\n}\\n\\n When using comments, note the following:
\\n- \\n
- Comments cannot be nested (
/* /* */ */is illegal). \\n - Comments cannot appear inside string literals or character constants. \\n
- Comments are for humans; they should be concise and clear, focusing on explaining "why" rather than "what" (the code itself should express "what"). \\n
- Good commenting habits are an important part of professional programming, aiding code maintenance and team collaboration. \\n
Identifiers
\\nIdentifiers are names for variables, functions, arrays, types, etc., defined by the programmer. An identifier consists of letters (upper or lower case), digits, and underscores, but the first character must be a letter or an underscore, not a digit.
\\nAn identifier starts with a letter A-Z or a-z or an underscore _, followed by zero or more letters, underscores, and digits (0-9).
Punctuation characters are not allowed inside C identifiers, such as @, $, and %. C is a case-sensitive programming language. Therefore, Manpower and manpower are two different identifiers. Here are some valid identifiers:
mohd zara abc move_name a_123 myname50 _temp j a23b9 retVal\\n Here are some examples of invalid identifiers and why:
\\n2abc // Error: Starts with a digit\\nmy-name // Error: Contains a hyphen -\\nfloat // Error: Same as a keyword\\nmy name // Error: Contains a space\\n Naming Suggestions: Identifiers should be descriptive and clearly express their purpose. Common naming styles include snake_case (my_variable) and lower camelCase (myVariable). Just be consistent within a project. Identifiers beginning with a double underscore __ are typically reserved for the compiler or standard library and should be avoided in user code.
Constants
\\nConstants are fixed values that do not change during program execution. In C, constants are mainly defined in two ways: using the const keyword or the #define macro.
Constants can be integer constants, floating-point constants, character constants, enumeration constants, etc.:
\\nconst int MAX = 100; // Integer constant\\nconst float PI = 3.14159; // Floating-point constant\\nconst char NEWLINE = 'n'; // Character constant (escape sequence)\\n Common escape sequences in character constants include:
\\n- \\n
'n': Newline \\n 't': Horizontal tab \\n '': Backslash \\n ''': Single quote \\n '': Null character (string terminator) \\n
Constants defined with const have type information, allowing the compiler to perform type checking. They are safer than #define macros and are the recommended approach since C99.
String Literals
\\nString literals are sequences of characters enclosed in double quotes, used to represent text data.
\\nIn memory, string literals are stored as character arrays, with a null character automatically appended at the end to mark the string's termination.
char greeting[] = "Hello, World!"; // Compiler automatically calculates length (including )\\n Here are some important points about string literals:
\\n- \\n
"Hello"actually occupies 6 bytes in memory:'H', 'e', 'l', 'l', 'o', ''. \\n - String literals are stored in a read-only memory area and cannot be modified. If modification is needed, copy them into a character array. \\n
- Two adjacent string literals are automatically concatenated by the compiler:
"Hello, " "World!"is equivalent to"Hello, World!". \\n - The character
'A'(character constant) and the string"A"(string literal) are different; the latter contains two characters:'A'and''. \\n
Operators
\\nOperators are used to perform various operations, such as arithmetic, logic, and comparison. Operators act on operands to form expressions and produce results.
\\nThere are many types of operators in C, commonly including:
\\n- \\n
- Arithmetic Operators:
+,-,*,/,%(where/performs integer division on integers,%gives the remainder) \\n - Relational Operators:
==,!=,>,<,>=,<=(return 1 for true, 0 for false) \\n - Logical Operators:
&&(AND),||(OR),!(NOT), supporting short-circuit evaluation \\n - Bitwise Operators:
&(AND),|(OR),^(XOR),~(NOT),<<(left shift),>>(right shift) \\n - Assignment Operators:
=,+=,-=,*=,/=,%=(compound assignment, e.g.,a += bis equivalent toa = a + b) \\n - Increment/Decrement Operators:
++(increment),--(decrement), in prefix (operate then take value) and suffix (take value then operate) forms \\n - Other Operators:
sizeof(computes size in bytes of a type or variable),?:(ternary operator),&(address-of),*(dereference),->(pointer member access),.(direct member access) \\n
int a = 5, b = 10;\\nint sum = a + b; // Arithmetic operator +, sum = 15\\nint isEqual = (a == b); // Relational operator ==, isEqual = 0 (false)\\nint result = a << 1; // Bitwise left shift, result = 10 (equivalent to a * 2)\\na++; // Postfix increment, a becomes 6\\nint max = (a > b) ? a : b; // Ternary operator, max = b = 10\\n Operators have precedence and associativity: precedence determines the order of evaluation, and associativity determines the direction of evaluation for operators of the same precedence. When unsure about precedence, use parentheses to explicitly group operations and improve code readability.
\\n\\nReserved Keywords
\\nThe following table lists the reserved words in C. These cannot be used as constant names, variable names, or other identifiers, as they have specific syntactic meanings in the C language.
\\n| Keyword | \\nDescription | \\n
|---|---|
| auto | \\nDeclares an automatic variable (default storage class for local variables, rarely used explicitly in modern C) | \\n
| break | \\nExits the current loop or switch statement | \\n
| case | \\nBranch label in a switch statement | \\n
| char | \\nDeclares a character variable or function return type (typically 1 byte) | \\n
| const | \\nDefines a read-only constant; variables modified by const cannot be changed | \\n
| continue | \\nEnds the current loop iteration and immediately starts the next | \\n
| default | \\nDefault branch in a switch statement (executed when no case matches) | \\n
| do | \\nLoop body of a do-while loop, executed at least once | \\n
| double | \\nDeclares a double-precision floating-point variable or function return type (typically 8 bytes) | \\n
| else | \\nNegative branch of a conditional statement (used with if) | \\n
| enum | \\nDeclares an enumeration type for defining a set of named integer constants | \\n
| extern | \\nDeclares a variable or function defined in another file or elsewhere in the same file | \\n
| float | \\nDeclares a single-precision floating-point variable or function return type (typically 4 bytes) | \\n
| for | \\nFor loop statement, suitable for scenarios with a known number of iterations | \\n
| goto | \\nUnconditional jump statement (use cautiously, may reduce code readability) | \\n
| if | \\nConditional judgment statement | \\n
| int | \\nDeclares an integer variable or function return type (typically 4 bytes) | \\n
| long | \\nDeclares a long integer variable or function return type | \\n
| register | \\nSuggests the compiler store the variable in a register for faster access (modern compilers often optimize automatically) | \\n
| return | \\nFunction return statement, can return a value or not (for void functions) | \\n
| short | \\nDeclares a short integer variable or function return type (typically 2 bytes) | \\n
| signed | \\nDeclares a signed type variable (can represent negative numbers; integers are signed by default) | \\n
| sizeof | \\nComputes the size in bytes of a data type or variable (compile-time operator) | \\n
| static | \\nDeclares a static variable (local static variables have a lifetime extending to program end; global static variables are limited to file scope) | \\n
| struct | \\nDeclares a structure type, combining data of different types into a single unit | \\n
| switch | \\nMulti-branch selection statement that jumps to the corresponding case based on an expression's value | \\n
| typedef | \\nCreates a new alias for an existing data type, improving code readability | \\n
| unsigned | \\nDeclares an unsigned type variable (can only represent non-negative numbers, with a larger range) | \\n
| union | \\nDeclares a union type where all members share the same memory space | \\n
| void | \\nDeclares a function with no return value or no parameters; also used for void pointers void* | \\n
| volatile | \\nDeclares a variable that may be changed by external factors (like hardware or interrupts), prohibiting compiler optimization of its reads/writes | \\n
| while | \\nWhile loop statement, suitable for scenarios with an unknown number of iterations but a clear termination condition | \\n
C99 New Keywords
\\n_Bool _Complex _Imaginary inline restrict\\n The new keywords in C99 are mainly used for: boolean type support (_Bool), complex number support (_Complex, _Imaginary), inline function optimization (inline), and pointer aliasing restriction optimization (restrict).
C11 New Keywords
\\n_Alignas _Alignof _Atomic _Generic _Noreturn\\n_Static_assert _Thread_local\\n The new keywords in C11 are mainly used for: memory alignment control (_Alignas, _Alignof), atomic operations and multi-threading support (_Atomic, _Thread_local), generic selection (_Generic), static assertions (_Static_assert), and marking functions with no return (_Noreturn).
Whitespace
\\nLines containing only whitespace, possibly with comments, are called blank lines and are completely ignored by the C compiler.
\\nIn C, whitespace describes blank characters (spacebar), tabs, newlines, and comments. Whitespace separates parts of statements, allowing the compiler to recognize where one element (like int) ends and the next begins. Therefore, in the following statement:
int age;\\n There must be at least one whitespace character (usually a space) between int and age so the compiler can distinguish them. On the other hand, in the following statement:
fruit = apples + oranges; // Get the total number of fruits\\n The whitespace between fruit and =, or between = and apples, is not required, but you can add spaces as needed to enhance readability.
Good whitespace and indentation habits are an important part of high-quality code. It is recommended to follow these conventions:
\\n- \\n
- Use consistent indentation (typically 4 spaces or 1 tab) to represent code block levels. \\n
- Add spaces around operators (e.g.,
a + b) to make expressions more readable. \\n - Add spaces after commas (e.g.,
int a, b, c;) to distinguish multiple elements. \\n - Use blank lines appropriately to separate logically related code sections, improving overall readability. \\n
YouTip