Unit 8 C Variables
Unit 8 C Variables
Introduction
ME 325
Spring, 2007
Overview
• Control and escape Codes
• Number System
1001=1(23)+0(22)+0(21)+1(20)=8+0+0+1
=9
Decimal System
• Base (radix) 10
• The numeric system that we have
always used.
Hexadecimal System
• Base (radix) 16 (multiple of 4 and 8 Nibble
and byte)
• While the decimal system uses 10 digits
(they are all numeric), the hexadecimal
system uses sixteen (16) symbols to
represent a number.
• After counting from 0 to 9, the system uses
letters until it gets 16 different values. The
letters used are a, b, c, d, e, and f, or their
uppercase equivalents.
• The hexadecimal system counts as follows:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f
– f=15, then the next number would be 10=16,
A Byte
• A byte is a group or eight consecutive
bits. The bits are counted from right to
left starting at 0. The most right bit is
bit 0;
• Each four bits are called Nibble.
(hexadecimal)
C Numeric Types
Variable Type Keyword Range Storag
e Bytes
Character char -127 to 127 1
Unsigned unsigned char 0 to 255 1
Character
Integer int -32,768 to 32,767 2
Unsigned Integer unsigned int 0 to 65,535 2
Short Integer short -32,768 to 32,767 2
Unsigned Short unsigned short 0 to 65,535 2
Integer
Long Integer long -2,147,483,648 to 4
2,147,483,647
Unsigned Long unsigned long 0 to 4,294,967,2954 4
Integer
Single precision float 1.2E-38 to 3.4E38, approx. 4
floating point range precision = 7 digits.
Double precision double 2.2E-308 to 1.8E308, approx. 8
floating point range precision = 19 digits
Variable Declaration
Examples
• The main variable types that you will use
most are char, int, float.
• Single declarations
– int age;
float amountOfMoney;
char initial;
• Multiple declarations
– You can repeat the declaration with a different
variable on each line or same line separated by a
coma
int age;
int houseNumber;
Or
int age, houseNumber, quantity;
float distance, rateOfDiscount;
char firstInitial, secondInitial;
Variable Scope
• The scope of a variable is
– The area of the program where that
variable is valid, i.e. the parts of the
program that have access to that variable.
This is determined by the location of the
declaration.
num++
num--