[go: up one dir, main page]

0% found this document useful (0 votes)
11 views13 pages

ctokens_chapter2

The document provides an overview of C tokens, including character sets, keywords, identifiers, constants, and data types. It explains the classification of tokens, the rules for naming identifiers, and the types of constants, along with examples and their outputs. Additionally, it covers variable declaration, initialization, and the different data types in C, including primary, user-defined, and derived types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views13 pages

ctokens_chapter2

The document provides an overview of C tokens, including character sets, keywords, identifiers, constants, and data types. It explains the classification of tokens, the rules for naming identifiers, and the types of constants, along with examples and their outputs. Additionally, it covers variable declaration, initialization, and the different data types in C, including primary, user-defined, and derived types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Unit-I

Chapter 2

C Tokens: Character set, Identifiers, Keywords, constants, Data types, type


qualifiers, Declaration and Initialization of variables.
C Tokens

Token: The smallest individual units in a program are called tokens.

The ‘C’ tokens are classified as:

1. Character set
2. Keywords
3. Identifiers
4. Constants
5. Operators

Character set:
Character set consists of
i) alphabet from A-Z or a-z
ii) digits from 0-9
iii) Special characters like (,), {,}, [,], <, >, &, $, #, %, ^, !, ?, :, ;, ",', .
iv) White space character: blank space
v) Escape sequences:

\b backspace
\a audible bell
\v vertical tab
\t horizontal tab
\f form feed
\r carriage return
\” double quotes
\’ single quotes
\\ back slash

\r is a carriage return character; it tells your terminal emulator to move the


cursor at the start of the line.
The cursor is the position where the next characters will be rendered.
So, printing a \r allows overriding the current line of the terminal emulator.
Prepared by Aravinda Kasukurthi, CSE, RVRJCCE Page 1
Example Program:

#include<stdio.h>
main()
{
printf("Backspace Character\b");
printf(" Audible Bell\a\n");
printf("Vertical Tab\vDemonstration\n");
printf("Horizental Tab\tDefault 8 spaces\n");
printf("Form feed\fIt is only with Printer\n");
printf("Carriage Return\rmove the cursor at the start of the line\n");
printf("Single Quotes The \"Sun\" rises in the \'east\'\n");
printf("Backslash \\hai\n");
}

Output:

Backspace Characte Audible Bell


Vertical Tab
Demonstration
Horizental Tab Default 8 spaces
Form feed
It is only with Printer
move the cursor at the start of the line
Double quotes and Single Quotes The "Sun" rises in the 'east'
Backslash \hai

Keywords:

Keywords are reserved words. All keywords have fixed meanings. Keywords
serve as basic building blocks for program statements. In ‘C’, there are 32
keywords. All keywords must be written in lower case.

These 32 keywords are classified into 3 types namely

1. Type related Keywords (16)


2. Storage related Keywords (4)
3. Control flow related Keywords (12)

Prepared by Aravinda Kasukurthi, CSE, RVRJCCE Page 2


Type related Keywords:

int short void enum


float long struct const
char signed union volatile
double unsigned typedef sizeof

Storage related keywords:

auto static register extern

Control flow related keywords:

if default goto for


else case continue while
switch break return do

Identifiers: Identifier refers to the names of the variables, functions and arrays.

Rules to name a particular identifier:

1. It must start with either alphabet or underscore.


2. Remaining letters may be alphabet, digit, underscore.
3. Identifier would not allow any special symbol except underscore.
4. An identifier can be of any length while most compilers of ‘C’
language recognize only the first 8 characters.
5. Do not use keywords as an identifier.

Constants:

Constants refer to fixed values that do not change during the execution of a
program.

Constants are 2 types.


1. Numerical constants
2. Non-numerical constants

Numerical Constants are 2 types.


i. Integer constants
ii. Real constants

Prepared by Aravinda Kasukurthi, CSE, RVRJCCE Page 3


Non- Numerical Constants are 2 types.
i. Character constants
ii. String or multiple character constants

Integer constants:

An integer constant refers to a sequence of digits. There are three types


of integers, namely, decimal, octal and hexadecimal.

 Decimal integers consist of a set of digits, 0 through 9, preceded


by an optional – or + sign.
 Valid examples of decimal integer constants are:
321, -789, 0, +77

 An octal integer constant consists of any combination of digits


from the set 0 through 7, with leading 0.
 Some examples of octal integers are
047, 0, 0543, 0655

 Hexadecimal integer constant consists of a sequence of digits


preceded by 0x or 0X.
 They may also include alphabets A through F or a through f.
(0 – 9 and A – F or a – f)
 Examples of hexadecimal integer constants are:
0X49, 0x8D

Real constants:

 Some quantities vary continuously, such as distances, heights,


temperatures, prices and so on.
 These quantities are represented by numbers containing
