Upload
tribhuwan-srivastava
View
221
Download
0
Embed Size (px)
Citation preview
8/7/2019 13407_Basics of C Lang
1/50
Constants, Variables,and Data Types
8/7/2019 13407_Basics of C Lang
2/50
Like any other language, C has itsown vocabulary and grammar. Inthis chapter, we will discuss theconcepts of constants andvariables and their types as they
relate to C programming language.
Constants, Variables, and
Data Types
8/7/2019 13407_Basics of C Lang
3/50
The type of an object determines the set
of values it can have and what
operations can be performed on it.Variables and constants are the basic
data objects manipulated in a program.
Declarations list the Variables to be
used, and state what type they have
and perhaps what their initial values are.
Introduction
8/7/2019 13407_Basics of C Lang
4/50
8/7/2019 13407_Basics of C Lang
5/50
The characters in C are grouped into thefollowing categories:
1. Letters
2. Digits
3. Special characters
4. White spaces
The entire character set is given inTable 2.1page 23.
Character Set
8/7/2019 13407_Basics of C Lang
6/50
C tokens
Fig. 2.1 C tokens and examples
8/7/2019 13407_Basics of C Lang
7/50
Keywords and Identifiers
Every C word is classified as either a keywordor an identifier.
All keywords have fixed meanings and thesemeanings cannot be changed. Keywordsserve as basic building blocks for programstatements.
The list of all keywords of ANSI C are listed inTable 2.3(page 24).
All keywords must be written in lowercase.Some compilers may use additional keywordsthat must be identified from the C manual.
8/7/2019 13407_Basics of C Lang
8/50
Identifiers refer to the names of variables,functions and arrays. These are user-defined
names and consist of a sequence of letters
and digits, with a letter as a first character.
Both uppercase and lowercase letters are
permitted, although lowercase letters are
commonly used.
The underscore character is also permitted inidentifiers. It is usually used as a link between
two words in long identifiers.
8/7/2019 13407_Basics of C Lang
9/50
8/7/2019 13407_Basics of C Lang
10/50
Constants
Constants in C refer to fixed values
that do not change during the
execution of a program. C supportsseveral types of constants as
illustrated in Fig. 2.2.
8/7/2019 13407_Basics of C Lang
11/50
Constants
See Page 25-26
8/7/2019 13407_Basics of C Lang
12/50
Constants
There are several constants in C,such asinteger constant, floating-point constant,char constant, string constant,
enumeration constant.Integer constant can be specified indecimal, octal, and hexadecimal.1234 is adecimal integer constant, 037 which hasprefix 0 is an octal integer constant, and0x1f or 0x1F which has prefix0xis ahexadecimal integer constant.
8/7/2019 13407_Basics of C Lang
13/50
Constants
/* exam2-3.c */
#include "stdio.h"
main( )
{
printf("Integer values\n\n");printf("%d %d %d\n",32767,32767+1,32767+10);
printf("\n");
printf("Long integer values\n\n");
printf("%ld %ld %ld\n", 32767L,32767L+1,32767L+10);getch( );
}
8/7/2019 13407_Basics of C Lang
14/50
Constants
char constant is an integer, written as
one character within single
quotes,such as x, including escape
sequences like \n. These escape
sequences look like two characters,
but represent only one.
8/7/2019 13407_Basics of C Lang
15/50
Constants
In addition, an arbitrary byte-sized bitpattern can be specified by\ooo, whereooo is one to three octal digits(07) orby\xhh,where hh is one or more
hexadecimal digits (09,af or AF).Sowe might write :
#define VTAB \013
/* ASCII vertical tab */#define BELL \007
/* ASCII bell character */
8/7/2019 13407_Basics of C Lang
16/50
Constants
Or, in hexadecimal
#define VTAB \xb
/* ASCII vertical tab */
#define BELL \x7
/* ASCII bell character */
8/7/2019 13407_Basics of C Lang
17/50
Constants
The complete of escape sequences is:
\a Alert (bell) character \\ Backslash
\b Backspace \? Question mark
\f Form feed \ Single quote\n Newline \ Double quote
\r Carriage return \ooo Octal number
\t Horizontal tab \xhh Hexadecimal
number
\v Vertical tab
8/7/2019 13407_Basics of C Lang
18/50
Constants
The character constant \0represents the character with valuezero, the null character.
A constant expression is anexpression that involves onlyconstants. Such expression may beevaluated during compilation ratherthan run-time, and accordingly may
be used in any place that a constantcan be occur.
8/7/2019 13407_Basics of C Lang
19/50
Constants
A string constant is a sequence of
zero or more characters surroundedby double quotes, as in it is a string,
or /* the empty string */ . The quotes
are not the part of the string.
8/7/2019 13407_Basics of C Lang
20/50
Constants
Technically, a string constant is an array
of characters. The internal representation
of a string has a null character\0at theend, so the physical storage required is
one more than number of characters
written between the quotes.
8/7/2019 13407_Basics of C Lang
21/50
Constants
This representation means that there is
no limit to how long a string can be, but
programs must scan a string completely
to determine its length.
8/7/2019 13407_Basics of C Lang
22/50
Constants
Notice: a is not the same as a. The
former is an integer, used to produce
the number value of the letter a in themachines character set. The latter is an
array of characters that contains one
character (the letter a) and the null
character ( \0 ).
8/7/2019 13407_Basics of C Lang
23/50
Variable
In c, there are some restrictions on
the names of variables and
symbolic constants.
Names are made up of letters anddigits;the first character must be a
letter. The underscore _ counts
as a letter; it is sometimes usefulfor improving the readability of long
variable names.
8/7/2019 13407_Basics of C Lang
24/50
Variable
Dont begin variable names with
underscore, however, since library
routines often use such names.
Upper case and lower case letters
are distinct, so x and X are two
different names. Traditional C
practice is to use lower case forvariable names, and all upper case
for symbolic constants.
8/7/2019 13407_Basics of C Lang
25/50
Variable
ANSI standard recognizes a length of
31 characters. However, length
should not be normally more thaneight characters, since only the first
eight characters are treated as
significant by many compilers. .
8/7/2019 13407_Basics of C Lang
26/50
Variable
We tend to use short names for local
variables, and longer names for external
variables. Usually the length of variable
names are less than 8 characters.
Keywords like if, else, int, float, etc. are
reserved: you cant use them as variable
names.
8/7/2019 13407_Basics of C Lang
27/50
C language is rich in its data types. The
variety of data types available allow the
programmer to select the type appropriate to
the needs of the application as well as themachine.
ANSI C supports three classes of data types:
1. Primary (or fundamental) data types
2. Derived data types
3. User-defined data types
Data Types and Sizes
8/7/2019 13407_Basics of C Lang
28/50
Data Types and Sizes
The primary data types and their
extensions are discussed in this
section. The user-defined data types
are defined in the next section while
the derived data types such as arrays,
functions, structures and pointers are
discussed as and when they areencountered.
8/7/2019 13407_Basics of C Lang
29/50
Fig. 2.4 Primary data types in C
8/7/2019 13407_Basics of C Lang
30/50
8/7/2019 13407_Basics of C Lang
31/50
8/7/2019 13407_Basics of C Lang
32/50
Data types
char
Range of values
128 to 127
int 32,768 to 32,767
float 3.4e38 to 3.4e+e38
double 1.7e308 to 1.7e+308
Primary data types and sizes
8/7/2019 13407_Basics of C Lang
33/50
Data Types and Sizes
In addition, there are a number of
qualifiers that can be applied to there
basic types. Such as short, long,signed, unsigned.
short int sh; long int counter;
/*here int can be omitted.*/
8/7/2019 13407_Basics of C Lang
34/50
char or signed char 8 128 to 127
unsigned char 8 0 to 255
int or signed int 16 32,768 to 32,767unsigned int 16 0 to 65535
short int or
signed short int 8 128 to 127
unsigned short int 8 0 to 255
long int or
signed long int 32 2,147,483,648 to
2,147,483,647
unsigned long int 32 0 to 4,294,967,295
float 32 3.4E 38 to 3.4E + 38
double 64 1.7E 308 to 1.7E + 308
long double 80 3.4E 4932 to 1.1E + 4932
8/7/2019 13407_Basics of C Lang
35/50
Data and i
Different type objects have differentstorage bits. So the range of therepresented value are not same. short is
often 16 bits, long 32 bits, and int either16 or 32 bits. Each compiler is free tochoose appropriate sizes for its ownhardware, subject only to the restrictionthat shorts and ints are at least 16 bits,
longs are at least 32 bits, and short is nolonger than int, which is no longer thanlong.
8/7/2019 13407_Basics of C Lang
36/50
Data Types and Sizes
The qualifier signed or unsignedmay be applied to char or anyinteger.
Unsigned numbers are alwayspositive or zero, and obey the laws
of arithmetic modulo 2n,where n isthe number of bits in the type.
8/7/2019 13407_Basics of C Lang
37/50
Data Types and Sizes
So, for instance, if chars are 8 bits,
unsigned char variables have values
between 0 to 255, while signed chars have
values between 128 and 127. Whetherplain chars are signed or unsigned is
machine-dependent, but printable
characters are always positive.
Look at the limits.h and float.h to get a
reference.
8/7/2019 13407_Basics of C Lang
38/50
Declarations
All variables must be declared before
use.
A variable can be used to store a
value of any data type.
8/7/2019 13407_Basics of C Lang
39/50
Declarations
A declaration specifies a type, andcontains a list of one or more variables ofthat type:
int lower, upper, step;
char c, line[1000];You can write it as :
int lower;
int upper;
int step;char c, line[1000];
8/7/2019 13407_Basics of C Lang
40/50
Declaration of Storage Class
Variables in C can have not only data
type but also storage class that provides
information about their location andvisibility. The storage class decides the
portion of the program within which the
variables are recognized.
8/7/2019 13407_Basics of C Lang
41/50
The storage class is another qualifier(like long orunsigned) that can beadded to a variable declaration asshown below:
8/7/2019 13407_Basics of C Lang
42/50
8/7/2019 13407_Basics of C Lang
43/50
Storage
class
auto
Meaning
Local variable known only to the function in which
it is declared. Default
is auto.
staticLocal variable which exists and retains its value
even after the control is
transferred to the calling function.extern Global variable known to all functions in the file.
register Local variable which is stored in the register.
Table 2.10 Storage Classes and Their Meaning
8/7/2019 13407_Basics of C Lang
44/50
Assigning Values to Variables
A variable may also be initialized in itsdeclaration.
char esc=\\;
float eps=1.0e-5;
If the variable in question is notautomatic, the initialization is done onceonly, conceptually before the program
starts executing, and the initializer mustbe a constant expression.
8/7/2019 13407_Basics of C Lang
45/50
Assigning Values to Variables
An explicitly initialized automatic variable
is initialized each time the function or
block it is entered; the initializer may be
any expression.
External and static variables are
initialized to zero or
(for char array) bydefault.
8/7/2019 13407_Basics of C Lang
46/50
Assigning Values to Variables
Automatic variables for which there is no
explicit initializer have undefined
values(i.e., garbage).
The qualifier const can be applied to the
declaration of any variable to specify that
its value will not be changed. It is can also
be used with array arguments, to indicatethat the function does not change that
array argument.
8/7/2019 13407_Basics of C Lang
47/50
/* ex1-3.c */
#include
/*print Fahrenheit-Celsius table for fahr=0,20,,300*/
main( )
{float fahr,celsius;
int lower,upper,step;
lower=0; /* lower limit of temperature table */upper=300; /* upper limit */
step=20; /* step size*/
fahr=lower;
while (fahr
8/7/2019 13407_Basics of C Lang
48/50
Defining Symbolic Constants
The 4th version of the temperature converter(ex1-5.c):
#include
/*print Fahrenheit-Celsius table forfahr=0,20,,300*/
#define LOWER 0 /* lower limit of table */
#define UPPER 300 /* upper limit of table */
#define STEP 20 /* step size */
main( )
{ int fahr;
for (fahr=LOWER; fahr
8/7/2019 13407_Basics of C Lang
49/50
Defining Symbolic Constants
A #define line defines a symbolic name orsymbolic constant to be a particular string of
characters:
#define name replacement text
thereafter, any occurrence of name (not in
quotes and not part of another name) will be
replaced by the corresponding replacement
text.
The name has the same form as a variable
name:a sequence of letters and digits that
begins with a letter.
8/7/2019 13407_Basics of C Lang
50/50
The replacement text can be anysequence of characters; it is not limited tonumbers.
Notice: symbolic constant differs from
variable, symbolic constant can not beassigned again in the program. Usuallysymbolic constants name are written inupper case so then can be readily
distinguished from lower case variablenames. And there is no semicolon at theend of a #define line.
Defining Symbolic Constants