Basic Data Types 5

Introduction to Programming

Fundamental Data Types
• A data type defines a set of values and a set of operations that can be performed on those values.
DATA TYPE char int float double SUGGESTED USAGE Text characters such as 'a','b','@' and so on Integral numbers such as 1, 2, 3, and so on Low/medium precision real numbers Medium/high precision real numbers
Introduction to Programming

Fundamental Data Types
• Their respective sizes are not strictly defined by the standard. • The compiler vendor selects the most appropriate size that suits the architecture of the host computer. • Hence, compiler- and machinedependent
Introduction to Programming

Fundamental Data Types
• Both char and int data types store integral values. • Get actual size in bytes of any data type by using the unary operator (not a function) sizeof .
... char ch; printf( “%u, %u”, sizeof ch, sizeof(float) ); ...
Introduction to Programming

Unsigned int

parenthesized

Fundamental Data Types
DATA TYPE char int float double VALUE Character Integer Real number Real number TYPICAL SIZE 8 bits 16 bits 32 bits 64 bits

Introduction to Programming

The Integer Data Type
• Keyword: int • Represents whole numbers in C • Its values typically range from –32768 to 32767 (16 bits) • Here are some example of integers: 13, -214, +2001, 2002 • An int cannot contain commas. For example, 2,001 is not a valid integer value
Introduction to Programming

Type Modifiers
• Modifiers such as signed, unsigned, long and short are used to alter the meaning of data types to fit the needs of various situations more precisely

Introduction to Programming

The Integer Data Type
• signed (default) 16-bit int • 0000 0000 0000 0000 = 0 • 1000 0000 0000 0000 = -32 768
Sign bit (1 for negative) 15 bits

-32 768 to +32 767

Introduction to Programming

The Integer Data Type
• unsigned 16-bit int
Whole 16 bits

0 to +65 535

Introduction to Programming

The Integer Data Type
• Care must be taken with signed quantities to ensure that any arithmetic operations such as addition or subtraction do not generate an over- or underflow • Incrementing 32 767 will result to a change of sign when using signed 16bit int
Introduction to Programming

Type Modifiers for Integral Quantities
• short int <= int <= long int • The following are the '%' format indicators recognized by printf()
'%' formatter %d %x %o %u Data type(s) char, short, int char, short, int char, short, int char, short, int Output displayed in following format Decimal Hexadecimal Octal Unsigned decimal Decimal, Hexadecimal, octal or unsigned decimal, respectively

%ld, %lx, %lo, %lu long int

Introduction to Programming

Type Modifiers for Integral Quantities
• The following are the '%' format indicators recognized by scanf()
'%' formatter %d, %i, %u, %x, %o Appropriate data type(s) int Input recognized in following format Decimal, decimal, unsigned decimal, hexadecimal, octal, respectively -same as above-same as above-

%ld, %li, %lu, %lx, %lo

long int

%hd, %hi, %hu, %hx, %ho short int

Introduction to Programming

Real Number Data Type
• Both float and double data types represent a real number in C • A float consists of an optional sign (+ or -), followed by one or more digits, a decimal point and one or more further numbers. • A double is a special float, which can store more significant digits.
Introduction to Programming

Real 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

Introduction to Programming

Real Number Data Type
• Here are some invalid examples of real number in C. • • • • 0.1234e /* missing exponent */ 15e-0.3 /* invalid exponent */ 34,500.99 /* comma not allowed */ 150 /* no decimal point */
Introduction to Programming

Real Number Data Type
• Typical representation of a 32-bit float
Sign bit

Exponent
Bit number 31 22

Mantissa
0

Introduction to Programming

Real Number Data Type
• Typical representation of a 64-bit double
Sign bit

Exponent
Bit number 63 51

Mantissa
0

Introduction to Programming

Type Modifier for Real Number Data Types
• float <= double <= long double • Typical size and range of real data types for 32-bit CPUs:
Data type float double long double Size 32 bits 64 bits 80 bits Typical range of values ±3.4e+38 to ±1.1e-38 OR 1.17549e-38 to 3.40282e+38 ±1.7e+308 to ±2.2e-308 OR 2.22507e-308 to 1.79769e+308 ±1.1e+4932 to ±3.3e-4932 OR 3.3621e-4932 to 1.18973e+4932

Introduction to Programming

Format Indicators
• For the printf() function
'%' formatter %f %e %g %Lf, %Le, %Lg Appropriate data type(s) Float or double Float or double Float or double Long double Output format Floating point format Scientific format Shortest of '%f' and '%e' Floating point, scientific, and shortest of '%f' and '%e', respectively

Introduction to Programming

Format Indicators
• For the scanf() function
Format indicator %f, %lf, %Lf Input format Floating point or scientific format for a float, double or long double, respectively -same as above-same as above-

%e, %le, %Le %g, %lg, %Lg

Introduction to Programming

The 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 character just like any letter or digit
Introduction to Programming

The Character Data Type
• Some non-printing and hard-to-print characters 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 C program.
Introduction to Programming

The Character Data Type
Some Non-printing characters
Name of Character Alert Backslash Backspace Carriage return Double quote
Introduction to Programming

Escape Sequence \a \\ \b \r \”

Data Types
• More on Data Types next time :)

Introduction to Programming

Variables
• Variables are the basic data objects manipulated in a program • All variables must be declared before they are used

Introduction to Programming