fractional parts. Such numbers are called real or floating point
constants.
 A real constant is a sequence of digits with a decimal point.
 Examples of real constants are:
0.00876, -0.456, 543.78, +247.0
 A real number may also be expressed in exponential or scientific
notation.
 Example is:
The value 289.45 may be written as 2.8945e2 in exponential
notation.

Prepared by Aravinda Kasukurthi, CSE, RVRJCCE Page 4


General form is:
mantissa e exponent

 The mantissa is either a real number expressed in decimal


notation or an integer.
 The exponent is an integer number with an optional plus or minus
sign.
 The letter e is either in lowercase or uppercase.

Single Character constants (Character constant):

 A single character constant contains a single character (alphabet,


digit, special symbol, white space) enclosed within a pair of single
quote marks.
 Examples are:
'1', 'a', ' ','@'

Note: Character constants have integer values known as ASCII values.


Since each character constant represent an integer value, so character
arithmetic is possible.

Example Program:

#include<stdio.h>
main()
{
char ch='a';
printf("ch=%c\n",ch);
printf("ASCII value of \'%c\'=%d\n",ch,ch);
}

Output:
ch=a
ASCII value of 'a'=97

Prepared by Aravinda Kasukurthi, CSE, RVRJCCE Page 5


//Character Arithmetic
#include<stdio.h>
main()
{
char ch='a';
printf("ch=%c\n",ch);
ch=ch+2;
printf("ASCII value of %c=%d\n",ch,ch);
}

Output:
ch=a
ASCII value of c=99

String constants (Multiple characters):

 String is a sequence of characters enclosed in double quotes.


 The characters may be an alphabet, digit, special symbol and
white space.
 Each and every string ends with null character '\0'.
 Examples are:
"ab", "ab5", "@an", "6*7"

Variable is a data name which can be used to store a data and a variable may
take different values at different times during execution.

Declaration and Initialization of variables:

Declaration of variables:
datatype v1,v2,…,vn

Initialization of variables:
Assign a value to a variable at the time of the variable is declared.

datatype variable_name=constant;
or
Assignment of an initial value to a variable is also called initialization.
variable_name=constant;

The process of giving initial values to variables is called initialization.

Prepared by Aravinda Kasukurthi, CSE, RVRJCCE Page 6


Data types:

Data type specifies which type of data that we are storing in a variable. There
are 3 types of data types.

1. Primary or primitive or fundamental data types


2. User defined data types
3. Derived data types
4. Empty data set

Primary data types:


There four fundamental data types.

i. Integer
ii. Floating point
iii. Character
iv. Double precision floating point

Integer:
'C' provides three different classes of integers.
They are
a) int
b) short int
c) long int
The difference between these 3 integers is the number of bytes to occupy the
range of values.

Type Bytes Bits Range


signed short int or 2 16 -32768 to 32767
short int
signed int or int 2/4 16/32 -32768 to 32767 or -2147483648 to
2147483647
signed long int or 4 32 -2147483648 to 2147483647
long int
unsigned short int 2 16 0 to 65535
unsigned int 2/4 16/32 0 to 65535 or 0 to 4294967295
unsigned long int 4 32 0 to 4294967295
Integers are whole numbers with a range of values supported by a particular
machine.
Integers occupy one word of storage, and since the word sizes of machines
vary (typically 16 or 32 bits).

Prepared by Aravinda Kasukurthi, CSE, RVRJCCE Page 7


The size of an integer that can be stored depends on the computer.
Signed integer uses one bit for sign and 15 bits for the magnitude of the
number.

Formula to find number of bits occupied by data type when number of bytes is
known:
-2n-1 to 2n-1 - 1

Floating point types:

Floating point or real numbers are stored in 32 bits (on all 16 bit and 32 bit
machines) with 6 bits precision.
3 classes of floating point types:
i. float
ii. double
iii. long double

Type Bytes Bits Range Precision


float 4 32 3.4E-38 to 3.4E+38 6 bits
double 8 64 1.7E-308 to 1.7E+308 14 bits
long double 12 96 3.4E-4932 to 1.1E+4932 19 bits

Character types:
A single character can be defined as a character type data.

Type Bytes Bits Range


signed char 1 8 -128 to 127
unsigned char 1 8 0 to 255

//Program to find number of bytes occupied by primary data types:

#include<stdio.h>
main()
{
printf("signed char size=%d\n",sizeof(char));
printf("signed short int size=%d\n",sizeof(short int));
printf("signed int size=%d\n",sizeof(int));
printf("signed long int size=%d\n",sizeof(long int));
printf("unsigned char size=%d\n",sizeof(unsigned char));
printf("unsigned int size=%d\n",sizeof(unsigned int));

Prepared by Aravinda Kasukurthi, CSE, RVRJCCE Page 8


printf("unsigned short int size=%d\n",sizeof(unsigned short int));
printf("unsigned long int size=%d\n",sizeof(unsigned long int));
printf("float size=%d\n",sizeof(float));
printf("double size=%d\n",sizeof(double));
printf("long double size=%d\n",sizeof(long double));
}
Output:

