C TOKENS
C TOKENS
C TOKENS
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
1
2
C –TOKENS:
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:
3
char extern near typedef
const float register union
continue for return unsigned
default volatile short void
do goto signed while
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.
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.
6
floating point number.
• Float can hold the real constant accuracy up to 6 digits after the decimal point.
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
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;
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
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"
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):
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
Output:
Welcome to India
Method 2:
Example:
int a;
float b;
scanf(“%d%f”,&a,&b);
16