You are on page 1of 50

C Fundamentals

Dr. Ajay Pratap

Assistant Professor

Department of Computer Science and Engineering

Indian Institute of Technology (BHU) Varanasi, India

ajay.cse@iitbhu.ac.in

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 1 / 33


C Fundamentals

Content of this topic:


Constants
Variables
Identifiers
Keywords and Data types
Storage classes

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 2 / 33


The C Character Set

A character denotes any alphabet, digit or special symbol used to


represent information.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 3 / 33


C Tokens

The smallest individual units are known as C tokens. C has six types
of tokens as follows:

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 4 / 33


Keywords

Keywords: Keywords have fixed meanings, and the meaning cannot


be changed. They act as a building block of a ’C’ program. There are
total 32 keywords in ’C’. Keywords are written in lowercase letters
such as follows:

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 5 / 33


Identifiers

Identifiers: A C identifier is a name used to identify a variable,


function, or any other user defined item. An identifier starts with a
letter A to Z, a to z, or an underscore ‘ ’ followed by zero or more
letters, underscores, and digits (0 to 9).
C does not allow punctuation characters such as @, $, and % within
identifiers. C is a case-sensitive programming language. Thus,
Manpower and manpower are two different identifiers in C.
Valid identifiers: marks, total marks, gross salary 1987, power(),
num[100].
Invalid identifiers: 100a, total marks, gross-salary-1987,
area under the curve.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 6 / 33


C Constants

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 7 / 33


Rules for Constructing Integer Constants

An integer constant must have at least one digit


It must not have a decimal point.
It can be either positive or negative.
If no sign precedes an integer constant it is assumed to be positive.
No commas or blanks are allowed within an integer constant.
The allowable range1 for integer constants is -32768 to 32767.
Example: 426, +782, -8000, -7605, etc.

1
The range of an Integer constant depends upon the compiler.
Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 8 / 33
Rules for Constructing Real Constants

Real constants are often called Floating Point constants. The real
constants could be written in two forms—Fractional form and
Exponential form.
A real constant must have at least one digit.
It must have a decimal point.
It could be either positive or negative.
Default sign is positive.
No commas or blanks are allowed within a real constant.
Examples: +325.34, 426.0, -32.76, -48.5792.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 9 / 33


Rules for Constructing Real Constants

Real constants are often called Floating Point constants. The real
constants could be written in two forms—Fractional form and
Exponential form.
A real constant must have at least one digit.
It must have a decimal point.
It could be either positive or negative.
Default sign is positive.
No commas or blanks are allowed within a real constant.
Examples: +325.34, 426.0, -32.76, -48.5792.
The exponential form of representation of real constants is usually
used if the value of the constant is either too small or too large.
In exponential form of representation, the real constant is represented
in two parts. The part appearing before ‘e’ is called mantissa,
whereas the part following ‘e’ is called exponent.
Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 9 / 33
Rules for Constructing Real Constants

The mantissa part and the exponential part should be separated by a


letter e.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 10 / 33


Rules for Constructing Real Constants

The mantissa part and the exponential part should be separated by a


letter e.
The mantissa part may have a positive or negative sign.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 10 / 33


Rules for Constructing Real Constants

The mantissa part and the exponential part should be separated by a


letter e.
The mantissa part may have a positive or negative sign.
Default sign of mantissa part is positive.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 10 / 33


Rules for Constructing Real Constants

The mantissa part and the exponential part should be separated by a


letter e.
The mantissa part may have a positive or negative sign.
Default sign of mantissa part is positive.
The exponent must have at least one digit, which must be a positive
or negative integer.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 10 / 33


Rules for Constructing Real Constants

The mantissa part and the exponential part should be separated by a


letter e.
The mantissa part may have a positive or negative sign.
Default sign of mantissa part is positive.
The exponent must have at least one digit, which must be a positive
or negative integer.
Default sign is positive.
Range of real constants expressed in exponential form is -3.4e38 to
3.4e38.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 10 / 33


Rules for Constructing Real Constants

The mantissa part and the exponential part should be separated by a