Variable Declaration and Definition
• Declaration is used to name an object like a variable • Definition is used to create objects and reserved memory space. • Syntax: <data type> <variable list>

Introduction to Programming

Variable Declaration
• C allows multiple variables of the same type 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; to Programming Introduction

Variable Initialization
• A variable may be initialized in its declaration • Example: int height = 10, weight = 20; char choice = ‘Y’, index = ‘a’; • the expression on the right serves as an initializer

Introduction to Programming

• Constants are data values that cannot be changed during the execution of a program • A literal is an unnamed constant used to specify data • Example: ‘c’ /* a character literal */ • 5 /* an integer literal */ • 3.1416 /* a float literal */ • “Hello” /* a string literal */
Introduction to Programming

Constants

Explicit Constants
• An explicit constant is introduced in much the same way as a variable, but with its definition prefixed by the word (or type qualifier) const. • They should be initialized during definition.
... const float pi = 3.14159; ...
Introduction to Programming

Preprocessor Constants
• Another way to designate a constant is to use the preprocessor command define • Syntax: #define <identifier> <value> • A #define statement allows you to give names to constants • A #define statement allows you to give names to constants
Introduction to Programming

Named Constants
• Example: #define PI 3.1416 #define EXCHANGE_RATE 52.50 • The action of #define is just like the search and replace command found in your text editor
No semicolon

Introduction to Programming

Input/Output Functions
• printf() function - general purpose output routine • Format: printf(<format string>, item1, item2…); • In its simplest form, it can display a string of characters enclosed in double-quote marks on the screen, as in: printf("You made an error - try again!\n");
Introduction to Programming

Input/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 • %s

-

single character. character string.

Introduction to Programming

Input/Output Functions
• scanf() – general-purpose output routine • Format: scanf(<format string>, addr1, addr2…);

Introduction to Programming

Arithmetic Operators
• C supports the arithmetic operators:
+ * / % Addition Subtraction Multiplication Division Modulo
Introduction to Programming

Arithmetic Operators
• Examples: A = 2 + 3; B = 2 – 3; C = 2* 3; D = 4 / 2; E = 5 % 2;
Introduction to Programming

Arithmetic Operators
• The modulo (%) operator computes the remainder of an integer division • For example, 5 % 2 is 1 because 5 divided by 2 is 2 remainder 1. • Moreover, 11 % 2 is 1 because 11 divided by 2 is 5 remainder 1.

Introduction to Programming

Arithmetic Operators
Floating Point Arithmetic • C requires that in a quotient A/B, the denominator, B, must be nonzero • Division by zero will cause a program crash that is why it should be avoided

Introduction to Programming

Arithmetic Operators
Integer Arithmetic • The quotient of 2 integers is not necessarily an integer. • For example, the quotient 3/4 equals a floating point number 0.75, which is not an integer.

Introduction to Programming

Arithmetic Operators
Integer Arithmetic • If you divide an int by an int, the value you get will be truncated or rounded down to the largest whole number less than the result • For example, • 10/3 = 3 and 3/4 = 0
Introduction to Programming

Unary Plus and Unary Minus
• The unary plus (+) operator causes no change to the quantity that follows • where the unary minus (-) operator causes the sign of the following quantity to be changed. • Examples: +5 -5

Introduction to Programming

Assignment Operator
• A statement that assigns a value to a variable is called an assignment statement and has the format: variable = expression • Example: y = 21; Ch = ‘A’; x = (2 + 5) * (10 – 6); net = gross – expenses;
Introduction to Programming

Assignment Operator
Expression k = k + 2; k = k - 2; k = k * 2; k = k / 2; Equivalent k += 2; k -= 2; k *= 2; k /= 2;
Introduction to Programming

Assignment Operator
• The semantics is specified by variable op= expression is equivalent to variable = variable op expression • The left side of the equal sign should be a variable, not an expression • Thus, the statement x + 2 = 0; • is not a valid statement
Introduction to Programming

Precedence and Associativity
• Precedence is used to determine the order in which different operators in a complex expression are evaluated • Associativity is used to determine the order in which operators with the same precedence are evaluated in a complex expression
Introduction to Programming

Precedence and Associativity
Operators -- ++ - (unary) * / + = += -= *= /= % Associativity Left to right Left to right Left to right Right to left

Introduction to Programming

Precedence and Associativity
• Example: The expression 1 + 2 *3 is equivalent to 1 + (2 * 3) • Expressions inside parentheses are evaluated first • Example: The expression 1 + 2 –3 + 4 –5 is equal to –1 based on the ”left-toright” rule of associativity This expression is equal to (((1 + 2) – 3) + 4) – 5
Introduction to Programming

Precedence and Associativity
• The unary operators have higher precedence than the binary plus. And minus • In the expression –a * b – c, the first minus is unary and the second is binary. • Using the rules of precedence, we see that the expression is equivalent to ((-a) * b) – c.
Introduction to Programming

Type Conversion
• Implicit type conversion happens when 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 integer variable.
Introduction to Programming

Type Conversion
• We can actually “force” the conversion process to take place • This is called explicit type conversion and is done with a unary operator called a type cast. • Format: (data type) expression
Introduction to Programming

Type Conversion
• The type cast causes the value of the expression to be converted to the data type. For example, (float) (10 + 2) evaluates to a floating point number, 12.0

Introduction to Programming