Apply for Zend Framework Certification Training

c



< Compile a C file using its filename How to print in c in different ways >



Tokens in C
In C programming, tokens are the smallest individual elements of a C program that have a meaningful role for the compiler. A C program is made up of different types of tokens such as keywords, identifiers, constants, strings, operators, and special symbols.
For example:
int age = 20;
This statement contains several tokens:
int → Keyword
age → Identifier
= → Operator
20 → Constant
; → Special symbol
Types of Tokens in C
C tokens are mainly divided into 6 categories:
Keywords
Identifiers
Constants
String Literals
Operators
Special Symbols / Punctuators
1. Keywords
Keywords are reserved words that have a predefined meaning in C. They cannot be used as variable, function, or other user-defined names.
Examples
int
float
char
if
else
for
while
return
void
switch
Example:
int marks = 90;
Here, int is a keyword.
Common C Keywords
Keyword                          Purpose
int                                  Integer data type
char                               Character data type
float                               Floating-point data type
double                            Double-precision data type
if                                    Conditional statement
else                                Alternative condition
for                                 Loop
while                              Loop
do                                  Do-while loop
switch                             Multiple-choice selection
case                                Defines a switch case
break                              Terminates a loop or switch
continue                          Skips current iteration
return                             Returns a value from a function
void                                Represents no value/type
2. Identifiers
Identifiers are names given by programmers to program elements such as:
Variables
Functions
Arrays
Structures
Pointers
Example:
int studentAge;
float salary;
Here:
studentAge → Identifier
salary → Identifier
Rules for Identifiers
An identifier can contain letters, digits, and underscore (_).
It must not start with a digit.
Spaces are not allowed.
Keywords cannot be used as identifiers.
C identifiers are case-sensitive.
Valid Identifiers
age
student_name
totalMarks
num1
_count
Invalid Identifiers
1student      // Cannot start with a digit
student name  // Space is not allowed
float         // Keyword
total-marks   // '-' is not allowed
3. Constants
A constant is a fixed value that does not change during the execution of a program.
Example:
int age = 25;
Here, 25 is an integer constant.
Types of Constants
Integer Constants
10
25
-50
1000
Floating-Point Constants
10.5
3.14
-25.75
Character Constants
A character constant is enclosed in single quotes.
'A'
'b'
'5'
'@'
Example:
char grade = 'A';
Escape Character Constants
'\n'
'\t'
'\0'
4. String Literals
A string literal is a sequence of characters enclosed in double quotation marks.
Example:
"Hello"
"Welcome to C"
"Programming"
Example program:
#include <stdio.h>
int main(){
    printf("Hello World");
    return 0;
}
Here:
"Hello World"
is a string literal.
In C, a string is internally stored as a sequence of characters ending with the null character '\0'.
5. Operators
Operators are symbols used to perform operations on values and variables.
Example:
a + b
Here, + is an operator.
Major Types of Operators
Arithmetic Operators
+   -   *   /   %
Example:
sum = a + b;
Relational Operators
==   !=   >   <   >=   <=
Example:
a > b
Logical Operators
&&   ||   !
Example:
if (age >= 18 && age <= 60)
Assignment Operators
=   +=   -=   *=   /=   %=
Example:
x += 5;
Increment and Decrement
++   --
Example:
count++;
Bitwise Operators
&   |   ^   ~   <<   >>
6. Special Symbols / Punctuators
C uses several symbols to define the structure of a program.
Examples:
;   ,   ( )   { }   [ ]   #   *
Semicolon ;
A semicolon generally terminates a statement.
int age = 20;
Comma ,
Used to separate variables or function arguments.
int a, b, c;
Parentheses ( )
Used in functions and expressions.
printf("Hello");
Curly Braces { }
Used to define a block of statements.
if (age >= 18){
    printf("Eligible");
}
Square Brackets [ ]
Used mainly with arrays.
int marks[5];
Hash #
Used with preprocessor directives.
#include <stdio.h>
#define PI 3.14
Example: Identifying Tokens
Consider the following program:
#include <stdio.h>
int main(){
    int a = 10;
    int b = 20;
    int sum = a + b;
    printf("Sum = %d", sum);
    return 0;
}
Tokens in the statement
int sum = a + b;
Token                    Type
int                         Keyword
sum                      Identifier
=                          Assignment operator
a                          Identifier
+                          Arithmetic operator
b                          Identifier
;                           Special symbol
Comments and Tokens
Comments are used to explain the program to programmers.
// This is a single-line comment
/*
   This is a
   multi-line comment
*/
Comments are ignored by the compiler and are not treated as executable elements of the program.
Quick Summary
Token Type                          Examples
Keywords                            int, if, return, while
Identifiers                           age, total, calculate
Constants                           10, 3.14, 'A'
String Literals                     "Hello", "C Programming"
Operators                           +, -, ==, &&
Special Symbols                   ;, ,, {}, (), []
Key Point
Tokens are the basic building blocks of a C program. The compiler analyzes these tokens to understand the structure and meaning of the program.

< Compile a C file using its filename How to print in c in different ways >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top