ch02 Variables
ch02 Variables
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:
Scalar Aggregate
void
Types Types
Integral Floating
Types Types
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.
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.
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
\\ - Backslash
%[-m.n]t
- Left adjustment
m Minimal field length
n Number of digits after decimal point
t Data type symbol (d, f etc.)
Examples:
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;
#include <stdio.h>
void main(void)
{
int sum1, sum2, sum3;
printf("%hd\n", short_num);
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.