Unit 2: Elements of C: Ramesh Rimal
Unit 2: Elements of C: Ramesh Rimal
Prepared By
Ramesh Rimal
6/21/20 11:52:26 AM 1
CHARACTER SET
A character denotes any alphabet, digit,
white space or special symbol used to
represent information.
The character set in C language can be
grouped into the following categories.
Letters: uppercase A to Z
lowercase a to z
Digits: 0 to 9
White spaces: Blank space, Horizontal
tab, Carriage return, New
line & Form feed
Special characters: listed in the table
6/21/20 11:52:26 AM 2
Symbol Name of Symbol Symbol Name of Symbol
. Period ^ Caret
; Semicolon * Asterisk
6/21/20 11:52:26 AM 3
COMMENTS/REMARKS
Comments are non executable program codes
that may appear any where within a program.
Comments are used to clarify purpose of the
entire program or the purpose of some
statements in the program.
Comments cannot be nested.
Comments can be written in two forms:
//Single Line Comments
/*Multi-Line .....................................
.....................................................
.........................Comments*/
6/21/20 11:52:26 AM 4
2.1 C TOKENS
The most basic element of a C program recognized by
the compiler is a single character or group of
characters called C token. C programs are written
using these tokens and syntax of language.
A token is an atomic unit of source program, i.e., the
compiler will not break down the token any further.
There are six types of C tokens which are given below.
1.Keywords
2.Operators
3.Identifiers
4.Constants
5.Strings
6.Special symbols
6/21/20 11:52:26 AM 5
KEYWORDS
Keywords are reserved words that have standard,
predefined meanings in C.
Keywords are all lowercase, since uppercase and
lowercase characters are not equivalent.
Keywords can’t be used as identifiers but It's
possible to utilize an uppercase keyword as an
identifier but it's not a good programming practice.
There are 32 keywords used in ANSI C.
Some C compilers may recognize other keywords as
well.
A compiler named C99 has added 5 more keywords:
_Bool, _Complex, _Imaginary, inline and restrict
6/21/20 11:52:26 AM 6
32 KEYWORDS USED IN ANSI C
6/21/20 11:52:26 AM 8
Escape Sequence Meaning ASCII Value
\b Backspace 008
\0 Null 000
6/21/20 11:52:26 AM 9
ESCAPE SEQUENCE CONTD.
Escape sequence can also be expressed in terms
of one, two or three octal digits which
represent single character bit patterns.
The general form of such an escape sequence is
\ooo, where each o represents an octal digit (0
to 7).
Some versions of C also allow an escape
sequence to be expressed in terms of one or
more hexadecimal digits, preceded by the
letter x.
The general form of a hexadecimal escape
sequence is \xhh, where each h represents a
hexadecimal digit (0 to 9 and a to f).
6/21/20 11:52:26 AM 10
IDENTIFIERS
Identifiers are the names given to various
program elements such as variable, structure,
function, array etc.
Identifiers are not defined in programming
language. They are user defined names and
consist of a sequence of letters, digits and
underscore.
Underscore is usually used as a link between
two words in long identifiers.
An identifier can be arbitrarily long. Some C
compilers recognize only first 8 characters but
most C compilers recognize more
(typically31characters).
6/21/20 11:52:26 AM 11
SOME RULES TO DEFINE IDENTIFIERS
It must consist of only letters, digits and underscore.
First character must be an alphabet or underscore.
Any standard C language keyword cannot be used as
identifier.
It should not contain a space.
It allows both upper case and lower case characters but
common usage favours the use of lowercase letters for
most types of identifiers.
Valid Identifiers
A1 B34 FIRST _NAME x_1
Invalid Identifiers
1A 34AB int void FIRST-NAMEX.1
6/21/20 11:52:26 AM 12
VARIABLE
A variable is an identifier that is used to
represent a single data item i. e. a numerical
quantity or a character constant with in a
designated portion of the program.
In other words, a variable is an entity which
may vary during program execution.
In computer programming, a variable is used
to reserve a location in the computer
memory.
Variable names are names given to locations
in the computer memory where different
types of can be stored.
6/21/20 11:52:26 AM 13
VARIABLE CONTD.
The data item must be assigned to the variable at
some point in the program that can then be accessed
later in the program simply by referring to the
variable name.
In any programming language, the types of variables
that it can support depends on the types of constants
that it can handle.
Declaration of variables:
The declaration of variables must be done before
they are used in the program. It does two things:
1.It tells the compiler what the variable name is.
2.It specifies what type of data the variable will hold.
Syntax of variable declaration:
data_type v1,v2,v3,……..,vn;
6/21/20 11:52:26 AM 14
DATA TYPES
Data types refer to formats of data what we use in
programs.
Data types are used to define a variable before they are
used in a program.
Data types determine the following:
Range of the data
Type of the data stored
Number of bytes it occupies in memory
C supports variety of data types, each of which may be
represented differently within the computer’s memory.
ANSI C supports three classes of data types:
1. Primary(or fundamental)Data Type
2. Secondary(or derived)Data Type
3. User-defined Data Type
6/21/20 11:52:26 AM 15
PRIMARY DATA TYPES
6/21/20 11:52:26 AM 17
INTEGER CONTINUED
16-bit 2 2 4
(Turbo C/ C++)
32-bit 2 4 4
(Visual Studio, GCC)
6/21/20 11:52:26 AM 18
FLOATING POINT VALUE
Floating point value represents a real
number with 6 digits precision and is stored
in 32 bits. (on all 16 bit and 32 bit machines)
and also known as single precision number.
It is declared in C by using keyword ‘float’.
It’s format specifier is %f and it occupies 4
byte memory.
It can range from -3.4×1038 to 3.4×1038
6/21/20 11:52:26 AM 19
DOUBLE
Another data type called Double can be used if
the accuracy provided by a ‘float’ number is
not sufficient.
It is declared in C by using ‘double’ keyword
and it’s format specifier is %lf.
It occupies 8 bytes in memory giving a precision
of 14 digits. So, it is also known as double
precision number.
It has a range from -1.7×10308 to 1.7×10308 .
To extend the precision further, we may use
‘long double’ which occupies 10 bytes in
memory and it’s format specifier is %Lf and
range is from -1.7×104932 to 1.7×104932 .
6/21/20 11:52:26 AM 20
CHARACTER
A character refers to any alphabet or digits or
symbol enclosed within single quotes.
It’s format specifier is %c and keyword is ‘char’
It’s range is from -128 to 127.
It occupies only one byte.
The qualifier signed or unsigned can be
explicitly applied to ‘char’.
A ‘signed char’ is same as an ordinary ‘char’
and has a range from -128 to 127; whereas, an
‘unsigned char’ has a range from 0 to 255
6/21/20 11:52:26 AM 21
CHARACTER CONTD..
/* A program that illustrates the range of signed
and unsigned character.*/
#include<stdio.h> What output do you
#include<conio.h> expect from this
program??
void main()
{ Possibly, 291 and
the character
char ch=291; corresponding to
clrscr(); that it.
}
6/21/20 11:52:26 AM 22
CHARACTER CONTD..
The reason is that ‘ch’ has been declared as
a ‘char’, and a ‘char’ cannot take a value
bigger than +127.
When value of ‘ch’ exceeds +127, an
appropriate value from the other side of the
range is picked up and stored in ‘ch’.
This value in our case happens to be 35,
hence 35 and its corresponding character #,
gets printed out.
6/21/20 11:52:26 AM 23
VOID
The void type specifies that no value is available.
It is used in three kinds of situations:
1. Function returns as void
There are various functions in C which do not return value or
you can say they return void. A function with no return value
has the return type as void. For eg: void sum(int, int);
2.Function arguments as void
There are various functions in C which do not accept any
parameter. A function with no parameter can accept as a void.
For eg: int rand(void);
3. Pointers to void
A pointer of type void * represents the address of an object, but
not its type. For example a memory allocation function void
*malloc( size_t size ); returns a pointer to void which can be casted
to any data type.
6/21/20 11:52:26 AM 24
DATA TYPE QUALIFIERS
6/21/20 11:52:26 AM 25
SIZE QUILIFIER
It alters the size of the basic data type.
There are two size qualifiers that can be
applied to integer: ‘short’ and ‘long’
In any ANSI C compiler, the size of ‘short int’,
‘int’ and ‘long int’ follow the following
restrictions
The minimum size of a ‘short int’ 2
bytes
The size of an ‘int’ must be greater than
or equal to that of
‘short int’
The size of a ‘long int’ must be greater
than or equal to that
6/21/20 11:52:26 AM 26
SIGN QUALIFIER
It specifies whether a variable can hold both
positive and negative numbers or only
positive numbers.
There are two sign qualifiers: ‘signed’ and
‘unsigned’
These qualifiers can be applied to the data
type ‘int’ and ‘char’ only.
6/21/20 11:52:26 AM 27
CONSTANTS/LITERALS
An entity whose value is not changed during program
execution is called constant. Constants are also called
literals
There are four basic types of constants in C.
Integer Constant
Floating point constant
Character constant
Enumeration constant
6/21/20 11:52:26 AM 28
INTEGER CONSTANTS
An integer constant is an integer valued number
which consists of sequence of digits.
Integer constants can be written in decimal,
octal and hexadecimal number system.
A size or sign qualifier can be appended at the
end of the constant. The suffix ‘u’ is used for
unsigned integer constants, ‘l’ for long integer
constants and ‘s’ for short integer constant.
Unsigned integer constants ( 123u,123U etc.)
Long integer constants (123456l, 123456L etc)
The suffixes can be combined and specified in
any order. (1234567UL, 12345lu etc.)
6/21/20 11:52:26 AM 29
DECIMAL INTEGER CONSTANT
A decimal integer constant can consists of any
combination of digits taken from the set 0
through 9.
If the constant consists two or more digits, the
first digit must be something other than 0.
Examples of valid decimal integer constant
0 32 674 7597 32767 9988
Examples of invalid decimal integer constant
Decimal Integer Constants Reasons
12,345 Illegal character (,)
123.45 Illegal character (.)
12 34 56 Illegal character (blank space)
12-34-56 Illegal character (-)
0123 The first digit cannot be zero
6/21/20 11:52:27 AM 30
OCTAL INTEGER CONSTANT
An octal integer constant consists of any
combination of digits from 0-7.
The first digit of an octal integer constant
must be 0.
Examples of valid octal integer constant
0 032 0674 0757 032767
Examples of invalid octal integer constant
Octal Integer Constant Reasons
6/21/20 11:52:27 AM 31
HEXADECIMAL INTEGER CONSTANT
An hexadecimal integer constant consists of any
combination of digits from 0-9 and a-f.
The first digit of an hexadecimal integer constant
must be 0x or 0X.
Examples of valid hexadecimal integer constant
0x 0X3FE2 0x674 0X759 0x32ab
Examples of invalid hexadecimal integer constant
6/21/20 11:52:27 AM 32
FLOATING POINT CONSTANTS
A floating point constant is a base 10 number
that contains either a decimal point or an
exponent (or both).
Examples of valid floating point constant:
1. 0.23 546.789 45E-7 3E5 32.456E+9
Examples of invalid floating point constant
Floating point constant Reason
6 Either a decimal point or
exponent must be present
12,345.6 Illegal character(,)
123E12.3 The exponent must be an
integer quantity
123E 12 Illegal character(blank
space) in the exponent
6/21/20 11:52:27 AM 33
CHARACTER CONSTANT
A character constant is a single character enclosed in single
quotation marks.
Character constants have integer values that are
determined by the computer’s particular set.
Most computers, and virtually all personal computers,
make use of the ASCII character set, in which each
individual character is numerically encoded with its own
unique-7 bit combination.
The first 32 characters in the ASCII-table are unprintable
control codes and are used to control peripherals such as
printers.
Codes 32-127 are common for all the different variations of
the ASCII table, they are called printable characters,
represent letters, digits, punctuation marks, and a few
miscellaneous symbols. Character 127 represents the
command DEL.
6/21/20 11:52:27 AM 34
STRING CONSTANT
6/21/20 11:52:27 AM 35
There are two simple ways inDEFINING
C to define constants:
CONSTANTS
a. Using #define preprocessor.
b. Using const keyword.
The #define Preprocessor
Syntax:
#define identifier value
Exmple:
#include <stdio.h>
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n’
void main()
{
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
}
When the above code is compiled and executed, it produces the following
result:
value of area : 50
6/21/20 11:52:27 AM 36
The ‘const’ keyword
Syntax:
const type variable = value;
Example
#include <stdio.h>
void main()
{
const int LENGTH=10;
const int WIDTH =5;
const char NEWLINE='\n’;
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
}
When the above code is compiled and executed, it produces the
following result:
value of area : 50
6/21/20 11:52:27 AM 37
EXPRESSIONS
An expression is a combination of variables,
constants and operators written according to
the syntax of C language. In C, every expression
evaluates to a value i. e. every expression
results in some value of a certain type that can
be assigned to a variable.
Some examples of C expressions are shown in
the table given below.
Algebraic Expression C Expression
(a + b) ÷ c (a +b) / c
(m + n) (x + y) (m + n) * (x + y)
(ab ÷ c) a*b/c
3x2 +2x + 1 3*x*x+2*x+1
(x / y) + c x/y+c
6/21/20 11:52:27 AM 38
EXPRESSION CONTD.
Evaluation of Expressions :
Expressions are evaluated using an assignment
statement of the form
Variable = expression;
Variable is any valid C variable name. When the
statement is encountered, the expression is evaluated
first and then replaces the previous value of the
variable on the left hand side.
All variables used in the expression must be assigned
values before evaluation is attempted.
Example of evaluation statements are
x = a * b – c
y = b / c * a
z = a – b / c + d;
6/21/20 11:52:27 AM 39
EXPRESSION CONTD.
The following program illustrates the effect of presence of
parenthesis in expressions.
main ()
{ Output:
float a, b, c x, y, z; x = 10.00 y = 7.00 z = 4.00
a = 9;
b = 12;
c = 3;
x = a – b / 3 + c * 2 – 1;
y = a – b / (3 + c) * (2 – 1);
z = a – ( b / (3 + c) * 2) – 1;
printf (“x = %.2f\t”,x);
printf (“y = %.2f\t”,y);
printf (“z = %.2f”,z);
}
6/21/20 11:52:27 AM 40
STATEMENTS
Statement causes the computer to carry out some
action. There are three types of statement.
Simple statement or Expression statement
Compound statement
Control Statement
Simple statement:
6/21/20 11:52:27 AM 44
TYPE CASTING CONTD.
The process of converting one type of data into
another type manually by writing program code is
called explicit type casting.
The general syntax is
(type_name) expression
Example
float x;
int x1=5;
int x2=2;
x=(float)x1/x2;
printf(“x=%f",x);
Output:
x=2.500000
6/21/20 11:52:27 AM 45