letter e.
The mantissa part may have a positive or negative sign.
Default sign of mantissa part is positive.
The exponent must have at least one digit, which must be a positive
or negative integer.
Default sign is positive.
Range of real constants expressed in exponential form is -3.4e38 to
3.4e38.
Example: +3.2e-5, 4.1e8, -0.2e+3, -3.2e-5.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 10 / 33


Rules for Constructing Character Constants

A character constant is a single alphabet, a single digit or a single


special symbol enclosed within single inverted commas. Both the
inverted commas should point to the left. For example, ’A’ is a valid
character constant whereas ‘A’ is not.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 11 / 33


Rules for Constructing Character Constants

A character constant is a single alphabet, a single digit or a single


special symbol enclosed within single inverted commas. Both the
inverted commas should point to the left. For example, ’A’ is a valid
character constant whereas ‘A’ is not.
The maximum length of a character constant can be 1 character.
Example: ’A’, ’I’,’5’, ’=’

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 11 / 33


C Variables

C Variables: An entity that may vary during program execution is


called a variable.
Variable names are names given to locations in memory.
These locations can contain integer, real or character constants.
An integer variable can hold only an integer constant, a real variable
can hold only a real constant and a character variable can hold only a
character constant.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 12 / 33


Rules for Constructing Variable Names

The first character in the variable name must be an alphabet or


underscore.
No commas or blanks are allowed within a variable name.
No special symbol other than an underscore (as in gross sal) can be
used in a variable name.
Type declaration statements:

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 13 / 33


Rules for Constructing Variable Names

The first character in the variable name must be an alphabet or


underscore.
No commas or blanks are allowed within a variable name.
No special symbol other than an underscore (as in gross sal) can be
used in a variable name.
Type declaration statements:
int si, m hra ;
float bassal ;
char code ;

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 13 / 33


Data Types
primary data types could be of three varieties—char, int, and float.
C can derive many data types from these three types.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 14 / 33


Data Types
primary data types could be of three varieties—char, int, and float.
C can derive many data types from these three types.
Integers, long and short: The range of an Integer constant depends
upon the compiler.
For a 16-bit compiler like Turbo C or Turbo C++ the range is –32768
to 32767. For a 32-bit compiler the range would be –2147483648 to
+2147483647.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 14 / 33


Data Types
primary data types could be of three varieties—char, int, and float.
C can derive many data types from these three types.
Integers, long and short: The range of an Integer constant depends
upon the compiler.
For a 16-bit compiler like Turbo C or Turbo C++ the range is –32768
to 32767. For a 32-bit compiler the range would be –2147483648 to
+2147483647.
C allows the abbreviation of short int to short and of long int to
long.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 14 / 33


Data Types
primary data types could be of three varieties—char, int, and float.
C can derive many data types from these three types.
Integers, long and short: The range of an Integer constant depends
upon the compiler.
For a 16-bit compiler like Turbo C or Turbo C++ the range is –32768
to 32767. For a 32-bit compiler the range would be –2147483648 to
+2147483647.
C allows the abbreviation of short int to short and of long int to
long.
short int j ; short int height ; or short j ; short height;

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 14 / 33


Integers, signed and unsigned

Sometimes, we know in advance that the value stored in a given


integer variable will always be positive: unsigned.
unsigned int num students; or unsigned num students;

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 15 / 33


Integers, signed and unsigned

Sometimes, we know in advance that the value stored in a given


integer variable will always be positive: unsigned.
unsigned int num students; or unsigned num students;
The range of permissible integer values (for a 16-bit OS) will shift
from the range -32768 to +32767 to the range 0 to 65535. Thus,
declaring an integer as unsigned almost doubles the size of the largest
possible value that it can otherwise take.
Still occupies two bytes.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 15 / 33


Integers, signed and unsigned

Sometimes, we know in advance that the value stored in a given


integer variable will always be positive: unsigned.
unsigned int num students; or unsigned num students;
The range of permissible integer values (for a 16-bit OS) will shift
from the range -32768 to +32767 to the range 0 to 65535. Thus,
declaring an integer as unsigned almost doubles the size of the largest
possible value that it can otherwise take.
Still occupies two bytes.
Like an unsigned int, there also exists a short unsigned int and a long
unsigned int. By default a short int is a signed short int and a long
int is a signed long int.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 15 / 33


