05 es26 lab - data types
TRANSCRIPT
-
8/14/2019 05 ES26 Lab - Data Types
1/52
Introduction to Programming
Basic Data TypesBasic Data Types
55
-
8/14/2019 05 ES26 Lab - Data Types
2/52
Introduction to Programming
Fundamental Data TypesFundamental Data Types
DATA TYPE SUGGESTED USAGE
char Text characters such as 'a','b','@' and so on
int Integral numbers such as 1, 2, 3, and so on
float Low/medium precision real numbers
double Medium/high precision real numbers
Adata type defines a set of values and aset of operations that can be performed onthose values.
-
8/14/2019 05 ES26 Lab - Data Types
3/52
Introduction to Programming
Fundamental Data TypesFundamental Data Types
Their respective sizes are not strictlydefined by the standard.
The compiler vendor selects the mostappropriate size that suits thearchitecture of the host computer.
Hence, compiler- and machine-dependent
-
8/14/2019 05 ES26 Lab - Data Types
4/52
Introduction to Programming
Fundamental Data TypesFundamental Data Types
Both char and int data types store integralvalues.
Get actual size in bytes of any data type byusing the unary operator (not a function)sizeof.
...
char ch;
printf( %u, %u, sizeof ch, sizeof(float) );
...
Unsigned intparenthesized
-
8/14/2019 05 ES26 Lab - Data Types
5/52
Introduction to Programming
Fundamental Data TypesFundamental Data Types
DATA TYPE VALUE TYPICAL SIZE
char Character 8 bits
int Integer 16 bits
float Real number 32 bits
double Real number 64 bits
-
8/14/2019 05 ES26 Lab - Data Types
6/52
Introduction to Programming
The Integer Data TypeThe Integer Data Type Keyword: int
Represents whole numbers in C
Its values typically range from 32768to 32767 (16 bits)
Here are some example of integers:
13, -214, +2001, 2002
An int cannot contain commas. Forexample, 2,001 is not a valid integervalue
-
8/14/2019 05 ES26 Lab - Data Types
7/52
-
8/14/2019 05 ES26 Lab - Data Types
8/52
Introduction to Programming
The Integer Data TypeThe Integer Data Type
signed (default) 16-bit int
0000 0000 0000 0000 = 0
1000 0000 0000 0000 = -32 768
-32 768 to +32 767
Sign bit (1 for negative) 15 bits
-
8/14/2019 05 ES26 Lab - Data Types
9/52
Introduction to Programming
The Integer Data TypeThe Integer Data Type
unsigned 16-bit int
0 to +65 535
Whole 16 bits
-
8/14/2019 05 ES26 Lab - Data Types
10/52
Introduction to Programming
The Integer Data TypeThe Integer Data Type
Care must be taken with signedquantities to ensure that any
arithmetic operations such as additionor subtraction do not generate anover- or underflow
Incrementing 32 767 will result to achange of sign when using signed 16-bit int
-
8/14/2019 05 ES26 Lab - Data Types
11/52
Introduction to Programming
Type Modifiers for IntegralType Modifiers for Integral
QuantitiesQuantities short int
-
8/14/2019 05 ES26 Lab - Data Types
12/52
Introduction to Programming
Type Modifiers for IntegralType Modifiers for Integral
QuantitiesQuantities The following are the '%' format
indicators recognized by scanf()
'%' formatter Appropriate
data type(s)
Input recognized in following
format
%d, %i, %u, %x, %o int Decimal, decimal, unsigned
decimal, hexadecimal, octal,
respectively
%ld, %li, %lu, %lx, %lo long int -same as above-
%hd, %hi, %hu, %hx, %ho short int -same as above-
-
8/14/2019 05 ES26 Lab - Data Types
13/52
Introduction to Programming
Real Number Data TypeReal Number Data Type
Both float and double data typesrepresent a real number in C
A float consists of an optional sign (+ or-), followed by one or more digits, adecimal point and one or more furthernumbers.
A double is a special float, which canstore more significant digits.
-
8/14/2019 05 ES26 Lab - Data Types
14/52
Introduction to Programming
Real Number Data TypeReal Number Data Type
Represented as floating-point numbers.
We often use scientific notation to
represent real numbers. In Math, 1.23 x 105 = 123000.0
In C, 1.23e5 = 123000.0
-
8/14/2019 05 ES26 Lab - Data Types
15/52
Introduction to Programming
Real Number Data TypeReal Number Data Type
Here are some invalid examples of realnumber in C.
0.1234e /* missing exponent */
15e-0.3 /* invalid exponent */
34,500.99 /* comma not allowed */
150 /* no decimal point */
-
8/14/2019 05 ES26 Lab - Data Types
16/52
Introduction to Programming
Real Number Data TypeReal Number Data Type
MantissaExponent
Typical representation of a 32-bit float
Sign bit
31 22 0Bit number
-
8/14/2019 05 ES26 Lab - Data Types
17/52
Introduction to Programming
Real Number Data TypeReal Number Data Type
MantissaExponent
Typical representation of a 64-bitdouble
Sign bit
63 51 0Bit number
-
8/14/2019 05 ES26 Lab - Data Types
18/52
Introduction to Programming
Type Modifier for Real Number Data TypesType Modifier for Real Number Data Types
float
-
8/14/2019 05 ES26 Lab - Data Types
19/52
Introduction to Programming
Format IndicatorsFormat Indicators
For the printf() function
'%' formatter Appropriatedata type(s)
Output format
%f Float or double Floating point format
%e Float or double Scientific format
%g Float or double Shortest of '%f' and '%e'
%Lf, %Le, %Lg Long double Floating point, scientific, and
shortest of '%f' and '%e',
respectively
-
8/14/2019 05 ES26 Lab - Data Types
20/52
Introduction to Programming
Format IndicatorsFormat Indicators
For the scanf() function
Format indicator Input format
%f, %lf, %Lf Floating point or scientific format for
a float, double or long double,
respectively
%e, %le, %Le -same as above-%g, %lg, %Lg -same as above-
-
8/14/2019 05 ES26 Lab - Data Types
21/52
Introduction to Programming
The Character Data TypeThe Character Data Type
Keyword: char
A char is a letter, digit or symbol
enclosed in single quotes. Here are some examples of characters:
c S 2 $
Note that the space ( ) is a characterjust like any letter or digit
-
8/14/2019 05 ES26 Lab - Data Types
22/52
Introduction to Programming
The Character Data TypeThe Character Data Type
Some non-printing and hard-to-printcharacters require an escape sequence
They consist of a backslash ( \ )followed by a character.
For example, a horizontal tab is
represented as \t when used in a Cprogram.
-
8/14/2019 05 ES26 Lab - Data Types
23/52
Introduction to Programming
The Character Data TypeThe Character Data Type
Name of Character Escape
Sequence
Alert \a
Backslash \\
Backspace \b
Carriage return \r
Double quote \
Some Non-printing characters
-
8/14/2019 05 ES26 Lab - Data Types
24/52
Introduction to Programming
Data TypesData Types
More on Data Types next time :)
-
8/14/2019 05 ES26 Lab - Data Types
25/52
-
8/14/2019 05 ES26 Lab - Data Types
26/52
Introduction to Programming
Variable Declaration andVariable Declaration and
DefinitionDefinition Declaration is used to name an object
like a variable
Definition is used to create objectsand reserved memory space.
Syntax:
-
8/14/2019 05 ES26 Lab - Data Types
27/52
Introduction to Programming
Variable DeclarationVariable Declaration
C allows multiple variables of the sametype to be defined in one statement
Example: int height, width;
char choice, index;Variables can be distributed among
declarations in any fashion.
Example:char choice;char index;
int width;
int height;
-
8/14/2019 05 ES26 Lab - Data Types
28/52
Introduction to Programming
Variable InitializationVariable Initialization
A variable may be initialized in itsdeclaration
Example: int height = 10, weight = 20;
char choice = Y, index = a;
the expression on the right serves as aninitializer
-
8/14/2019 05 ES26 Lab - Data Types
29/52
Introduction to Programming
ConstantsConstants Constants are data values that cannot be
changed during the execution of aprogram
Aliteral is an unnamed constant used tospecify data
Example: c /* a character literal */
5 /* an integer literal */ 3.1416 /* a float literal */
Hello /* a string literal */
-
8/14/2019 05 ES26 Lab - Data Types
30/52
Introduction to Programming
Explicit ConstantsExplicit Constants
An explicit constant is introduced in muchthe same way as a variable, but with its
definition prefixed by the word (or typequalifier) const.
They should be initialized during definition.
...
const float pi = 3.14159;
...
-
8/14/2019 05 ES26 Lab - Data Types
31/52
Introduction to Programming
Preprocessor ConstantsPreprocessor Constants
Another way to designate a constant isto use the preprocessor commanddefine
Syntax: #define
A#define statement allows you togive names to constants
A#define statement allows you togive names to constants
-
8/14/2019 05 ES26 Lab - Data Types
32/52
Introduction to Programming
Named ConstantsNamed Constants
Example:
#define PI 3.1416
#define EXCHANGE_RATE 52.50
The action of #define is just like thesearch and replace command found in
your text editor
No semicolon
-
8/14/2019 05 ES26 Lab - Data Types
33/52
Introduction to Programming
Input/Output FunctionsInput/Output Functions printf() function - general purpose
output routine
Format: printf(, item1,
item2); In its simplest form, it can display a string of
characters enclosed in double-quote marks onthe screen, as in:
printf("You made an error - try again!\n");
-
8/14/2019 05 ES26 Lab - Data Types
34/52
Introduction to Programming
Input/Output FunctionsInput/Output Functions
Controlling the Format
%d - decimal integer.
%f - float.
%lf - double
%e - float in 'exponent' form, eg 1.23E+02 ( E+02 means ten to power +2.)
%c - single character.
%s - character string.
-
8/14/2019 05 ES26 Lab - Data Types
35/52
Introduction to Programming
Input/Output FunctionsInput/Output Functions
scanf() general-purpose outputroutine
Format:
scanf(, addr1, addr2);
-
8/14/2019 05 ES26 Lab - Data Types
36/52
Introduction to Programming
Arithmetic OperatorsArithmetic Operators
C supports the arithmetic operators:
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo
-
8/14/2019 05 ES26 Lab - Data Types
37/52
Introduction to Programming
Arithmetic OperatorsArithmetic Operators
Examples:
A = 2 + 3;
B = 2 3;
C = 2* 3;
D = 4 / 2;
E = 5 % 2;
-
8/14/2019 05 ES26 Lab - Data Types
38/52
Introduction to Programming
Arithmetic OperatorsArithmetic Operators
The modulo (%) operator computesthe remainder of an integer division
For example, 5 % 2 is 1 because 5divided by 2 is 2 remainder 1.
Moreover, 11 % 2 is 1 because 11
divided by 2 is 5 remainder 1.
-
8/14/2019 05 ES26 Lab - Data Types
39/52
Introduction to Programming
Arithmetic OperatorsArithmetic Operators
Floating Point Arithmetic
C requires that in a quotient A/B, the
denominator, B, must be nonzero
Division by zero will cause a programcrash that is why it should be avoided
-
8/14/2019 05 ES26 Lab - Data Types
40/52
Introduction to Programming
Arithmetic OperatorsArithmetic Operators
Integer Arithmetic
The quotient of 2 integers is not
necessarily an integer.
For example, the quotient 3/4 equals afloating point number 0.75, which is not
an integer.
-
8/14/2019 05 ES26 Lab - Data Types
41/52
Introduction to Programming
Arithmetic OperatorsArithmetic Operators
Integer Arithmetic
If you divide an int by an int, the value
you get will be truncated or roundeddown to the largest whole number lessthan the result
For example, 10/3 = 3 and 3/4 = 0
-
8/14/2019 05 ES26 Lab - Data Types
42/52
Introduction to Programming
Unary Plus and UnaryUnary Plus and Unary
MinusMinus The unary plus (+) operator causes no
change to the quantity that follows
where the unary minus (-) operatorcauses the sign of the followingquantity to be changed.
Examples: +5 -5
-
8/14/2019 05 ES26 Lab - Data Types
43/52
Introduction to Programming
Assignment OperatorAssignment Operator
A statement that assigns a value to avariable is called an assignment
statement and has the format:variable = expression
Example: y = 21;
Ch = A;x = (2 + 5) * (10 6);
net = gross expenses;
-
8/14/2019 05 ES26 Lab - Data Types
44/52
Introduction to Programming
Assignment OperatorAssignment Operator
Expression Equivalent
k = k + 2; k += 2;
k = k - 2; k -= 2;
k = k * 2; k *= 2;
k = k / 2; k /= 2;
-
8/14/2019 05 ES26 Lab - Data Types
45/52
Introduction to Programming
Assignment OperatorAssignment Operator
The semantics is specified by
variable op= expression
is equivalent to
variable = variable op expression
The left side of the equal sign should bea variable, not an expression
Thus, the statementx + 2 = 0;
is not a valid statement
-
8/14/2019 05 ES26 Lab - Data Types
46/52
Introduction to Programming
Precedence and AssociativityPrecedence and Associativity
Precedence is used to determine theorder in which different operators in a
complex expression are evaluatedAssociativity is used to determine the
order in which operators with the same
precedenceare evaluated in a complexexpression
-
8/14/2019 05 ES26 Lab - Data Types
47/52
Introduction to Programming
Precedence and AssociativityPrecedence and Associativity
Operators Associativity
-- ++ - (unary) Left to right
* / % Left to right
+ - Left to right
= += -= *= /= Right to left
-
8/14/2019 05 ES26 Lab - Data Types
48/52
Introduction to Programming
Precedence and AssociativityPrecedence and Associativity
Example: The expression 1 + 2 *3 isequivalent to 1 + (2 * 3)
Expressions inside parentheses are
evaluated first Example: The expression 1 + 2 3 + 45 is equal to 1 based on the left-to-
right rule of associativity Thisexpression is equal to
(((1 + 2) 3) + 4) 5
-
8/14/2019 05 ES26 Lab - Data Types
49/52
Introduction to Programming
Precedence and AssociativityPrecedence and Associativity
The unary operators have higherprecedence than the binary plus. And
minus In the expression a * b c, the firstminus is unary and the second is binary.
Using the rules of precedence, we seethat the expression is equivalent to
((-a) * b) c.
-
8/14/2019 05 ES26 Lab - Data Types
50/52
Introduction to Programming
Type ConversionType Conversion
Implicit type conversion happenswhen C automatically converts a type
from one format to another. For example, the expression in the
statement x = 2 + 3.5 will evaluate to
in integer value if x is an integervariable.
-
8/14/2019 05 ES26 Lab - Data Types
51/52
Introduction to Programming
Type ConversionType Conversion
We can actually force the conversionprocess to take place
This is called explicit typeconversion and is done with a unaryoperator called a type cast.
Format: (data type) expression
-
8/14/2019 05 ES26 Lab - Data Types
52/52
Introduction to Programming
Type ConversionType Conversion
The type cast causes the value of theexpression to be converted to the data
type. For example,(float) (10 + 2)
evaluates to a floating point number,
12.0