Skip to content
C Language Basics

C Language – Basics

Learning Objective: In this lesson, we will understand the character set, tokens, keywords, identifiers, and variables in the C programming language.

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 ;, ,, ( ), { }, [ ]
Note: Whitespace characters such as spaces, tabs, and new lines are generally used to separate different parts of a C program and improve readability.

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.

Important: C is a case-sensitive language. C keywords are written in lowercase. For example, int is a keyword, but INT is not the C keyword int.

32 Keywords in Traditional C (C90)

auto
break
case
char
const
continue
default
do
double
else
enum
extern
float
for
goto
if
int
long
register
return
short
signed
sizeof
static
struct
switch
typedef
union
unsigned
void
volatile
while

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.

Remember: 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

  1. A keyword cannot be used as an identifier.
  2. The first character must be an alphabet (A-Z or a-z) or an underscore (_).
  3. An identifier cannot begin with a number. Numbers can be used after the first character.
  4. Spaces are not allowed in identifiers.
  5. Special characters and symbols are not allowed, except the underscore (_).
  6. Both uppercase and lowercase letters can be used.
  7. 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 type
  • age → Variable name / identifier
  • = → Assignment operator
  • 20 → 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.

Important: A variable has a name, a data type, and a value stored in memory.

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, and char are keywords.
  • age, marks, and grade are identifiers.
  • 20 and 85.5 are 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

1. C is a __________ sensitive programming language.
2. Words having predefined meanings in C are called __________.
3. A programmer-defined name is called an __________.
4. A variable is a named __________ location used to store data.
5. An identifier cannot begin with a __________.

B. True or False

1. int can be used as a variable name.
2. student_name is a valid identifier.
3. 1student is a valid identifier.
4. age and Age are treated as different identifiers.
5. Spaces are allowed inside identifiers.

C. Identify Valid and Invalid Identifiers

Classify each of the following as Valid or Invalid:

  1. studentName
  2. 2marks
  3. total_marks
  4. float
  5. first-name
  6. _count
  7. marks2
  8. student name
  9. MAX_VALUE
  10. @number

D. Multiple Choice Questions

1. Which of the following is a valid identifier?
  1. 2value
  2. first name
  3. first_name
  4. int
2. Which of the following is a keyword?
  1. number
  2. student
  3. while
  4. marks
3. Which character can be used as the first character of an identifier?
  1. 5
  2. @
  3. _
  4. #

E. Short Answer Questions

  1. What is a character set in C?
  2. What is a token?
  3. What is a keyword? Give five examples.
  4. Why can’t keywords normally be used as identifiers?
  5. What is an identifier?
  6. Write any five rules for naming identifiers.
  7. What is a variable?
  8. Differentiate between a keyword and an identifier.
  9. Why is C called a case-sensitive language?
  10. Explain the statement int age = 20;.

11. Practice Answer Key

Fill in the Blanks:
1. case
2. keywords
3. identifier
4. memory
5. number
True / False:
1. False
2. True
3. False
4. True
5. False
MCQ Answers:
1. C — first_name
2. C — while
3. C — _
C Language Basics — Character Set, Tokens, Keywords, Identifiers and Variables