05 Es26 Lab - Data Types

  • Uploaded by: Wilmarc
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View 05 Es26 Lab - Data Types as PDF for free.

More details

  • Words: 2,162
  • Pages: 52
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

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 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 . parenthesized

Unsigned int

... char ch; printf( “%u, %u”, sizeof ch, sizeof(float) ); ... Introduction to Programming

Fundamental 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

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

Data type(s)

Output displayed in following format

%d

char, short, int

Decimal

%x

char, short, int

Hexadecimal

%o

char, short, int

Octal

%u

char, short, int

Unsigned decimal

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

Decimal, Hexadecimal, octal or unsigned decimal, respectively

Introduction to Programming

Type Modifiers for Integral Quantities • 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-

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

Mantissa

22

Introduction to Programming

0

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

Exponent Bit number

63

Mantissa

51

Introduction to Programming

0

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

Size

Typical range of values

float

32 bits

±3.4e+38 to ±1.1e-38 OR 1.17549e-38 to 3.40282e+38

double

64 bits

±1.7e+308 to ±2.2e-308 OR 2.22507e-308 to 1.79769e+308

long double

80 bits

±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

Appropriate data 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

Introduction to Programming

Format 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-

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

Escape Sequence \a

Backslash

\\

Backspace

\b

Carriage return

\r

Double quote

\” Introduction to Programming

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:

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; Introduction to Programming

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

• 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

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 • 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 No semicolon

• 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

Introduction to Programming

Input/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 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

• %c • %s

-

single character. character string.

Introduction to Programming

+2.)

Input/Output Functions • scanf() – general-purpose output routine • Format: scanf(, 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

Equivalent

k = k + 2;

k += 2;

k = k - 2;

k -= 2;

k = k * 2;

k *= 2;

k = 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

Associativity

-- ++ - (unary)

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

Related Documents


More Documents from "Wilmarc"

Mscsgradapp
May 2020 8
09 Es26 Lab - Loops 2
May 2020 14
11 Es26 Lab - Arrays
May 2020 14
08 Es26 Lab - Loops
May 2020 6
13 Es 26 - Strings
May 2020 4