Introduction to C tokens

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Title

Introduction to C tokens.

Objectives
To understand the concept of tokens in C programming.

To understand what C tokens are and how they are classified.

To identify and categorize different types of tokens in C programs.

To implement examples using tokens to understand their practical use.

Introduction
In the C programming language, tokens are the smallest individual units of a program. They serve

as the building blocks of C code and are essential for understanding how to write, interpret, and

analyze C programs. This lab introduces C tokens, such as keywords, identifiers, constants,

operators, and special symbols. Understanding these tokens is fundamental to developing a solid

foundation in C programming, as they dictate the syntax and structure of the language. This report

details each category of tokens, describes their roles in the C programming language, and

demonstrates practical applications.

In programming languages, tokens are the smallest units of a program. In C, tokens are individual

components that collectively define the syntax and structure of a program. Tokens include

keywords, identifiers, constants, strings, operators, and punctuation symbols. Understanding these

tokens is fundamental for writing C programs, as each token plays a unique role in defining the

logical flow and instructions in a program. By learning about tokens, we gain foundational

knowledge essential for understanding and writing C code.


Theory
In C programming, tokens are the smallest individual units or building blocks of code. They
represent different data elements and are essential in writing and executing code. C tokens include
keywords, identifiers, constants, strings, and operators. This report will focus on operators in C.

1. Arithmetic Operators:
Arithmetic operators are used for performing basic mathematical operations. In C, these include +
(addition), - (subtraction), * (multiplication), / (division), and % (modulus). For instance, using the
operator + in a + b will add two numbers stored in variables a and b.

2. Relational Operators:
Relational operators help in comparing two values and return a boolean result. Common relational
operators in C are == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than
or equal to), and <= (less than or equal to). They are primarily used in conditional statements.

3. Logical Operators:
Logical operators are used to combine multiple conditions in control statements. The main logical
operators in C are && (logical AND), || (logical OR), and ! (logical NOT). They play a crucial
role in evaluating complex conditions.

4. Bitwise Operators:
Bitwise operators perform operations at the bit level. The operators include & (AND), | (OR), ^
(XOR), ~ (NOT), << (left shift), and >> (right shift). They are useful in low-level programming,
such as setting or clearing specific bits.

5. Assignment Operators:
Assignment operators are used to assign values to variables. The simplest assignment operator is
=. Compound assignment operators such as +=, -=, *=, and /= combine arithmetic and assignment
operations, which help simplify code.

6. Increment and Decrement Operators:


These operators (++ and --) are used to increase or decrease the value of a variable by one. They
come in both prefix and postfix forms. For example, i++ increments i after its current value is
used, while ++i increments it before.

7. Conditional (Ternary) Operator:


The conditional operator (?:) is a shorthand for an if-else statement. It is used to evaluate a
condition and select one of two expressions based on the result. The syntax is
condition ? expression1 : expression2.
Implementation:
Area of Rectangle
#include <stdio.h>
int main()
{
int a,b,area;
printf("Enter height and width:\n");
scanf("%d%d",&a,&b);
area=a*b;
printf("Area of rectangle=%d",area);
return 0;
}

Swapping of two numbers


#include <stdio.h>
int main()
{
int a=10,b=20,temp;
printf("Before swap a=10\nb=20\n");
temp=a;
a=b;
b=temp;
printf("after swap a=%d\nb=%d",a,b);
return 0;
}

Determine the largest and smallest number among three numbers


#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter three number:");
scanf("%d%d%d",&a,&b,&c);
if(a>b&&a>c)
{
printf("%d is the largest num\n",a);
}
if(b>a&&b>c)
{
printf("%d is the largest num\n",b);
}
if(c>a&&c>b)
{
printf("%d is the largest num\n",c);
}
if(a<b&&a<c)
{
printf("%d is the smallest num\n",a);
}
if (b<a&&b<c)
{
printf("%d is the smallest num\n",b);
}
if(c<a&&c<b)
{
printf("%d is the smallest num\n",c);
}
return 0;
}
Determine the largest and smallest number among four numbers
#include <stdio.h>
int main()
{
int a,b,c,d;
printf("Enter three number:");
scanf("%d%d%d%d",&a,&b,&c,&d);
if(a>b&&a>c&&a>d)
{
printf("%d is the largest num\n",a);
}
if(b>a&&b>c&&b>d)
{
printf("%d is the largest num\n",b);
}
if(c>a&&c>b&&c>d)
{
printf("%d is the largest num\n",c);
}
if(d>a&&d>b&&d>c)
{
printf("%d is the largest num\n",d);
}
if(a<b&&a<c&&a<d)
{
printf("%d is the smallest num\n",a);
}
if(b<a&&b<c&&b<d)
{
printf("%d is the smallest num\n",b);
}
if(c<a&&c<b&&c<d)
{
printf("%d is the smallest num\n",c);
}
if(d<a&&d<b&&d<c)
{
printf("%d is the smallest num\n",d);
}
return 0;
}

Triangular Series
#include <stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
{
printf("\n");
}
}
return 0;
}
Output:
Area of Rectangle:

Swapping of two numbers:

Triangular Series:
Determine the largest and smallest number among three numbers:
Determine the largest and smallest number among four numbers:
Conclution
Understanding tokens in C is fundamental for programming in C, as every statement relies on

tokens to convey commands to the computer. Each type of token has a unique role, contributing to

the language's syntax and functionality. Practicing with tokens enables programmers to write

syntactically correct and functional code.

This lab provided a solid understanding of the six main types of tokens in C and their roles in

program structure. By working with operators we gained insight into how C programs are

constructed. We understannd the necessity of correctly using each token to avoid syntax errors

during compilation. Mastery of tokens is essential for writing effective and error-free C code,

which lays the foundation for more advanced programming concepts.

You might also like