0% found this document useful (0 votes)
57 views9 pages

TOKENS

A token is the smallest unit used in a C program. Tokens include identifiers, keywords, constants, operators, special characters and strings. A compiler breaks a C program into tokens as the first step of compilation. There are 6 types of tokens - identifiers, keywords, constants, operators, special characters and strings.

Uploaded by

jincy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views9 pages

TOKENS

A token is the smallest unit used in a C program. Tokens include identifiers, keywords, constants, operators, special characters and strings. A compiler breaks a C program into tokens as the first step of compilation. There are 6 types of tokens - identifiers, keywords, constants, operators, special characters and strings.

Uploaded by

jincy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

A token is the smallest unit used in a C program.

Each and every punctuation and word that you come


across in a C program is token.

Just like we can't create a meaningful sentence without using words, we can't imagine a human
body without living cells similarly, we can't develop or construct a C program without using
tokens in C. Tokens in C language are the smallest elements or the building blocks used to
construct a C program.

C Tokens are of 6 types, and they are classified


as: Identifiers, Keywords, Constants, Operators, Special Characters and Strings.

A compiler breaks a C program into tokens and then proceeds ahead to the next stages used in the
compilation process.

In the C language, the following 6 types of tokens are available:

1. Identifiers
2. Keywords
3. Constants
4. Operators
5. Special Characters
6. Strings

Keywords

Keywords in C language are the collection of pre-defined or reserved words. These are case-
sensitive and written in lower cases. Their meaning and functionality are already known to the
compiler.

We can't use these keywords as variable names or function names because by doing so, we are
trying to assign a new meaning to the keyword, which is not allowed in C language. There are a
total of 32 keywords supported by the C language:

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
Identifiers

Identifiers in C are short and informative names that uniquely identify variables or function
names.

These are user-defined words used for naming of functions, variables, structures, unions, arrays
etc. These can be composed of lowercase letters, uppercase letters, underscore or digits, but the
first character should be either an underscore or an alphabet.

There are some defined rules in C language for declaring identifiers:

 Identifiers shouldn't begin with any numerical digit and hence, the first character must be
either an underscore or an alphabet.
 Identifiers are case-sensitive and hence, both lowercase and uppercase letters are distinct.
 The length of identifiers shouldn't be more than 31 characters.
 Commas and blank spaces are not allowed while declaring an identifier.
 Also, the most important rule is that we can't use keywords as identifiers because
keywords in C language are reserved words for special purposes only.

Some Valid Identifiers:

scaler, _scaler, scaler123, scaler_123, count1_, Double

The above examples are following all the essential rules for defining identifiers i.e, they are not
started with a numerical digit, not a keyword and also, there is no blank space or any special
operator.

Some invalid identifiers:

100scaler //started with a numerical digit


_hello,morning //can't use comma operator
int //keyword
float //keyword
Delhi(100) //circular brackets can't be used

The above examples are not following all the essential rules for defining identifiers and hence,
are invalid identifiers.

**

Constants
Constants are the variables whose values are fixed and can not be modified during the execution
of a program once they are defined. They are also known as literals.

The constant variables in C can be initialized only once and their default value is zero. If we try
to re-initialize or re-define any constant variable, then we will get a compilation error.

We can declare constants in C language using:

 const keyword Here, we are using the const keyword to declare a variable and assigning a
value to it that can not be modified later.

const variable_name = value; //We can assign a constant value to


variable_name

 #define pre-processor Here, we are using #define pre-processor and constant ll will be an
alias-name for long keyword.

#define ll long //Here, constant ll will be treated as an alias name for


long

Types of Constants in C Language

Constant Example
Integer constant 10, 20, 30 etc.
Floating-point constant 10.2, 20.5, 30.6 etc.
Octal constant 011, 022, 088 etc.
Hexadecimal constant 0x1a, 0x4b, 0x6b, etc.
Character constant 'x', 'y', 'z' etc.
String constant "Java", "C++", "Python" etc.

Strings in C

A string is a group of characters that should be enclosed in double quotations.

For example: char string[]=“gitam”;

The strings in C always get represented in the form of an array of characters. We have a ‘\0′ null
character at the end of any string- thus, this null character represents the end of that string. In C
language, double quotes enclose the strings, while the characters get enclosed typically within
various single characters.

The number of characters in a string decides the size of that string.