Chars, signed and unsigned
Both, signed and unsigned chars, occupying one byte each, but
having different range
char ch = ’A’ ;
Here what gets stored in ch is the binary equivalent of the ASCII
value of ‘A’ (i.e. binary of 65).
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.

1
ASCII is the acronym for the American Standard Code for Information Interchange.
It is a code for representing 128 English characters as numbers, with each letter assigned
a number from 0 to 127. For example, the ASCII code for uppercase M is 77. Most
computers use ASCII codes to represent text, which makes it possible to transfer data
from one computer to another.
Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 16 / 33
Chars, signed and unsigned
Both, signed and unsigned chars, occupying one byte each, but
having different range
char ch = ’A’ ;
Here what gets stored in ch is the binary equivalent of the ASCII
value of ‘A’ (i.e. binary of 65).
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.

1
ASCII is the acronym for the American Standard Code for Information Interchange.
It is a code for representing 128 English characters as numbers, with each letter assigned
a number from 0 to 127. For example, the ASCII code for uppercase M is 77. Most
computers use ASCII codes to represent text, which makes it possible to transfer data
from one computer to another.
Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 17 / 33
Floats and Doubles

A float occupies four bytes in memory and can range from -3.4e38 to
+3.4e38.
Double data type occupies 8 bytes in memory and has a range from
-1.7e308 to +1.7e308.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 18 / 33


Range of Different Data Types

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 19 / 33


Storage Classes in C

To fully define a variable one needs to mention not only its ‘type’ but
also its ‘storage class’.
In other words, not only do all variables have a data type, they also
have a ‘storage class’.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 20 / 33


Storage Classes in C

To fully define a variable one needs to mention not only its ‘type’ but
also its ‘storage class’.
In other words, not only do all variables have a data type, they also
have a ‘storage class’.
Storage classes have defaults.
From C compiler’s point of view, a variable name identifies some
physical location within the computer where the string of bits
representing the variable’s value is stored.
Values are stored in memory and CPU registers.
It is the variable’s storage class that determines in which of these two
locations the value is stored.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 20 / 33


Storage Classes in C

A variable’s storage class tells us:


Where the variable would be stored.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 21 / 33


Storage Classes in C

A variable’s storage class tells us:


Where the variable would be stored.
What will be the initial value of the variable, if initial value is not
specifically assigned.(i.e. the default initial value).
What is the scope of the variable; i.e. in which functions the value of
the variable would be available.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 21 / 33


Storage Classes in C

A variable’s storage class tells us:


Where the variable would be stored.
What will be the initial value of the variable, if initial value is not
specifically assigned.(i.e. the default initial value).
What is the scope of the variable; i.e. in which functions the value of
the variable would be available.
What is the life of the variable; i.e. how long would the variable exist.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 21 / 33


Storage Classes in C

A variable’s storage class tells us:


Where the variable would be stored.
What will be the initial value of the variable, if initial value is not
specifically assigned.(i.e. the default initial value).
What is the scope of the variable; i.e. in which functions the value of
the variable would be available.
What is the life of the variable; i.e. how long would the variable exist.
There are four storage classes in C:
Automatic storage class
Register storage class
Static storage class
External storage class

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 21 / 33


Automatic Storage Class

The features of a variable defined to have an automatic storage class


are as under:

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 22 / 33


Automatic Storage Class

Example:

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 23 / 33


Automatic Storage Class

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 24 / 33


Register Storage Class

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 25 / 33


Static Storage Class

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 26 / 33


Difference Between the Automatic and Static Storage
Classes

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 27 / 33


External Storage Class

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 28 / 33


External Storage Class

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 29 / 33


External Storage Class

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 30 / 33


Which to Use When

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 31 / 33


Suggested Readings/Reference Materials

1st and 6th Chapters of:


Yashavant P. Kanetkar ”Let US C”, 5th Edition.

Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 32 / 33


Dr. Ajay Pratap (IIT (BHU), Varanasi) C Fundamentals ajay.cse@iitbhu.ac.in 33 / 33

You might also like