C TOKENS

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

Character set of C language

 Definition: A symbol that is used while writing a program is called a character.


 A character can be:
• Alphabets/Letters (Lowercase a-z, Uppercase A-Z)
• Digits (0-9)
• Special Symbols (~ ‘ ! @ # % & * () - + / $ = \ { } [ ] : ; “ “ ? etc)
• Whitespaces
• Escape sequences
Escape sequences are special characters denoted by a backslash (\) and a
character after it. It is called escape sequence because it causes an ‘escape’
from the normal way for characters are interpreted. For example if we use ‘\
n’, then the character is not treated as n but it is treated as a new line.

Symbol Name Symbol Name Symbol Name

~ Tilde | Vertical bar [ Left bracket

Left
# hash ( ] Right bracket
parenthesis

Right
$ Dollar sign ) : Colon
parenthesis
Quotation
% Percent sign _ Underscore ”
mark
^ Caret + Plus sign ; Semicolon
& Ampersand { Left brace < Less than
* Asterisk } Right brace > Greater than

‘ Single quote . dot = Assignment

, comma \ backslash / Division

1
2
C –TOKENS:

• Tokens are the basic building blocks in C language.


• Token as the smallest individual unit in a C program.
• This means that a program is constructed using a combination of these tokens.
• There are six main types of tokens in C.

1. Keywords:
 The tokens which have predefined meaning in C language are called
keywords.
 They are reserved for specific purpose in C language they are called

as Reserved Words.
 There are totally 32 keywords supported in C they are:

auto double if static


break else int struct
case enum long switch

3
char extern near typedef
const float register union
continue for return unsigned
default volatile short void
do goto signed while

Rules for keywords


1. Keywords should not be used as variables, function names, array names
etc.
2. All keywords should be written in lowercase letters.

3. Keywords meaning cannot be changed by the users.

2. Identifiers:
Definition:
 Identifiers are the names given to program elements such as variables,
constants ,function names, array names etc
 It consists of one or more letters or digits or underscore.

Rules for identifiers


i) The first character in the identifier should be a letter or underscore and can be
followed by any number of letters or digits or underscores “_“ .
ii) The first character of the identifier there cannot be 2 successive underscores.
iii) No other special symbols are allowed other than letters, digits, and underscore
i.e “_ “ in the identifiers.
iv) The length of an identifier can be any reasonable length. C allows to identifiers
length upto 63 characters, but the first 31 characters are significant i.e compiler
looks at only the first 31 characters.
v) Reserved words or keywords cannot be used as identifiers
vi) Whitespaces are not allowed between the words of identifiers, an underscore
can be used to connect 2 or more words.

Valid identifiers Invalid identifiers


marks 23marks
roll_no __roll_no
MARKS int
_dept_name -marks
Age123 Basic-pay
rollNo Roll no
4
Empname_ #emp_no
Dob12_34 (&address)

3. Data Types:
• The data type defines the type of data stored in a memory location and
determines how much memory should be allocated for variable.
• The data type defines the type of data stored in memory location or the type of
data the variable can hold.
• The basic data types/primitive data types in C are:-
a) char
b) int
c) float
d) double

• C also supports four modifiers—two sign specifiers (signed and unsigned) and
two size specifiers (short and long).
• Table below lists the basic data types, their size, range, and usage for a C
programmer on a 16-bit computer.

5
1) Character data type (char):
• char is a keyword which is used to define single character or a sequence of
character in c language capable of holding one character from the local
character set.
• The size of the character variable is 1 byte.
• The range is from -128 to +127.
• Each character stored in the memory is associated with a unique value termed
as ASCII (American Standard Code for Information Interchange).
• It is used to represent character and strings.
2) Integer data type (int):
• It stores an integer value or integer constant.
• An int is a keyword using which the programmer can inform the compiler
that the data associated with this keyword should be treated as integer.

3) Floating data type (float):


• An float is a keyword using which the programmer can inform the
compiler that the data associated with this keyword should be treated as

6
floating point number.
• Float can hold the real constant accuracy up to 6 digits after the decimal point.

4) Double data type (double):


• An double is a keyword using which the programmer can inform the
compiler that the data associated with this keyword should be treated as
long floating point number.
• Double can hold real constant up to 16 digits after the decimal point.
5) Void Type (void):
• The void type holds no value.
• It is primarily used in three cases:
 To specify the return type of a function (when the function returns no
value).
 To specify the parameters of the function (when the function accepts no
arguments from the caller).
 To create generic pointers. We will read about generic pointers in the
chapter on Pointers.

How are Float and Double Stored?


 In computer memory, float and double values are stored in mantissa and
exponent forms where the exponent represents power of 2 (not 10).
 The number of bytes used to represent a floating point number generally
