0% found this document useful (0 votes)
13 views

ch02 Variables

Variables are used to store and represent data in a program. There are two classes of elementary variable types: integral types for whole numbers and floating-point types for real numbers. Integral types include char, short, int, and long, while floating-point types are float and double. Variables must be declared before use with a type and name to allocate memory, but are not initialized to a default value.

Uploaded by

Vikrant Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

ch02 Variables

Variables are used to store and represent data in a program. There are two classes of elementary variable types: integral types for whole numbers and floating-point types for real numbers. Integral types include char, short, int, and long, while floating-point types are float and double. Variables must be declared before use with a type and name to allocate memory, but are not initialized to a default value.

Uploaded by

Vikrant Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

CHAPTER 2 VARIABLES

ELEMENTARY VARIABLE TYPES


 A variable is a piece of memory in which the program stores information.

 In C there are two classes of elementary variable types:


 Integral types.
 Floating-point types.

 The data to be stored in a variable of one of the integral types is a whole number.
 The data stored in a floating type is a real number.
Floating point representation involves splitting the numberinto3 parts:

 The sign of the number.


 The mantissa ( 0 < mantissa < 1 ).
 The exponent.
INTEGER VS. FLOATING-POINT
Integer Floating-point

Whole numbers Real numbers

Small range Large range

1,2 or 4 bytes, depending 4 or 8 bytes, depending


on the type on the type
Faster operations Slower operations*

* Some machines have a floating-point-coprocessor, which is a piece of hardware, external


to the CPU, responsible for floating point operations.
TYPE OF VARIABLES
Data Types

Scalar Aggregate
void
Types Types

Pointers Arithmetic Enumerated


Types Types

Integral Floating
Types Types

char short int long float double


INTEGRAL TYPES
 Integral types include : int, short, long and char.
Represented in memory in 2’s complement mode or in normal
binary mode.

Size: one byte.


Range : -128 - 127 or 0 - 255
char
Conversion specifications : %c

(%d only in printf)


Can contain : 'a', 97, '+', '\n' and more.

Size : 2 bytes.
Range : -32768 - 32767 or 0 - 65535
short
Conversion specification : %hd
Can contain : -2000, 2001, 0x62F3, 'a' and more.
INTEGRAL TYPES – CONT’D
Size : 2 or 4 bytes, depending on the machine.

int Range: when 2 bytes : -32768 - 32767 or 0 - 65535


when 4 bytes : -2147483648 - 2147483647
or 0 - 4294967295
Conversion specification : %d
Can contain : when 2 bytes : -2000, 2001, 0xAB92, 'a‘
when 4 bytes : 100001, -100001 and more.

Size : 4 bytes.
Range: -2147483648 - 2147483647
long
or 0 - 4294967295
Conversion specification : %ld
Can contain : 20000, -800, 'r', 0xA3F4 and more.
FLOATING POINT TYPES
 Floating point types include : float and double. They are
represented in memory in floating point mode.

Size : 4 bytes.
float Range (absolute values) : 3.4e-38 to 3.4e38
(Numbers may, of course, be negative)
Conversion specification : %f
Can contain : 0.2, -100.3, 42, -7 and more.

Size : 8 bytes.
Range (absolute values) : 1.7e-308 to 1.7e308
double (Numbers may, of course, be negative)
Conversion specification : %f for output .%lf for input.
Can contain : 999999999999999.9 and more...
DECLARATION OF VARIABLES
 As mentioned before, A variable is a piece of memory, in which
the program stores information.
 A variable must be declared before it is used.
 Declaring a variable means - specifying its type and its name.
 Example:
 int num; /* type : int
name : num
Data represented in 2's complement
binary */
 float f1, f2; /* type : float
name : f1, f2.
We have 2 variables of type float.
Data represented in floating
point */
NAMES OF VARIABLES
 In order to use the variable, we give it a name.
When choosing a name for a variable, we must abide to some
rules.

 Rules for a legal variable name:


A variable name may contain:
 Digits ( 0 to 9 ).
 Letters, both lower case and upper case

(‘a’-’z’ and ‘A’-’Z’).


 Underscores ('_').

 The maximum number of characters in a name is 31.


 The first character must not be a digit.
DECLARATION OF VARIABLES – IMPORTANT
!!
 The declaration does not give a value to the variables.
The variable contains whatever was in that piece of memory
before. Programmers tend to call this ‘garbage’.
 Do not assume that declaring a variable automatically
initializes it with a certain value.
 There are exceptions to this rule, which will be learned in a
later chapter.
GIVING VALUES TO VARIABLES
 A variable can be given a value in a few ways :
 Assignment :

int num; /* num is of type int. It


contains ‘garbage’ */
num = 42; /* the value 42 is assigned the
variable num */
 Initialization :

int num = 42; /* num is assigned a value


upon declaration */
 Input from the user :
int num;
scanf("%d", &num); /*The library function scanf
gets input from the
user and
stores it in the variable num */
PRINTF()
 The library function printf() is used for printing to the
standard output (usually the screen).
 It receives a control string and a list of arguments (optional).
 Example:

int i_num = 42;


float f_num = 2.3;
printf ("The numbers are %d %f \n",i_num, f_num);

Control string
The control string is printed, replacing Argument List
the conversion specifications
(%d and %f) with the corresponding values in the argument list (i_num
and f_num).
PRINTF() - CONTROL SYMBOLS
 \n - Line feed (new line)

 \t - Tabulator

 \b - Moves the cursor 1 position left

 \" - Double quote

 \\ - Backslash

 Remark: The percent sign % is printed by typing %% (unique to


printf())
PRINTF() – CONT’D
 Conversion specification syntax for printf() is:

