Back to Module Topics
1. C Basics
1. Character Set in C
A character set is a set of alphabets, letters and some special characters that are valid in C language.
- Alphabets: Uppercase (
A-Z) and Lowercase (a-z) - Digits:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9 - Special Characters:
~ @ # $ % ^ & * ( ) _ - + = { } [ ] ; : ' " < > , . / ? | \ - White Space Characters: space, newline (
\n), horizontal tab (\t), carriage return (\r), form feed (\f)
2. Tokens
Tokens are the smallest individual units in a C program. When a compiler compiles a program, it breaks the code into tokens.
- Keywords: Reserved words that have special meaning to the compiler (e.g.,
int,if,while,return). C has 32 standard keywords. - Identifiers: User-defined names for variables, functions, arrays, etc.
- Constants: Fixed values that do not change during program execution.
- Strings: Sequence of characters enclosed in double quotes (e.g.,
"Hello"). - Special Symbols: Characters having special meaning (e.g.,
[ ] ( ) { } , ;). - Operators: Symbols that perform operations on variables and values (e.g.,
+,-,*).
3. Variables
A variable is a name given to a memory location to store data. Its value can be changed during execution.
Rules for naming variables (Identifiers):
- Must begin with a letter or an underscore (
_). - Can consist of letters, digits, and underscores.
- Cannot be a reserved keyword.
- Case-sensitive (e.g.,
numandNumare different).
Syntax:
datatype variable_name;
Example: int age = 20;
4. Escape Characters (Escape Sequences)
Escape sequences are used in C to represent characters that cannot be typed directly (like newline) or have special meaning (like quotation marks).
\n: Newline\t: Horizontal Tab\\: Backslash\': Single quote\": Double quote\a: Alert / Bell\b: Backspace\0: Null character (used to terminate strings)
5. Constants
Constants are values that remain fixed during the execution of a program.
- Integer Constants: Whole numbers without a decimal point.
- Base 10 (Decimal): Allowed digits
0-9. E.g.,15, -20, 0 - Base 8 (Octal): Must start with
0. Allowed digits0-7. E.g.,014(equals 12 in decimal) - Base 16 (Hexadecimal): Must start with
0xor0X. Allowed digits0-9, A-F. E.g.,0x1A
- Base 10 (Decimal): Allowed digits
- Floating-point Constants: Numbers with a fractional part or decimal point. E.g.,
3.14,-0.5,2.0e5(exponential notation) - Character Constants: A single character enclosed in single quotes. E.g.,
'A','1','\n' - String Constants: A sequence of characters enclosed in double quotes. E.g.,
"Hello World" - Symbolic Constants: Defined using the
#definepreprocessor directive. They do not occupy memory like variables.#define PI 3.14159 #define MAX 100