depends on the precision of the value. While float is used to declare single-
precision values, double is used to represent double-precision values.
 Floating-point numbers use the IEEE (Institute of Electrical and Electronics
Engineers) format to represent Sign bit Exponent Mantissa
 The sign bit denotes the sign of the value. If the value is positive, the sign bit
contains 0 and in case the value is negative it stores 1.

 IEEE format for storing floating point numbers uses a sign bit, mantissa, and the
exponent . The sign bit denotes the sign of the value. If the value is positive, the
sign is zero(0) and if the value is negative then the sign is one(1).
 Generally, exponent is an integer value stored in unsigned binary format after
adding a positive bias. In other words, because exponents are stored in an
unsigned form, the exponent is biased by half its possible value.
 For type float, the bias is 127 and for type double, it is 1023.
7
 The actual exponent value can be computed by subtracting the bias value from
the exponent value.
 Finally, the normalized binary equivalent is stored in such a way that lower byte
is stored at higher memory address. For example ABCD is stored as DCBA.

Note:
 Unsigned int/char keeps the sign bit free and makes the entire word available
for storage of the non-negative or positive numbers.
 Sign bit is the left most bit of a memory word which is used to determine the
sign of the content stored in that word. When it is 0, the value is positive and
when it is 1, the value is negative.

4. Variables:
 A variable is a name given to memory location where data can be stored.
 Using variable name data can be stored in a memory location and can be
accessed or manipulated very easily.
Rules for variables
• The First character should be an alphabet or an underscore _
• Then First character is followed by any number of letters or digits.
• No extra symbols are allowed other than letters ,digits and Underscore
• Keywords cannot be used as an identifier

 C language supports two basic kinds of variables—numeric and character.


I. Numeric variables can be used to store either integer values or floating
point values. C language automatically takes it as a signed variable. To
declare an unsigned variable, the unsigned modifier must be explicitly
8
added during the declaration of the variable.
II. Character variables are just single characters enclosed within single
quotes. These characters could be any character from the ASCII character
set—letters (‘a’, ‘A’), numerals (‘2’), or special characters (‘&’). In C, a
number that is given in single quotes is not the same as a number
without them. This is because 2 is treated as an integer value but ‘2’ is a
considered character not an integer.
 In C variables are declared at three basic places as follows:
• When a variable is declared inside a function it is known as a local variable.
• When a variable is declared in the definition of function parameters it is
known as a formal parameter
• When the variable is declared outside all functions, it is known as a global
variable.
Note: A variable cannot be of type void.

Declaration of variables:
• Giving a name to memory location is called declaring a variable.
• Reserving the required memory space to store the data is called defining a
variable.
• In C, variable declaration always ends with a semicolon.

General Syntax:

datatype variable;
(or)
datatype variable1, variable2,… variablen;

Example: int a; float x, y;


double sum; unsigned short int acc_no;
Variable Initialization
• Variables are not initialized when they are declared and defined, they contain
garbage values(meaningless values)
• The method of giving initial values for variables before they are processed is
called variable initialization.
• General Syntax:
Var_name = expr;
Where,
Var_name is the name of the variable ,
expr is the value of the expression.
• The “expr” on the right hand side is evaluated and stored in the variable name
(Var_name) on left hand side.
• The expression on the right hand side may be a constant, variable or a larger
formula built from simple expressions by arithmetic operators.
9
Examples:
• int a=10;
// assigns the value 10 to the integer variable a
• float x; x=20;
// creates a variable y of float type and assigns value 20 to it.
• int a=10,b,b=a;
// creates two variables a and b. “a” is assigned with value 10, the value of “a” is
assigned to variable “b”. Now the value of b will be 10.
• price = cost*3;
//assigns the product of cost and 3 to price.
• Square = num*num;
// assigns the product of num with num to square.

10
5. Constants:
Constants are the named memory location; it refers to fixed values that do not change
during the execution of a program.
We have different types of constants
1. Integer constant
2. Real constant/Floating Pointing
3. Character constant Ex: ‘a’, ‘9’, ‘\n’
4. String constant Ex: “INDIA”, “8”

1) Integer constant:
• It refers to sequence of digits
• Embedded spaces, commas, characters should not be included.
• Must not contain a decimal point.
• May be signed or unsigned. (default is +)
Three types of integers
❖ Decimal integers: Consist of digits 0 to 9
Ex: 123, -345, 5436, +79
❖ Octal integers: Digits from 0 to 7 but it has to start with 0
Ex: 027, 0657 , 0777645
❖ Hexadecimal integers: Digits from 0 to 9 and characters from a to f, it has to start
with 0X or 0x
Ex: 0X2 0x56 0X5fd 0xbdae

2) Real constants/Floating point:


The numbers that are represented by fractional parts are called real constants.
Two forms
1. Fractional form:
❖ Digits from 0 to 9, sign (+ or -), dot(.)
❖ Ex: 0.0083 215. -71. +0.56 etc..
2. Exponential form:
❖ Syntax: mantissa e exponent
❖ Mantissa is either real number expressed in decimal notation or integer.
❖ Exponent is integer with optional + or –
Ex: 0.65e4, 3.18e-2, 73e+3, 12e+5

3)Character Constant:
• Character Constant Can hold Single character at a time.
• Contains Single Character Closed within a pair of Single Quote Marks
• Single Character is smallest Character Data Type in C.
• Integer Representation : Character Constant have Integer Value known as
‘ASCII’ value
• It is Possible to Perform Arithmetic Operations on Character Constants
Examples: ‘a’ ,‘1’ , ‘#’ , ‘<‘ , ‘X’
11
4) String Constant
• A character string, a string constant consists of a sequence of characters
enclosed in double quotes.
• A string constant may consist of any combination of digits, letters, escaped
sequences and spaces.
• Note that a character constant ‫ۥ‬A’ and the corresponding single character string
constant "A" are not equivalent.
• The string constant "A" consists of character A and \0. However, a single
character string constant does not have an equivalent integer value. It occupies
two bytes, one for the ASCII code of A and another for the NULL character with
a value 0, which is used to terminate all strings.
Valid String Constants: -
"W"
"100"
"24, Kaja Street"

Invalid String Constants: -


"W the closing double quotes missing
Raja" the beginning double quotes missing

Rules for Constructing String constants


 A string constant may consist of any combination of digits, letters, escaped
sequences and spaces enclosed in double quotes.
 Every string constant ends up with a NULL character (\0) which is automatically
assigned (before the closing double quotation mark) by the compiler.

12
Input/Output Statements in C
Streams:
 A stream is the source of data as well as the destination of data.
 Streams are associated with a physical device such as a monitor or a file stored
on the secondary memory.
 C uses two forms of streams—text and binary.

 In a text stream, sequence of characters is divided into lines with each line
being terminated with a newline character (\n).
 A binary stream contains data values using their memory representation.

13
Formatting Input/Output
C language supports two formatting functions printf and scanf.
Assume that the source of data is the keyboard and destination of the data is the
monitor as shown above. printf is used to convert data stored in the program into a
text stream for output to the monitor, and scanf is used to convert the text stream
coming from the keyboard to data values and stores them in program variables.

Output function(printf):

Displaying Output using printf


 printf is an output statement in C used to display the content on the screen.
 print: Print the data stored in the specified memory location or variable.
 Format: The data present in memory location is formatted in to appropriate
data type.

There are various forms of printf statements.

Method 1:
printf(“ format string”);

Format string may be any character. The characters included within the double
quotes will be displayed on the output screen

Example: printf(“Welcome to India”);

Output:
Welcome to India

Method 2:

printf(“ format string”, variable list);

 Format string also called as control string.


 Format string consist of format specifier of particular data type
 Format specifiers starts with % sign followed by conversion code.
14
 variable list are the variable names separated by comma.

Example: int a=10;


float b=20;
printf(“ integer =%d, floating=%f”,a,b);
Output:
integer=10, floating=20.00000

 Number of format specifiers must be equal to number of variables.


 While specifying the variables name make sure that it matches to the format
specifiers with in the double quotes.

Input Function ( scanf) :

Inputting Values Using scanf


 To enter the input through the input devices like keyboard we make use of
scanf statement.
General Syntax:
scanf(“format string”, list of address of variables);
Where:
 Format string consists of the access specifiers/format specifiers.
 Format string also called as control string.
 Format string consist of format specifier of particular data type
 Format specifiers starts with % sign followed by conversion code.
 List of addresses of variables consist of the variable name preceded with
& symbol(address operator).

Example:
int a;
float b;
scanf(“%d%f”,&a,&b);

Rules for scanf():

• No escape sequences or additional blank spaces should be specified in the


format specifiers.
Ex: scanf(“%d %f”,&a,&b); //invalid
scanf(“%d\n%f”,&a,&b); //invalid
• & symbol is must to read the values, if not the entered value will not be stored
in the variable specified.
15
Ex: scanf(“%d%f”,a,b);//invalid.

Format Specifiers:
Format specifiers are the character string with % sign followed with a character. It
specifies the type of data that is being processed. It is also called conversion
specifier. When data is being output or input it must be specified with identifier
(variable) and their format specifier.

Symbols Meaning
%i or %d integer number
%f float point number
%c Character
%o octal number
%x hexadecimal integer(Lower case letter x)
%X hexadecimal integer(Upper case letter X)
%e floating point value with exponent(Lower case letter e)
%E floating point value with exponent (Upper case letter E)
%g floating point value with or without exponent
%ld long integer
%s String
%lf double

16

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