signed char size=1


signed short int size=2
signed int size=4
signed long int size=4
unsigned char size=1
unsigned int size=4
unsigned short int size=2
unsigned long int size=4
float size=4
double size=8
long double size=12

User defined data types:

 'C' supports a feature known as “type definition” that allows users to


define an identifier that would represent an existing data type.
 The user defined data type identifier can later be used to declare
variables.

General form:

typedef type identifier

where type is any existing data type, identifier is a new name given to
the data type.

Examples:
typedef int units;
typedef float marks;
units u1,u2;
marks m1, m2;

Prepared by Aravinda Kasukurthi, CSE, RVRJCCE Page 9


 The main advantage of typedef is that we can create meaningful data
type names for increasing the readability of the program.
 typedef is mainly used in the concepts of structure and union.

Example program:
#include<stdio.h>
int main()
{
typedef int units;
typedef float marks;
units a,b;
marks x,y;
printf("Enter 2 values\n");
scanf("%d%d",&a,&b);
printf("Enter 2 values\n");
scanf("%f%f",&x,&y);
printf("a=%d\tb=%d\n",a,b);
printf("x=%f\ty=%f\n",x,y);
}
Output:
Enter 2 values
5
6
Enter 2 values
1.9
6.7
a=5 b=6
x=1.900000 y=6.700000

Enumerated data type provided by ANSI standard. It is defined as:

enum identifier {value1,value2,…,valuen};

 Where identifier is a user defined enumerated data type, which can be


used to declare variables which can have one of the values enclosed
within the braces (known as enumeration constants).
 After this definition, we can declare variables to be of this 'new' type.

enum identifier v1,v2,…,vn can only have one of the values value1, value2, …,
valuen.

Prepared by Aravinda Kasukurthi, CSE, RVRJCCE Page 10


The assignments are:
v1=value3;
v5=value1;

Example:

enum day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,


Sunday};
enum day weekst, weekend;
weekst=Monday;
weekend=Thursday;

Example program:
#include<stdio.h>
main()
{
enum day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,
Sunday};
enum day weekst,weekend;
weekst=Monday;
weekend=Thursday;
printf("weekst=%d\nweekend=%d\n",weekst,weekend);
}
Output:
weekst=0
weekend=3

 The compiler automatically assigns integer digits from 0 to all


enumerated constants.
0 for value1,
1 for value2,
2 for value3,
3 for value4,
4 for value5,
5 for value6,
6 for value7

 Automatic assignments can be overridden by using the following:

enum day {Monday, Tuesday=5, Wednesday, Thursday, Friday,


Saturday, Sunday};

Prepared by Aravinda Kasukurthi, CSE, RVRJCCE Page 11


Example program:
#include<stdio.h>
main()
{
enum day {Monday, Tuesday=5, Wednesday, Thursday, Friday, Saturday,
Sunday};
enum day weekst,weekend;
weekst=Tuesday;
weekend=Thursday;
printf("weekst=%d\nweekend=%d\n",weekst,weekend);
}
Output:
weekst=5
weekend=7

 The definition and declaration can be combined in one statement as


follows:

enum day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}


weekst, weekend;

#include<stdio.h>
main()
{
enum day { Monday, Tuesday=5, Wednesday,Thursday, Friday, Saturday}
weekst,weekend;
weekst=Tuesday;
weekend=Thursday;
printf("weekst=%d\nweekend=%d\n",weekst,weekend);
}

Type qualifiers:

We can change the properties of primitive or fundamental data type.


There are two type qualifiers:
i. const for constant
ii. volatile

Prepared by Aravinda Kasukurthi, CSE, RVRJCCE Page 12


const:

const is a new data type qualifier defined by ANSI standard.


Example:

const int classsize=60;

This tells the compiler that the value of the int variable classsize must not be
modified by the program.

Example program:
1. #include<stdio.h>
2. main()
3. {
4. const int classsize=60;
5. printf("classsize=%d\n",classsize);
6. classsize=classsize*3;
7. printf("classsize=%d\n",classsize);
8. }
Note: The compiler gives an error at line number 6: assignment of read-only
variable 'a'.

volatile:

ANSI standard defines qualifier volatile that could be used tell explicitly the
compiler that a variable’s value may be changed at any time by some external
sources (from outside the program).
Example:
volatile int a;

Example program:
#include<stdio.h>
main()
{
volatile int a=5;
printf("a=%d\n",a);
a=a*4;
printf("a=%d\n",a);
}
Output:
a=5
a=20
Prepared by Aravinda Kasukurthi, CSE, RVRJCCE Page 13

You might also like