Chapter 2 Simple Data Types By C. Shing ITEC Dept Radford University.

Slides:



Advertisements
Similar presentations
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Advertisements

Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
Numeric Types, Expressions, and Output ROBERT REAVES.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
C/C++ Operators Binary Operators: Operators Between Two Operands: Operator + MeaningExample Definition. Additionx = 6 + 2;Add the values on either side.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Numeric types of C Integers – Signed and unsigned – In a few different sizes – 209 Floating point – In different sizes –
Chapter 3 Flow Control By C. Shing ITEC Dept Radford University.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Variables Symbol representing a place to store information
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 9 - Formatted Input/Output
Chapter Topics The Basics of a C++ Program Data Types
Chap. 2. Types, Operators, and Expressions
Input/output.
Basic Elements of C++.
TMF1414 Introduction to Programming
Basic Elements of C++ Chapter 2.
Input and Output Lecture 4.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
CS111 Computer Programming
Chapter 9 - Formatted Input/Output
Conversion Check your class notes and given examples at class.
Conversion Check your class notes and given examples at class.
By C. Shing ITEC Dept Radford University
Introduction to Java Applications
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
DATA TYPES There are four basic data types associated with variables:
Presentation transcript:

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 Types TypeSize (bit)Range unsigned char80~255 char, signed char8-128~127 unsigned int(16 or ) 320~2^32 -1 int, signed int(16 or) 32-2^31 ~2,147,483,647 unsigned short int160~65535 short int, signed short int ~32767

Slide 6 Simple Data Types TypeSize (bit)Range float320.xxxxxxE-38 ~ 0.xxxxxxE+38,6 significant digits double64E-308~E+308,15 significant digits long int(32 or)64-2^63~ 2^63-1 long long int64Same as above (by C99) unsigned long int(32 or)640~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: L  float: 0.123F  double:  Any number with decimal point is treated as double, e.g  Number in scientific notation, e.g e-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

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 Priority OperatorSame 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 operatorRight 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-c b * ++ d a/b/c a/b % c 'A'+1 ('A'+1) < b a>b && c<d a< ! b || ! ! ‘A’ a + b < !c + c a - d || b*c && b/a

Slide 20 Operators Exercises: Answer Given int a=1,b=2,c=3,d=4; Find the values of the following table: Expression Value ________ _____ a*b/c 0 a*b%c a*b-c b * ++ d 17 a/b/c0 a/b % c0 'A'+1 66 ('A'+1) < b0 a>b && c<d0 a< ! b || ! ! ‘A’1 a + b < !c + c0 a - d || b*c && b/a1

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 CodeExplain %d, %iSigned decimal %uUnsigned decimal %fFloating point %e, %EScientific notation (lower/upper case e) %g, %GShorter of either %f or %e (%f or %E for exponent < -4) %sstring

Slide 23 Format Output – printf (Cont.) Format CodeExplain %x, %Xunsigned hexadecimal (lower/upper case output) %oUnsigned octal %ccharacter %ppointer %Print a % sign

Slide 24 Format Output – printf (Cont.) Format CodeExplain %nNumber of characters up to %n, argument must be a pointer to an integer %lfUsed for a double %a, %AHexadecimal 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); // 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); // 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= ; 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= ; Show what to be printed? statement print out ________ _______ printf("%d",i) 123 printf("%05d",i) printf("%7o",i) 0173 printf("%-9X",i) 0X7B printf("%-#9x",i) 0x7b printf("%10.5f",x) printf("%-12.5e",x) e-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 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 ( ) from/to file: a.out output.txt inputdata.txt output.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, 4 th ed, Osborne Publishing Deitel & Deitel: C How to Program, 4th ed., Chapter 2 & 9, Prentice Hall