%[-m.n]t
- Left adjustment
m Minimal field length
n Number of digits after decimal point
t Data type symbol (d, f etc.)

 Examples:

%4d %6.2f %-6.2f


42 3 . 1 4 3 . 1 4
SCANF()
 The library function scanf() is used for receiving data from
the user through the standard input (usually the keyboard).

 It receives a control string and a list of arguments.


 Example :
int i_num;
float f_num;
scanf ("%d%f", &i_num, &f_num);

 Note that the addresses of the variables are passed.


 Besides formats, the control string of scanf() may include
spaces between the formats (for readability), but nothing else!
TYPE CONVERSION
 Two types of conversions:

 Implicit Conversion:
Invoked automatically by the compiler when expressions mix
different types.
 Explicit Conversion (Casting):
Invoked by the programmer.
 Conversion can result in:
 Promotion: The type is converted to a “higher” type.
 Demotion: The type is converted to a “lower” type,
which may lead to trouble.
MIXED TYPE OPERATION
 Example :

double d = 12.5;
int a, b = 4;
a = b + d;

 Mixed type operation

 Promotion to make addition possible


 b int  double
 Addition of double types
 b + d
 Demotion upon assignment
 b + d double  int (CAUTION !)
EXAMPLE: CONVER.C
/* conver.c
This program illustrates type conversion (casting).
There are some implicit (automatic)
casts and some explicit casts.
Can you predict the output of the program ? */

#include <stdio.h>

void main(void)
{
int sum1, sum2, sum3;

sum1 = 2.5 + 3.7; /* what casts took place here? */


sum2 = (int)2.5 + (int)3.7; /* and here ? */
sum3 = 2.5 + (int)3.7; /* and here ? */

printf("\nsum1 = %d\nsum2 = %d\nsum3 = %d\n",


sum1,sum2,sum3);
}
EXAMPLE: OVERFLOW1.C
/* This program illustrates an overflow. A variable of type char
is assigned values that are out of its range(too large/too
small) */
#include <stdio.h>
void main(void)
{
short short_num;
char ch = 0;
printf ("Enter a number which create an overflow ->");
scanf("%hd", &short_num); /* if the input is out of range of
short,there will be an overflow */

printf("%hd\n", short_num);

ch = -140; /* overflow (some may say 'underflow') */


printf("%d\n", ch);/* not –140, because of overflow */
ch = 200; /* overflow */
printf("%d\n", ch);/* not 200,because of overflow */
}
SUMMARY
 The basic memory unit is a bit. 8 consecutive bits are the
smallest addressable memory unit – a byte.
 A variable is a memory location for storing data.
It is referred to by a symbolic name.
 Variables used in a program must be declared before use.
The declaration tells the compiler how many bytes to
allocate for the variable, and the name that we choose for
it.
SUMMARY – CONT’D
 There are two basic variable types:
 Integral types : char, short, int, long.
 Floating Point types: float and double.

 The integral type variable may be :


 Signed - represented in 2’s complement binary.
 Unsigned - represented in normal binary.

 A variable may be declared as const.

 Variables can be given values


 By assignment.
 Within definition (initialization).
 In executable statement.
 By input from the user.
SUMMARY – CONT’D
 printf() and scanf() are library functions for
interactive communication between the user and the program
(I/O). They take parameters : a control string and a list of
arguments.

 Type Conversion
 Implicit - done automatically by the computer.
 Explicit - upon demand by the user.

Can result in :
 Promotion - conversion a higher type.
 Demotion - conversion a lower type.

 Integer Overflow may be very dangerous. It can yield wrong


results.
SUMMARY – CONT’D
 Size & range of data types
Size Highest value
Type Lowest value
(bits)
Unsigned char 8 0 255

Char 8 -128 127

Unsigned short 16 0 65535

Short 16 -32768 32767

Unsigned long 32 0 4294967295

Long 32 -2147483648 2147483647

Float (absolute value) 32 3.4E-38 3.4E+38


Double (absolute value) 64 1.7E-308 1.7E+308

 int is like long or like short, depending on the compiler.


ASCII CODE TABLE
ASCI Char ASCI Char ASCI Char ASCI Char
I I I I
000 nul 016 Dl 032 sp 048 0
001 soh 017 dc1 033 ! 049 1
002 stx 018 Dc2 034 " 050 2
003 etx 019 dc3 035 # 051 3
004 eot 020 dc4 036 $ 052 4
005 enq 021 Nak 037 % 053 5
006 ack 022 syn 038 & 054 6
007 bel 023 etb 039 ' 055 7
008 bs 024 can 040 ( 056 8
009 ht 025 em 041 ) 057 9
010 nl 026 sub 042 * 058 :
011 Vt 027 esc 043 + 059 ;
012 np 028 fs 044 , 060 <
013 Cr 029 gs 045 - 061 =
014 so 030 rs 046 . 062 >
015 si 031 Us 047 / 063 ?
ASCII CODE TABLE – CONT’D
ASCII Char ASCII Char ASCII Char ASCII Char

064 @ 080 P 096 ` 112 p

065 A 081 Q 097 a 113 q

066 B 082 R 098 b 114 r

067 C 083 S 099 c 115 s

068 D 084 T 100 d 116 t

069 E 085 U 101 e 117 u

070 F 086 V 102 f 118 v

071 G 087 W 103 g 119 w

072 H 088 X 104 h 120 x

073 I 089 Y 105 i 121 y

074 J 090 Z 106 j 122 z

075 K 091 [ 107 k 123 {

076 L 092 \ 108 l 124 |

077 M 093 ] 109 m 125 }

078 N 094 ^ 110 n 126 ~

079 O 095 _ 111 o 127 del

You might also like