C ProgrammingUnit 24 min read
Elements of C: Character Set, Keywords, Identifiers, Data Types, and Constants
Unit 2 of C Programming: This unit provides the foundational building blocks of the C language, covering the essential syntax rules, character sets, data types, and the declaration of constants and variables required to write structured and efficient source code.
Key points
- C programs are constructed using a specific character set including letters, digits, and special symbols.
- Keywords are reserved words with predefined meanings that cannot be used as identifiers.
- Data types determine the amount of memory allocated and the range of values a variable can store.
- Constants are fixed values that do not change during program execution, while variables hold mutable data.
- Proper naming conventions for identifiers improve code readability and maintainability.
1. Character Set
The C character set is the fundamental alphabet of the language. A C compiler recognizes the following categories:
- Letters: Uppercase (A-Z) and lowercase (a-z).
- Digits: 0-9.
- Special Characters: Symbols like
!,@,#,$,%,^,&,*,(,),-,+,=,{,},[,],:,;,",',<,>,,,.,?,/,\,|,~,_. - White Spaces: Blank space, horizontal tab, carriage return, new line, and form feed.
2. Keywords
Keywords are reserved words in C that have a special meaning to the compiler. They cannot be used as variable names or identifiers. C (ANSI C) has 32 keywords:
| auto | double | int | struct |
| break | else | long | switch |
| case | enum | register | typedef |
| char | extern | return | union |
| const | float | short | unsigned |
| continue | for | signed | void |
| default | goto | sizeof | volatile |
| do | if | static | while |
3. Identifiers
Identifiers are names given to various program elements such as variables, functions, and arrays.
- Rules for naming:
- Must begin with a letter or underscore (
_). - Can contain letters, digits, and underscores.
- Cannot be a keyword.
- C is case-sensitive (
Totalandtotalare different). - No special characters or spaces are allowed.
- Must begin with a letter or underscore (
4. Data Types
Data types specify the type of data a variable can hold and the memory allocated for it.
| Type | Size (Bytes) | Format Specifier | Range (Approx) |
|---|---|---|---|
char |
1 | %c |
-128 to 127 |
int |
2 or 4 | %d |
-32,768 to 32,767 |
float |
4 | %f |
3.4e-38 to 3.4e+38 |
double |
8 | %lf |
1.7e-308 to 1.7e+308 |
- Modifiers:
short,long,signed, andunsignedcan be used withintandcharto change their range and size.
5. Constants
Constants are fixed values that do not change during program execution.
- Integer Constants: e.g.,
10,-500,0(decimal, octal, or hexadecimal). - Real (Floating-point) Constants: e.g.,
3.14,-0.005,2.5e3. - Character Constants: Enclosed in single quotes, e.g.,
'A','z','5'. - String Constants: Enclosed in double quotes, e.g.,
"Hello","TU". - Backslash Character Constants (Escape Sequences):
\n(New line)\t(Horizontal tab)\b(Backspace)\\(Backslash)
6. Variables
A variable is a named memory location used to store data.
- Declaration:
int age; - Initialization:
int age = 20;
Worked Example: Variable Declaration and Usage
#include <stdio.h>
int main() {
int age = 21; // Integer variable
float gpa = 3.85; // Floating point variable
char grade = 'A'; // Character variable
printf("Age: %d\n", age);
printf("GPA: %.2f\n", gpa);
printf("Grade: %c\n", grade);
return 0;
}
Trace:
- Memory is allocated for
age(4 bytes),gpa(4 bytes), andgrade(1 byte). - Values are assigned to these memory locations.
printfuses format specifiers to display the values stored in these locations.
7. Comparison: Constants vs. Variables
| Feature | Constant | Variable |
|---|---|---|
| Value | Fixed | Mutable |
| Declaration | const int x = 10; |
int x = 10; |
| Memory | Often stored in read-only memory | Stored in RAM (stack/heap) |
Exam Tip
In TU exams, questions from this unit are usually short-answer questions (2.5 or 5 marks). Focus on:
- Rules for identifiers: Always provide examples of valid vs. invalid identifiers.
- Data types: Be prepared to write the size and range of
intandfloat. - Escape sequences: Memorize at least four common ones (
\n,\t,\a,\\). - Distinction: Be ready to differentiate between keywords and identifiers, or constants and variables.
Based on the TU BSc CSIT syllabus for C Programming (CSC115), unit 2.
Discussion
Loading…