Main Function Variants
1. void main()
Some compilers support this non-standard syntax, but it is not recognized by any official C language standard. Most compilers will reject this form.
2. Implicit int main() (unnamed main declaration)
This form was partial permitted in the C90 standard, where the return type of main was implicitly assumed to be int. However, C99 and later C standards explicitly disallow this syntax. Even if your compiler allows it, avoid using this form.
3. int main()
This is the most widely used standard-compliant form, fully supported by all modern C compilers. It indicates that the main function returns an integer value to the operating system upon exit.
4. int main(void)
This is another standard-compliant variant, explicitly stating that the main function accepts no input arguments. It is commonly used for programs that do not take command-line parameters.
Comments
C supports two types of comments: single-line and multi-line.
- Single-line comments: Start with
//and extend to the end of the current line. Example:
// This is a single-line comment explaining a variable declaration
int user_score = 95;
- Multi-line comments: Enclosed between
/*and*/, can span multiple lines or be used inline. Examples:
/*
This is a multi-line comment
covering multiple lines of text
*/
/* This inline multi-line comment works for a single line too */
Comments are ignored by the compiler, and are used to document code logic, explain intent, and improve readability and maintainability for other developers.
Identifiers
Identifiers are names used to label variables, functions, arrays, structures, and other user-defined entities in C. The following rules apply to valid C identifiers:
- May only contain uppercase leters, lowercase letters, Arabic numerals, and underscores (
_). - Must start with either an uppercase/lowercase letter or an underscore.
- Are case-sensitive:
TotalSumandtotalsumare two distinct identifiers. - While there is no official maximum length, only the first 63 characters are recognized by most compilers.
- Cannot be reserved C keywords (such as
int,return,if).
Valid identifier examples include:
- Variables:
total_score,user_age,item_count - Functions:
calculate_average,display_user_data - Arrays:
temperature_readings,matrix_2d - Structures:
EmployeeRecord,BookMetadata
Using descriptive, meaningful identifiers greatly improves code readability, so avoid vague names like x or tmp unless used for temporary, obvious variables.
ASCII Character Encoding
ASCII is a character encoding standard used to represent text characters as integer values in C. Every printable and non-printable character maps to a unique integer between 0 and 127.
Characters with ASCII codes 0-31 are non-printable control characters, which can only be represented using escape sequences like \ddd or \xdd.
Common ASCII code mappings:
- Uppercase letters A-Z: 65 to 90
- Lowercase letters a-z: 97 to 122
- Numeric digits 0-9: 48 to 57
- Special characters:
- Space: 32
- Newline: 10
- Carriage return: 13
- Period: 46
- Comma: 44
- Colon: 58
- Semicolon: 59
You can use the char type to store a single ASCII character, and the int type to store its corresponding ASCII code. Below is a sample program to print a character and its ASCII value:
#include <stdio.h>
int main(void) {
char test_char = 'B';
printf("Character: %c\n", test_char);
printf("ASCII Value: %d\n", test_char);
return 0;
}
The output will be:
Character: B
ASCII Value: 66
Escape Sequences
Escape sequences are special character sequences starting with a backslash (\) that represent non-printable control characters or characters with special syntax meaning in C. Below are common escape sequences:
\': Single quotation mark\": Double quotation mark\\: Backslash character\n: Newline\t: Horizontal tab\r: Carriage return\b: Backspace\f: Form feed\a: Alert (beep)\0: Null character
Basic Escape Sequences (\n, \", \\, \a, \')
This sample program demonstrates usage of these escape sequences:
#include <stdio.h>
int main(void) {
printf("Greeting\tfrom C program!\n");
printf("This string has a \"quoted\" word.\n");
printf("A single backslash: \\\n");
printf("Second line starts here.\n");
printf("This line will trigger a system beep: \a\n");
return 0;
}
Expected output:
Greeting from C program!
This string has a "quoted" word.
A single backslash: \
Second line starts here.
This line will trigger a system beep:
Backspace Escape (\b)
The \b escape sequence moves the cursor back one position in the output stream. Example:
#include <stdio.h>
int main(void) {
printf("abc\bde");
return 0;
}
The output will be abde, as the backspace removes the 'c' before printing 'de'.
Carriage Return Escape (\r)
The \r escape sequence moves the cursor to the start of the current line, allowing in-line updates of output. A common use case is dynamic progress indicators:
#include <stdio.h>
int main(void) {
printf("Initializing... 0%%");
fflush(stdout);
for (int progress_step = 1; progress_step <= 10; progress_step++) {
printf("\rInitializing... %d0%%", progress_step);
fflush(stdout);
// Simulate a short delay for demonstration
// sleep(1); // Uncomment to add a 1-second delay between updates
}
printf("\nSetup complete!\n");
return 0;
}
When run, this will overwrite the same line to show increasing progress percentage. Note that fflush(stdout) is required to force the output buffer to flush immediately, otherwise updates may not appear until the program exits.
Horizontal Tab Escape (\t)
The \t escape sequence inserts a horizontal tab, moving the cursor to the next tab stop (typically every 8 columns by default). Example:
#include <stdio.h>
int main(void) {
printf("Product ID\tProduct Name\tPrice\n");
printf("P1001\t\tWireless Mouse\t$29.99\n");
printf("P1002\t\tMechanical Keyboard\t$89.99\n");
return 0;
}
Sample output:
Product ID Product Name Price
P1001 Wireless Mouse $29.99
P1002 Mechanical Keyboard $89.99
Note that tab width may vary across different terminals or text editors, so alignment may not be consistent across all environments.
Octal Escape Sequences (\ddd)
Octal escape sequences use a 3-digit octal number (000 to 377, equivalent to 0 to 255 in decimal) to represent an ASCII character. For example, octal 141 maps to the lowercase letter 'a':
#include <stdio.h>
int main(void) {
printf("%c\n", '\141');
return 0;
}
This will print a to the console. Octal escapes are often used to represent non-printable control characters like newline (\012) or carriage return (\015).
Hexadecimal Escape Sequences (\xdd)
Hexadecimal escape sequences use a 2-digit hexadecimal number (00 to FF, equivalent to 0 to 255 in decimal) to represent an ASCII character. For example, hex 61 maps to the lowercase letter 'a':
#include <stdio.h>
int main(void) {
printf("%c\n", '\x61');
return 0;
}
This will print a to the console. Hex escapes are also commonly used for non-printable characters, such as space (\x20) or tab (\x09).