C Language – Basics
1. Character Set
A character set is a collection of characters that can be used while writing a C program.
The commonly used characters in C include:
| Category | Characters / Examples |
|---|---|
| Small alphabets | a - z |
| Capital alphabets | A - Z |
| Digits | 0 - 9 |
| Special symbols |
+, -, *,
/, %, #,
=, <, >
|
| Whitespace characters | Space, Tab, New Line |
| Punctuation |
;, ,, ( ),
{ }, [ ]
|
Example
int marks = 90;
This statement contains letters, a number, an assignment operator, and a semicolon.
2. Tokens
A token is the smallest meaningful unit of a C program that is recognized by the compiler.
In simple words, when the compiler reads a C program, it breaks the source code into meaningful units called tokens.
Major Types of Tokens in C
| Token Type | Example |
|---|---|
| Keywords | int, if, while |
| Identifiers | marks, studentName |
| Constants | 10, 25.5, 'A' |
| String literals | "Hello" |
| Operators | +, -, *, = |
| Punctuators | ;, ,, ( ), { } |
Example: Identifying Tokens
int age = 20;
| Token | Type |
|---|---|
int |
Keyword |
age |
Identifier |
= |
Operator |
20 |
Constant |
; |
Punctuator |
3. Keywords
Keywords are reserved words that have a predefined meaning in the C programming language.
Because keywords already have a special meaning to the compiler, they cannot normally be used as names of variables, functions, arrays, etc.
int is a keyword, but INT
is not the C keyword int.
32 Keywords in Traditional C (C90)
Example
int age = 20;
Here, int is a keyword. It tells the compiler that
age is an integer variable.
4. C is Case-Sensitive
C distinguishes between uppercase and lowercase letters. Therefore, the following identifiers are different:
age
Age
AGE
These are treated as three different identifiers by the compiler.
int is a keyword, whereas Int and
INT are different words and are not the keyword
int.
5. Identifier
An identifier is a name given by the programmer to program elements such as variables, functions, arrays, structures, and other user-defined elements.
Rules for Selecting an Identifier
- A keyword cannot be used as an identifier.
-
The first character must be an alphabet
(
A-Zora-z) or an underscore (_). - An identifier cannot begin with a number. Numbers can be used after the first character.
- Spaces are not allowed in identifiers.
-
Special characters and symbols are not allowed, except
the underscore (
_). - Both uppercase and lowercase letters can be used.
- C identifiers are case-sensitive.
Valid and Invalid Identifiers
| Identifier | Valid? | Reason |
|---|---|---|
int |
Invalid | int is a keyword. |
1stNo |
Invalid | An identifier cannot begin with a number. |
first name |
Invalid | Spaces are not allowed. |
CLang@1 |
Invalid | @ is not allowed in an identifier. |
INT |
Valid | It is not the keyword int. |
_1stNo |
Valid | It starts with an underscore. |
No1 |
Valid | The number occurs after the first character. |
first_name |
Valid | Underscore is allowed. |
firstName |
Valid | Uses alphabets only. |
Examples of Valid Identifiers
age
studentName
total_marks
_marks
number1
MAX_VALUE
6. Variable
A variable is a named memory location used to store a value of a particular data type.
The value stored in a variable can change during program execution. This is why it is called a variable.
Example
int age = 20;
In the above statement:
int→ Data typeage→ Variable name / identifier=→ Assignment operator20→ Initial value;→ Statement terminator
Changing the Value of a Variable
int age = 20;
age = 21;
age = 22;
The variable age can hold different values at different
points during program execution.
7. Variable Declaration
Before using a variable, we normally declare it by specifying its data type and name.
int age;
float salary;
char grade;
We can also declare and initialize a variable at the same time.
int age = 20;
float salary = 25000.50;
char grade = 'A';
| Declaration | Meaning |
|---|---|
int age; |
Declares an integer variable named age. |
float salary; |
Declares a floating-point variable named salary. |
char grade; |
Declares a character variable named grade. |
8. Complete Example
#include <stdio.h>
int main()
{
int age = 20;
float marks = 85.5;
char grade = 'A';
printf("Age = %d\n", age);
printf("Marks = %.1f\n", marks);
printf("Grade = %c\n", grade);
return 0;
}
Important Parts
int,float, andcharare keywords.age,marks, andgradeare identifiers.20and85.5are constants.=is an assignment operator.;terminates a statement.
9. Quick Revision
| Concept | Remember |
|---|---|
| Character Set | Characters that can be used to write a C program. |
| Token | Smallest meaningful unit recognized by the compiler. |
| Keyword | Reserved word with a predefined meaning. |
| Identifier | Name given by the programmer to program elements. |
| Variable | Named memory location used to store a value. |
| Case Sensitive | Uppercase and lowercase letters are treated differently. |
10. Practice Questions
A. Fill in the Blanks
B. True or False
int can be used as a variable name.
student_name is a valid identifier.
1student is a valid identifier.
age and Age are treated as different identifiers.
C. Identify Valid and Invalid Identifiers
Classify each of the following as Valid or Invalid:
studentName2markstotal_marksfloatfirst-name_countmarks2student nameMAX_VALUE@number
D. Multiple Choice Questions
2valuefirst namefirst_nameint
numberstudentwhilemarks
5@_#
E. Short Answer Questions
- What is a character set in C?
- What is a token?
- What is a keyword? Give five examples.
- Why can’t keywords normally be used as identifiers?
- What is an identifier?
- Write any five rules for naming identifiers.
- What is a variable?
- Differentiate between a keyword and an identifier.
- Why is C called a case-sensitive language?
- Explain the statement
int age = 20;.
11. Practice Answer Key
1. case
2. keywords
3. identifier
4. memory
5. number
1. False
2. True
3. False
4. True
5. False
1. C —
first_name2. C —
while3. C —
_