Professional Documents
Culture Documents
Chap 3
Chap 3
Chapter
3
3.1. Introduction
kind of data consisting of numbers,
is designed to process certain
A programming language known as "information. The
task of processing of
useful output
characters and strings to provide called "program". These
instructions
executing a sequence of pre-instructions
data is accomplished by known as "syntax rules"
and words according to some rigid rules
are formed using certain symbols and grammar. In this chapter
we
C has its own vocabulary
or "grammer". Like any
other language,
variables and data types.
discuss the concepts of constants,
Table 3.1
A,B,...... Y, Z
1. Alphabets Uppercase
a,b,. .y, Z
Lowercase
9
2. Digits All decimal digits 0,1,2.....8,
)_+-={}l?7;|
3. Special Symbols *!@# %^& *
new line
4. White Spaces tab, blank spaces, carriage return,
3.3. CTokens in
tokens. Similarly,
In a marks are called these these
passage text,
« individual words and punctuation
written with
the help of
kens. There
smallest individual unit is called C-token. C programs are
are shown
here are six types of tokens as
Or tokens in C language. C has
-2 Constants, Variables & Data TyDee
pes
CTOKENS
KEYWORDS OPERATORS
CONSTANTS STRINGS
SPECIAL
IDENTIFIERS SYMBOLS
Figure 3.1
3.4. Keywords and Identifiers
3.4.1. Keywords
Keywords are the words whose meaning is
to
compiler .The keywords cannot be used asalready
C explained Definition
variable Definition
because if we do so we are names
trying to assign a new meaning to the Keywords are the words whose
keyword.Keywords are called reserved
words. All keywords must meaning
meaning isis already
already defined
defined in
n the
be written in lowercase.
In C there are 32 Compiler
keywords. These are
Table 3.2
auto
double int
break else struct
case
enum
long switch
char register typedef
extern
const return union
float
continue short unsigned
for
default goto
signed void
do sizeof volatile
if
static while
3.4.2. Identifiers
dentifiers refer to the
name of
ise
uCea names and consist variables, functions, arrays, strings, e at
an
1aenuiier must an of alphabets, digits structures, aracter
alphabet. No special symbol both in upper and lower cases. The
nist
Examples: rsd, bs_salary is used as identifier
(except unaetoe
Constants,
Variables
&Data Types 3-3
3.5. Constants
constant is a data storage location used by your program whose value does not change
C
A
ino the execution of the program. Constants are classified into two categories.
cONSTANTS
NUMERIC CHARACTER
SINGAL
REAL STRING
INTEGER CHARACTER
Figure 3.2
constants consists
of combination of digits
a) Octal integer
character).
auways preceded by leading o or O(English
0-9 and
Examples :-025,05245,00,-035. combination of digits from
are represented by characters A-F
respectively.
D) Hexadecimal integer constants represented by
10-15 are
chara A-F alone. Digits from oX or ox.
ETS from or
preceeded by
OX or Ox or
are always
a l integer c o n s t a n t s
Examples:-Ox44,0X9889,0X123A etec. from 0-9
represented by
combination of digits
constants are
Decimal integer
Example 23,566
3-4 Constants, Variables & Dataa Types
T
Rules for constructing integer constant
1. An integer constant must have at least one digit.
2. It must not have a decimal point.
3. It can be either positive or
negative.
4. It is assumed to be positive, if there is no sign
specified.
5. Spaces and commas are not allowed with in it.
6. It can have values from 32768 to +32768
Examples of valid integer constants are: 346,134,3201,-665
3.5.1.2 Real constant
Real constants are used to
represent the
quantities are distances, weights, heights andquantities
that vary rapidly. The
examples of suh
prices. Real constants contain fractional
accurately represent such quantities. Real constants are also called parts
constants are further as
floating point
of two types: real constants in fraction form and real constants constants. Red
form. in exponentia
The fractional form real
constants are similar to
Some valid real constant in integer constants with added decimal
fractional form precision
Rules for
are
constructing real constants in fraction form 120.588,8484.345 and 97.25.
Real constants are also
called
namely, fractional and exponential floating
form.
point constants. They can be divided into two fo
constructing real constants Following are the rules that must be followed
1. It must expressed fractional form.
in w
have at least one
2. It must have a digit.
decimal point.
3. It could be
either positive or negative.
4. Positive is
considered as default
5. Commas and spaces are not sign.
Examples of some real constantsallowed in a real constant.
are
Real constants fractional
forms 54.564,-65.235, and 5555.78.
numbers. Due to this, real constants in are not adequate for
written as 5.5E9 when exponential form representing very
are used. For
small and very
very Sma
andexponent. The partrepresented in
exponential form, example, » 0000
appearing before "E" is called as constant contains two pat antis
a real
can
Exponent. ed
Mantissa and the part after it called
For example, in constant
valid examples of real 125.3e5, 125.3 is the
constants in
exponential mantissa part and 5 is the
part. So
Rules for constructing real constants in form are 4.le12, -56.3e3 etc. expone
1. The mantissa and the exponent Exponent form
part must be
2. The mantissa may be
negative or
positive.
separated by character e.
3. The exponent must contain
4. The default sign for both
number, which must
be an integer of any
mantissa and the sign
5. It can have values from exponent is positive.
range -3.4e38 to
Examples: +2.8e-4, 2.3e2, -0.5e-1. 3.4e38.
Variables & Data Types
Constants, 3-5
3.5.2. Character constants
In addition to numeric constants, the programs also need to deal with characters and strings.
In.
The character const nstants are used to express quantities like name, place or gender. They can be a
s i n g l e c h a r a c t e r or a group of characters or back slash character constants.
character
constant
3.5.2.1. Single
A single character constant contains a single character. They are enclosed with in single quotation
narks. A character constant
can be either single alphabet, a single digit or a single special symbol
a
3.6. Variables
will frequently want your program Definition
As programmer, you
a
if
"remember" a value. For example, your program
requests a A variable is a quantity whose
to
value from the user, or if
it calculates a value, you
will want to value may vary during execution.
remember it somewhere so you
can use it later. The way your t is a store house for a data item
remembers things is by using
variables. Itis the name given to the stored.
program value is
be assigned different
values at various places location where the
A variable can
be declared before it is
used in a
in the program. A variable must
program.
Example: int b; integer value." A
hold one
called b that is able to
want to create a space store a value
This line says, "I integer). You can
type (in this case, int, an
b) and a
vanable has a name (in this case,
in b by saying something like:
b 5; like:
YOU can use the value in b by saying something
printf("od", b);
for variables
n C, there are several standard types
i n t - integer (whole number) values
float floating point values
as "m" or "Z)
Char single character values (such
Rules for creating Variable character ().
underscore
letters, digits, and the
can contain is also a legal first
character.
ame letter. The
underscore
character of the n a m e must be a
rst rerer
but its use is not Count
recommended. count and
lowercase letters).
Thus, the names
enum day
Output
Day of Week is 1
Constants, ariables & Data Types
3.9
umerated data type
Program:
#include<stdio.h>
#include<conio.h>
main ()
enum
Output
0
the
enumerators can be modified explicitly by initializing
The default integer values of
enumerators with new values.
enum day
Output is
Day of Week is 6
remaining
In above pro value 5, the
explicitly the
example, Sunday is assigned
values
ues
incrementprogramming
by 1.
3-10
Constants,
iables &Data
typedef Data Type Type
It allows defining new data types that are equivalent to existing data
The format for type.
typedef data type
typedef type name;
Where type is either built in data
the type. type or user defined data type and name is the
new nama
The typedef keyword tells the
type.
compiler to
recognize the word name
as
synonymous
Example. The following C statement illustrates the use of
typedef float
typedef data type.
real;
redefine the data type float to
real.
Program: To illustrate the use of
#include <stdio.h>
typedef
typedef float real;
main()
real a,b,c,sum;
printf("Enter the value of a,b,c"):
scanf(%f%f%f",&a,&b,&c);
sum =a+b+c;
printf("Sum-%f',sum);
#include <stdio.h>
main ()
typedef enum
unday, Monday, Tuesday, Wednesday, Thursday, Friday,
day: a
day dofweek;
dofweek=monday;
printf" Day of Week is %d", dofweek);
Output
Day of Week is 1
4. Void Data
Void data Type
type represents
empty or null.
Vanables & Data Types
Constants, 3-11
t VOU have a summary of the basic fundamental data types in C, as well as the range of
0 to 255 Toc
Unsigned char
short signed int 2 -32768 to 32767 %d
Things to Remember
is called character set.
Collection of alphabets, digits, special
symbols and white spaces
execution of a program.
value that do not change during
A constant refers
to a
words). erc.
functions ,arrays, strings
aentifiers refer to the names of variables,
or underscore(-).
be an alphabet
C irst character of an identifier must
Numeric constants can be integer and real.
Four fundament
dnentals data are available in
types
Ci.e. int, float,
char, douoi
Q.2. What is a variable? What are the rules what have to follow to declare a e
Ckeywords can't be used as variable names. A keyword is a word that is part ofthe
C language.
mean by data type ? Explain the basic data types ?
3 , What do you
Data type defines the amount of memory required by a variable, There are differet
Ans.
data types available in C language. Each data type have its own significance. For example
it of int type. and if we
If we want the to enter the total number of students .We declare
want to enter the salary of an employee it must be in float type.
DATA TYPES
J
PRIMARY VOID DERIVED USER DEFINED
ARRAYS STRUCTURE
INTEGER FLOAT CHARACTER POINTERS UNIONS
ENUM
TYPEDEF
Figure 3.44
fractions floating
In programming som store the vaule in the helpof
ome time we want to nep
to enter the allows this
with the
with
fractions.
C programming
i data salary ofthe employee.
point used to store
n u m b e r s
Q4 How
ow many ways are there in C to represent an integer?
CC language a interger can be represented in the following ways :
Ans.
meaning.
are SUM, sum, xx etc
.
me example 6. Some example
are int, float case etc.
and variable?
Q.6. between constant
What is the difference
Ans.
Variable
in memory
location
location
es not store in a memory. 2. It stores to memory
3. CaR is a name given
Mant 3. Variable
is stored
quantity which is fixed. where
constant