Chapter 2 Simple Data Types
description
Transcript of Chapter 2 Simple Data Types
Chapter 2 Simple Data Types
By C. Shing
ITEC Dept
Radford University
Slide 2
Objectives Understand How to declare a variable Describe C simple data types Understand C expressions, operators
and their priority level Understand how to use format output (printf) Understand how to use keyboard input: scanf Understand how to design a small pogram
Slide 3
Variable Declaration Declaration form:
Type identifier [= initial value];
Note: In ANSC C, you must declare all variables
before using statements
Identifier: begins with a letter or underscore
Begins with underscore: for system Begins with letter: for user
The rest either letter, digit or underscore Up to 255 characters
Slide 4
Types Simple data type
Modifier: (signed, unsigned), (long, short) except on char char, int (default), float, double Sizes are system dependent
int (16 or 32 bots), float (32 or 64 bits) Use sizeof (type) operator to find the data type size sizeof (char)=1<=sizeof (short)<=sizeof (int)<=sizeof (long) sizeof (signed)=sizeof (unsigned)=sizeof (int) sizeof (float) <= sizeof (double)<=sizeof (long double)
Structured data type char[] (array, string), type * (pointer) struct (record)
Slide 5
Simple Data TypesType Size (bit) Range
unsigned char 8 0~255
char, signed char 8 -128~127
unsigned int (16 or ) 32 0~2^32 -1
int, signed int (16 or) 32 -2^31 ~2,147,483,647
unsigned short int 16 0~65535
short int, signed short int
16 -32768~32767
Slide 6
Simple Data TypesType Size (bit) Range
float 32 0.xxxxxxE-38 ~ 0.xxxxxxE+38,6 significant digits
double 64 E-308~E+308,15 significant digits
long int (32 or)64 -2^63~ 2^63-1
long long int 64 Same as above (by C99)
unsigned long int
(32 or)64 0~2^64-1
Slide 7
Initial Values char: ‘A’, ‘\a’ (or ‘\7’,’\07’,’\007’ alert), ‘\\’,
‘\b’ (backspace), ‘\r’ (carriage return),
’\”’, ‘\f’ (form feed), ‘\t’, ‘\n’, ‘\0’ (null), ‘\’’,
‘\v’ (vertical tab), ‘\?’ all characters are stored as ASCII integers No constants of character type in C
(However, yes in C++) int: with or without sign
Decimal: -3 Octal: 023 Hexadecimal: 0x123
Slide 8
Initial Values (Cont.)
long: 1234567L float: 0.123F double:
Any number with decimal point is treated as
double, e.g. 0.123 Number in scientific notation, e.g. -123.45e-
1
Slide 9
Expression Combination of variables, constants
and operators
Example: x-1 > 5 is an expression
Slide 10
Operators Increment and decrement: Prefix and postfix
++, -- Arithmetic
*, /, %, +, - Relational
==, !=, >, <, >=, <= (no space in between) Logic
! (not), && (and), || (or) Conditional
Expr1 ? expr2 : expr3 Comma (used in variable declaration)
Slide 11
Operators – Increment and Decrement
++ variable/ -- variable Increment(or decrement) variable by 1
before evaluating the current statement variable ++/ variable –
Increment(or decrement) variable by 1
after evaluating the current statement
Slide 12
Operators - Arithmetic / : quotient of 2 integers division
Example:
5/9=0
9/5=1
Note: in order to get the desired result of 5/9,
Use either one below:
5.0/9, 5/9.0. 5.0/9.0
Slide 13
Operators – Arithmetic (Cont.) %: remainder of 2 integers division
If negative integer is in exactly one of numerator
or denominator, then do division as in all positive
Numbers and just add a negative sign in front of
the result. For example: (-40)/11=- (40/11)=-3
Note: Do not use negative numbers in % since it is
implementation dependent.
Slide 14
Operators - Relational Do not compare float or double number
using == or !=
Example:
double x=5.0;
if (x == 5.0)
{
printf (“x=5.0\n”); // this line may not be
//printed due to round off error
}
Slide 15
Operators - Logic
Example:
int x=2;
printf (“%d\n”, 1<x<3); // 1 is printed
Slide 16
Operators PriorityOperator Same Priority Rule
(), postfix++/-- Left to right
Unary +/-, prefix++/--, ! Right to left
*, /, % Left to right
+, - Left to right
<, >, <=, >= Left to right
==, != Left to right
&& Left to right
|| Left to right
?: Right to left
Assignment operator Right to left
, Left to right
Slide 17
Example Given a=2, b=-3, c=5, d=-7, e=11(1) a/b/c=(2) 7+c* -- d/e = (3) 2* a%-b+c+1=(4) 7+ ++a%4=
Slide 18
Example Given a=2, b=-3, c=5, d=-7, e=11(1) a/b/c=(2/-3/5)=(-(2/3))/5=(-0)/5=-(0/5)=-0=0(2) 7+c* -- d/e = 7+5*(-8)/11=7+(-40)/11=7+(-(40/11))=7+(-3)=4(3) 2* a%-b+c+1=2*2%3+5+1=4%3+5+1=1+5+1=7(4) 7+ ++a%4=7+3%4=7+3=10
Slide 19
Operators Exercises:Given int a=1,b=2,c=3,d=4; Find the values of the following table: Expression Value ________ _____ a*b/c a*b%c+1 ++a*b-c7 - - b * ++ d a/b/ca/b % c'A'+1 ('A'+1) < ba>b && c<da< ! b || ! ! ‘A’a + b < !c + ca - d || b*c && b/a
Slide 20
Operators Exercises: AnswerGiven int a=1,b=2,c=3,d=4; Find the values of the following table: Expression Value ________ _____ a*b/c 0a*b%c+1 3++a*b-c 17 - - b * ++ d 17a/b/c 0a/b % c 0'A'+1 66('A'+1) < b 0a>b && c<d 0a< ! b || ! ! ‘A’ 1a + b < !c + c 0a - d || b*c && b/a 1
Slide 21
Format Output - printf Form:
printf(“%modifier format_code characters”,
variables);
Example:
printf(“%6d\t%d\n”,number1, number2);
Slide 22
Format Output – printf (Cont.)Format Code Explain
%d, %i Signed decimal
%u Unsigned decimal
%f Floating point
%e, %E Scientific notation (lower/upper case e)
%g, %G Shorter of either %f or %e (%f or %E for exponent < -4)
%s string
Slide 23
Format Output – printf (Cont.)Format Code Explain
%x, %X unsigned hexadecimal (lower/upper case output)
%o Unsigned octal
%c character
%p pointer
%% Print a % sign
Slide 24
Format Output – printf (Cont.)Format Code Explain
%n Number of characters up to %n, argument must be a pointer to an integer
%lf Used for a double
%a, %A Hexadecimal form 0xy.yyyyP+y (C99 only)
Slide 25
Format Output – printf (Cont.) Modifier:
printf(“%modifier format_code characters”, variables);
-: left justified +: display + for positive, - for negative value 0m: padded with leading 0s when less than m digits
printed n(integer): min number of spaces
Slide 26
Format Output – printf (Cont.) Modifier : (Cont.)
p.q:min: %f, %e, %E:
min total p spaces, q digits after decimal point %g, %G:
min total p spaces, q significant digits %s:
At least p total spaces, at most q spaces of the string %d:
At least p spaces, min q digits displayed; padded with 0 if not enough digits
#: hexadecimal with 0x prefix *.*: specify min spaces and # of digits after decimal
point
Slide 27
Format Output – printf (Cont.) Examples:
printf (“%s\n”, “How are you?”); printf (“Beep me!%c\n”,’\a’); printf (“A right-justified number: %6d”, 5); printf (“A left-justified number: %-6d”, 5); printf (“%6.2f\n”, 12.3F); printf (“%*.*f\n”, 6, 2, 12.3F); printf (“%6.2lf\n”, 1.236); printf (“%#x\n”, 123); printf(“%2.6d\n”, 123); printf(“%2.4s\n”, “Morning”);
Slide 28
Format Output – printf (Cont.) Examples:
printf (“%s\n”, “How are you?”); // How are you? Is printed printf (“Beep me!%c\n”,’\a’); printf (“A right-justified number: %6d”, 5); // 5 is printed printf (“A left-justified number: %-6d”, 5); // 5 is printed printf (“%6.2f\n”, 12.3F); // 12.30 is printed printf (“%*.*f\n”, 6, 2, 12.3F); // similar to above printf (“%6.2lf\n”, 1.236); // 1.24 is printed printf (“%#x\n”, 123); // 0x7b is printed printf(“%2.6d\n”, 123); //000123 is printed printf(“%2.4s\n”, “Morning”); //Morn is printed
Slide 29
Format Output – printf (Cont.) Examples:
Hello World Example
Slide 30
Format Output – printf (Cont.) Practice:Given int i=123; double x=0.123456789;Show what to be printed? statement print out ________ _______ printf("%d",i) printf("%05d",i) printf("%7o",i) printf("%-9X",i) printf("%-#9x",i) printf("%10.5f",x) printf("%-12.5e",x)
Slide 31
Format Output – printf (Answer) Practice:Given int i=123; double x=0.123456789;Show what to be printed? statement print out ________ _______ printf("%d",i) 123printf("%05d",i) 00123printf("%7o",i) 0173 printf("%-9X",i) 0X7B printf("%-#9x",i) 0x7b printf("%10.5f",x) 0.12346 printf("%-12.5e",x) 1.23457e-01
Slide 32
Keyboard Input Format Function - scanf Form: scanf(“format”, variable_address);
Format: similar to those used in printf
Example:char c;int i;double db;
scanf(“%c%d%lf”, &c, &i, &db); // sample data:a 100 -1.23 This is a sample data// c=‘a’, i=100, db=-1.23
Slide 33
Small Program Design
1. Comment: describe assumption,
given input data, and output result.
Write the algorithm how to achieve the output
using the input
2. Define variables (use meaningful names
and data types) that used to store input,
output and intermediate results
Slide 34
Small Program Design (Cont.)
3. Input data: either store initial values
in variables, get data from files or
use keyboard input
4. Process data: use expression to calculate
Results and store them in variables
5. Output result: display results in screen
or store them in files
Slide 35
Small Program Design (Cont.)Example:/* Program Purpose:
assumption:input:output:algorithm:Programmer’s Name:
*/
Slide 36
Small Program Design (Cont.)Example: (Cont.)int main(void) {
double celsius, fahrenheit=…; // input
celsius= …; // process
printf (“…..”); // output
return 0;}
Slide 37
Small Program Design (Cont.) Example:
Yard-Meter Conversion
Slide 38
Class Example Example 1 Example 2 Example 3 Example 4
Slide 39
Class Example (Cont.) Use scanf function to read data from keyboard:
Example 1 Use redirect input (<) /output (>) from/to file:
a.out < inputdata.txt > output.txtinputdata.txtoutput.txt
Use “for loop”:Example 1
Interchange DOS and Unix format: dos2unix, unix2dos
dos2unix dosformatfile.c unixformatfile.c
Slide 40
References Herbert Schildt: C: The Complete Reference,
4th ed, Osborne Publishing Deitel & Deitel: C How to Program, 4th ed.,
Chapter 2 & 9, Prentice Hall