Now, there are different ways in which we can describe a string:

char x[9] = “chocolate’; // Here, the compiler allocates a total of 9 bytes to the ‘x’ array.

char x[] = ‘chocolate’; // Here, the compiler performs allocation of memory during the run time.

char x[9] = {‘c’,’h’,’o’,’c’,’o’,’l’,’a’,’t’,’e’,’\0′}; // Here, we are representing the string in the


form of the individual characters that it has.

Strings in C are represented as an array of characters having null character '\0' at the end. This
null character '\0' denotes the end of the string. Also, these strings are always enclosed within
double quotes. The size of the string is basically the number of characters it contains and also, 1
byte memory space is always reserved for null character value.

Examples of Describing Strings in C

char string[10] = {'s','c','a','l','e','r','\0'};


char string[10] = "scaler";
char string [] = "scaler";

 Here using string[10] denotes that 10 bytes of memory space is allocated for holding the
string value.
 When we declare char as “string []”, memory space will be allocated dynamically as per
the requirements during the execution of the program.

C Token – Special Symbols


The following special symbols are used in C having some special meaning and thus,
cannot be used for some other purpose. Some of these are listed below:
 Brackets[]: Opening and closing brackets are used as array element
references. These indicate single and multidimensional subscripts.
 Parentheses(): These special symbols are used to indicate function calls and
function parameters.
 Braces{}: These opening and ending curly braces mark the start and end of a
block of code containing more than one executable statement.
 Comma (, ): It is used to separate more than one statement like for separating
parameters in function calls.
 Colon(:): It is an operator that essentially invokes something called an
initialization list.
 Semicolon(;): It is known as a statement terminator. It indicates the end of
one logical entity. That’s why each individual statement must be ended with a
semicolon.
 Asterisk (*): It is used to create a pointer variable and for the multiplication
of variables.
 Assignment operator(=): It is used to assign values and for logical operation
validation.
 Pre-processor (#): The preprocessor is a macro processor that is used
automatically by the compiler to transform your program before actual
compilation.
 Period (.): Used to access members of a structure or union.
 Tilde(~): Used as a destructor to free some space from memory.

C Token – Operators
Operators:

Operators are symbols that provide instructions for the performance of various
mathematical and logical operations. Operators are tools that can alter data and
variable values.

Operators are classified into the following eight types:

1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Increment/Decrement Operators
5. Assignment Operator
6. Bitwise Operators
7. Conditional Operators
8. Special Operators
9. Hello World: Write a program that prints "Hello, World!" to the console.

#include <stdio.h>

int main() {
printf("Hello, World!\n");

return 0;
}
10. User Input and Output: Write a program that takes the user's name as input and
prints a greeting using printf.

#include <stdio.h>

int main() {

char name[50]; // Assuming the name won't exceed 50 characters

printf("Enter your name: ");

scanf("%s", name);

printf("Hello, %s!\n", name);

return 0;
}
11. Sum of Two Numbers: Write a program that takes two numbers as input and
prints their sum.

#include <stdio.h>

int main() {

int num1, num2, sum;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);


sum = num1 + num2;

printf("Sum: %d\n", sum);

return 0;
}
12. Area of a Rectangle: Write a program that takes the length and width of a
rectangle as input and calculates its area.
13. #include <stdio.h>

14.

15. int main() {

16. float length, width, area;

17.

18. printf("Enter the length and width of the rectangle: ");

19. scanf("%f %f", &length, &width);

20.

21. area = length * width;

22.

23. printf("Area of the rectangle: %.2f\n", area);

24.

25. return 0;
26. }
27. Celsius to Fahrenheit: Write a program that converts Celsius to Fahrenheit. Take
Celsius as input and print the equivalent Fahrenheit temperature.

#include <stdio.h>
int main() {

float celsius, fahrenheit;

printf("Enter temperature in Celsius: ");

scanf("%f", &celsius);

fahrenheit = (celsius * 9/5) + 32;

printf("Equivalent temperature in Fahrenheit: %.2f\n", fahrenheit);

return 0;
}
28. Swapping Two Numbers: Write a program that swaps the values of two
variables. Take two numbers as input and print their swapped values.

#include <stdio.h>

int main() {

int num1, num2, temp;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);


printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);

// Swapping

temp = num1;

num1 = num2;

num2 = temp;

printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy