1 Chapter 6 The Fundamental Data Types. 2 Outline  Declarations and expressions  Fundamental data types  Characters and the data type char  The Data.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms The C Programming Language.
Advertisements

C Programming Lecture 7 Functions. Structured Programming b Keep the flow of control in a program as simple as possible. b Use top-down design. Keep decomposing.
1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
Structure of a C program
1 Review of Chapter 3--- Flow of Control  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 Review of Class on Oct Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The.
1 Structured Programming in C Welcome to CPSC 206.
1 Fundamental Data Types. 2 Declaration All variables must be declared before being used. –Tells the compiler to set aside an appropriate amount of space.
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Data types and variables
1 Chapter 4 (II) Functions and Structured Programming.
Chapter 2 Data Types, Declarations, and Displays
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23.
1 Review of Chapter 6: The Fundamental Data Types.
Data Types, Expressions and Functions (part I)
C Programming Lecture 12. The Compound Statement b A compound statement is a series of declarations and statements surrounded by braces. b A compound.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Fundamentals of C and C++ Programming Control Structures and Functions.
Conditional Statement
C Tokens Identifiers Keywords Constants Operators Special symbols.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
Data Type. Syntax Rules Recap keywords breakdoubleifsizeofvoid caseelseintstatic..... Identifiers not#me123th scanfprintf _idso_am_igedd007 Constant ‘a’‘+’
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
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 –
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
The Machine Model Memory
Chapter 4 C Program Control Part I
ECE Application Programming
Tokens in C Keywords Identifiers Constants
Fundamental Data Types
Chapter 7 Additional Control Structures
Lectures on Numerical Methods
Differences between Java and C
C Programming Getting started Variables Basic C operators Conditionals
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Fundamental Data Types
Department of Statistics St. Joseph’s College (Autonomous)
Data Type.
Flow of Control.
DATA TYPES There are four basic data types associated with variables:
C Language B. DHIVYA 17PCA140 II MCA.
Presentation transcript:

1 Chapter 6 The Fundamental Data Types

2 Outline  Declarations and expressions  Fundamental data types  Characters and the data type char  The Data type int and the integral types  The floating types  The sizeof Operator  Mathematical Functions  Conversions and Casts

3 Declarations and Expressions  Declarations  All variables must be declared before they can be used.  Why?

4 Declarations and Expressions  Appropriate amount of space in memory should be set aside to each variable to hold its value  Declaration of a variable tells the compiler its data type. Reason 1: Declarations enable the compiler to set aside an appropriate amount of space in memory to hold values associated with variables.

5 Declarations and Expressions Example: applying division on different data types  int a=5, b=2, c=a/b;  float a=5, b=2, c=a/b;  Machine instructions used are different  Different machine instructions are used when an operator applied on different data types Reason 2: Declarations enable the compile to instruct the machine to perform specified operations correctly. The programmer need not be concerned about the difference, but the C compiler has to recognize the difference and give the appropriate machine instructions

6 Declarations and Expressions  Declarations:  Summary All variables must be declared before they can be used. oTell the compiler to set aside an appropriate amount of space in memory to hold values associated with variables. oEnable the compile to instruct the machine to perform specified operations correctly.

7 Declarations and Expressions  Expressions  Meaningful combinations of constants, variables, and function calls.  Most expressions have both a value and a type

8 Declarations and Expressions  Examples of expressions Declarations: int a =12, b=10, c, d; Function min(int x, int y), returns the minimum value of x and y. Examples of expressions:  exp1, function call min(var1, var2):min(a,b)  exp2, var * constant:a*12  exp3, function call min(exp1, exp2):min(min(a,b), a*12)  exp4, exp3+exp1: min(min(a,b), a*12) + min(a,b)  exp5, var = exp3: c = min(min(a,b), a*12)  exp6, var = exp5:d = c = min(min(a,b), a*12)

9 Declarations and Expressions  Summary  Declarations: All variables must be declared before they can be used. oTell the compiler to set aside an appropriate amount of space in memory to hold values of variables. oEnable the compile to instruct the machine to perform specified operations correctly.  Expression Meaningful combination of constants, variables, and function calls. Most expressions have both a value and a type

10 Outline  Declarations and expressions  Fundamental data types  Characters and the data type char  The Data type int and the integral types  The floating types  The sizeof Operator  Mathematical Functions  Conversions and Casts

11 The Fundamental Data Types  Integral types  The types that can be used to hold integer values. Characters: char, signed char, unsigned char short, int, long, unsigned short, unsigned, unsigned long  Floating types  The types that can be used to hold real values. float, double, long double

12 Characters and the data Type char  Characters:  Each char is stored in one byte of memory  Variables of any integral type can be used to represent characters. char, int  When a variable is used to read in characters and a test must be must be made for EOF, The variable should be of type int, not char

13 Characters and the data Type char  Examples char c=‘a’;/* ‘a’ has ASCII encoding 97 */ int i=65;/* 65 is SCII encoding for ‘A’ */ printf(“%c”, c+1); /* b is printed */ printf(“%d”, c+2); /* 99 is printed */ printf(“%c”, i+3); /* D is printed */ Character codes for letters A through Z are contiguous Character codes for letters a through z are contiguous

14 Characters and the data Type char  char, signed char, and unsigned char  Each of the three char types is stored in one byes, which can hold 2 8 =256 distinct values. signed char: -128 to 127 unsigned char: 0 to 255  Typically, char is equivalent to either signed char or unsigned char, depending on the compiler.

15 Characters and the data Type char  Summary:  Each char is stored in one byte of memory  Variables of any integral type can be used to represent characters.  When a variable is used to read in characters and a test must be must be made for EOF, The variable should be of type int, not chars  Each of the three char types is stored in one byes, which can hold 256 distinct values. signed char:-128 to 127 unsigned char: 0 to 255 char is equivalent to either signed char or unsigned char, depending on the compiler.

16 The Fundamental Data Types  Integral types  The types that can be used to hold integer values. Characters: char, signed char, unsigned char short, int, long, unsigned short, unsigned, unsigned long  Floating types  The types that can be used to hold real values. float, double, long double

17 The Data Type int and the integral types  int  Typically, an int is stored in a machine word.  The length of a machine word is system-dependent Two bytes: 16 bits o-2 15, , ……, -3, -2, -1, 0, 1, 2, 3, ……, Four bytes: 32 bits o-2 31, , ……, -3, -2, -1, 0, 1, 2, 3, ……,  Integer overflow

18 The Fundamental Data Types  Integral types  The types that can be used to hold integer values. Characters: char, signed char, unsigned char short, int, long, unsigned short, unsigned, unsigned long  Floating types  The types that can be used to hold real values. float, double, long double

19 The Data Type int and the integral types  short, int, long  storage provided for each type: short <= int <= long  signed/unsigned  The integer values stored in an unsigned variable have no sign.  Example, If a variable of type unsigned int is stored in 4 bytes, orange: 0, 1, ……, oCompared to int: -2 31, ……, -2, -1, 0, 1, 2, ……,

20 The Fundamental Data Types  Integral types  The types that can be used to hold integer values. Characters: char, signed char, unsigned char short, int, long, unsigned short, unsigned, unsigned long  Floating types  The types that can be used to hold real values. float, double, long double

21 The Floating Types  Three floating types:  float, double and long double float: a floating constant with suffix F oExample: 3.7F double: no suffix, oExample: 3.7 long double: a floating constant with suffix L oExample:3.7L

22 The Floating Types  Notation for floating constants:  Decimal notation 12.0  Exponential notation: Examples: o1.2e5 = 1.2*10 5 o1.2e-5 = 1.2*10 -5

23 The Floating Types  rules of exponential notation:  No blank, no special characters (such as, ; )  Three parts: Integer Fraction Exponent must contain either a decimal point or an exponential part or both If a decimal point is present, either an integer part or fractional part or both must be present. Otherwise, there must be an integer part along with an exponential part. Example: 1.234e-5

24 The Floating Types  Format of exponential notation: (cont’d)  Example e-2F 0e0 1.  But not 3.1,1 31.e /* floating constant expression */  No blank, no special characters (such as, ; )  either a decimal point or an exponential part or both  If a decimal point is present, either an integer part or fractional part or both must be present.  Otherwise, there must be an integer part along with an exponential part.

25 The Floating Types  Three floating types: float, double and long double  storage : float <= double <= long double  Not all real numbers are representable. Typically, a float: to a double: to  Floating arithmetic operations, unlike the integer arithmetic operations, need not be exact.

26 The Floating Types  Summary:  Three floating types: float, double and long double storage : float <= double <= long double  Notation of floating constant: Decimal notation: Exponential notation: e5  Not all real numbers are representable.  Floating arithmetic operations, unlike the integer arithmetic operations, need not be exact.

27 Outline  Declarations  Fundamental data types  Characters and the data type char  The Data type int and the integral types  The floating types  The sizeof Operator  Mathematical Functions  Conversions and Casts

28 The sizeof Operator  Operator: sizeof(object)  Returns an integer that represents the number of bytes needed to store the object in memory.  An object can be a type o int ofloat, An expression oa+b, An array or structure type (introduced later)

29 The sizeof Operator  Why Operator: sizeof(object)?  The sizes of some fundamental types are machine dependent, such as int.  The sizeof operator provides precise information about the store requirements for the fundamental types on a given machine.

30 The sizeof Operator  Example #include int main(void) { printf("Size of char: %3d byte \n", sizeof(char)); printf("Size of int: %3d byte \n", sizeof(int)); printf("Size of float: %3d byte \n", sizeof(float)); printf("Size of double: %3d byte \n", sizeof(double)); } % a.out Size of char: 1 byte Size of int: 4 byte Size of float: 4 byte Size of double: 8 byte

31 The sizeof Operator  sizeof(….) is an operator, not a function  If sizeof is being applied to a type, parentheses are required Example: sizeof (int), but not sizeof int  Otherwise, parentheses are optional Example osizeof(a+b+7.7)  sizeof a+b+7.7

32 The sizeof Operator  Summary sizeof(object)  Returns an integer that represents the number of bytes needed to store the object in memory.  An object can be a type such as int or float, An expression such as a+b, An array or structure type (introduced later)  sizeof(….) is an operator, not a function If sizeof is being applied to a type, oParentheses are required Otherwise, parentheses are optional

33 Outline  Declarations  Fundamental data types  Characters and the data type char  The Data type int and the integral types  The floating types  The sizeof Operator  Mathematical Functions  Conversions and Casts

34 Mathematical Functions  C System  The C language  The preprocessor  The compiler  The library Contains many useful functions, such as file handles and mathematical functions.  Other tools, such as editors and debuggers

35 Mathematical Functions  There are no built-in mathematical functions in C language  Functions are available in the mathematics library.  Examples of mathematical functions: sqrt(), pow(), exp(), log(), sin(), cos(), tan()

36 Mathematical Functions  How to use the functions in the mathematics library?  Provide function prototypes in the code The function prototypes of mathematics functions are provided in file math.h #include  In traditional C systems, the mathematics library is often considered to be separate, The –lm option may be needed to compile gcc –lm f.c

37 Mathematical Functions  Example #include main(){ double x = 0.81; printf("%.2f\n", sqrt(x)); } % gcc -lm m.c % a.out 0.90 %

38 Mathematical Functions  Summary  There are no built-in mathematical functions in C language  Functions are available in the mathematics library.  How to use the functions in the mathematics library? In the code: #include Compile: gcc –lm f.c

39 Outline  Declarations  Fundamental data types  Characters and the data type char  The Data type int and the integral types  The floating types  The sizeof Operator  Mathematical Functions  Conversions and Casts

40 Conversions and Casts  An arithmetic expression has both a value and a type  Example: int x=1, y=2; The type of expression (x+y) is int

41 Conversions and Casts  Arithmetic conversions  Arithmetic conversions can occur when the operands of a binary operator are evaluated.  Example: int i, float f; expression i+f oi is promoted to a float oThe expression i+f as a whole has type float.

42 Conversions and Casts  Arithmetic conversions  Why type of an expression? #include main(){ int i=2; float f = 3.0; printf("%.2f\n", i+f); printf("%d\n", i+f); } % gcc con.c % a.out % The type of an expression  The rules for conversions when types of the constants, variables and function calls making up the expression have different types.

43 Conversions and Casts  The integral promotions  A char or short, either signed or unsigned, can be used in any expression where an int or unsigned int may be used.  If all the values of the original type can be represented by an int, the value is converted to an int;  Otherwise it is converted to an unsigned int. (unsigned) char, (unsigned) short  int, unsigned int

44 Conversions and Casts  The usual arithmetic conversions  If either operand is of type long double, the other operand is converted to long double.  Otherwise, if either operand is of type double, the other operand is converted to double.  Otherwise, if either operand is of type float, the other operand is converted to float. float  double  long double

45 Conversions and Casts  The usual arithmetic conversions (cont’d) Otherwise, all operands are of integral types  If either is of type unsigned long, the other  unsigned long.  Otherwise if either long, the other unsigned, If a long can represent all the values of an unsigned, then unsigned  long. Otherwise, both  unsigned long  Otherwise if either has type long, the other  long  Otherwise if either has type unsigned, the other  unsigned  Otherwise, both have type int short  int  unsigned  long  unsigned long

46 Conversions and Casts  Cast operator: explicit conversions  (type)  Example: int i (double) i Casts the value of i so the expression has type double The variable i itself remains unchanged.

47 Conversions and Casts  More Examples:  (long)(‘A’+1.0)  X=(float)((int)y+1)  (double)(x=7)  But not  (double) x=7

48 Conversions and Casts  Cast operator: unary operator  The same precedence as other unary operator  Right-to-left associativity  Example: (float) i + 3  ((float) i) +3

49 Conversions and Casts  Summary  An arithmetic expression has both a value and a type  Arithmetic conversions can occur when the operands of a binary operator are evaluated.  Cast operator: explicit conversions (type)

50 Outline  Declarations  Fundamental data types  Characters and the data type char  The Data type int and the integral types  The floating types  The sizeof Operator  Mathematical Functions  Conversions and Casts

51 End of Chapter 6: The Fundamental Data Type Read 6.1 – 6.13

52 Review of Chapter 3

53  How true and false are implemented in C  Representation of true and false false: represented by any zero value oint 0 ofloating 0.0 oNull character ‘\0’ oNull Pointer ( will be introduced in Chapter 8) true: represented by any nonzero value

54 Relational, Equality, and Logical Operators  Relational Operators: =  Equality Operators: == !=  Logical Operators: ! && ||  conditional operator: expr1? expr2: expr3  Semantics: First, expr1 is evaluated. If it is nonzero (true), then expr2 is evaluated, and this is the value of the conditional expression as a whole. If expr1 is zero (false), then expr3 is evaluated, and this is the value of the conditional expression as a whole.

55 The if and if-else Statement  exp is enclosed by parentheses  Where appropriate, compound statements should be used to group a series of statements under the control of a single if expression  An if or if-else statement can be used as the statement part of another if or if-else statement. an else attaches to the nearest if. if (expr) statement1 else statement2 if (expr) statement1

56 The switch Statement  General form: switch ( switch_exp ) { case constant_exp1: statements; break; // optional case constant_exp2 : statements; break; // optional... case constant_expn: statements; break; // optional default: statements; break; } //optional

57 The switch Statement  The effect of a switch:  Evaluate the switch_exp.  Go to the case label having a constant value that matches the value of the switch_exp. If a match is not found, go to the default label. If there is no default label, terminate the switch.  Terminate the switch when a break statement is encountered, or by “falling off the end”.

58 The switch Statement switch ( switch_exp ) { case constant_exp1: statements; break; // optional case constant_exp2 : statements; case constant_exp3 : statements; …../* no break */ case constant_expi : statements; break; …… case constant_expn: statements; break; // optional …… } Next statement;

59 The switch Statement switch ( switch_exp ) { case constant_exp1: statements; break; // optional …… case constant_expi : statements; break; …… case constant_expn: statements; break; // optional default: statements; break; } Next statement;

60 The switch Statement switch ( switch_exp ) { case constant_exp1: statements; break; // optional …… case constant_expi : statements; break; …… case constant_expn: statements; break; // optional } Next statement;

61 The while Statement  General form while (expr) Statement Next statement  First expr is evaluated.  If expr is nonzero (true), then statement is executed and control is passed back to the beginning of the while loop. Statement is repeatedly until expr is zero (false)  Then control passes to next statement.

62 The for Statement  Semantics:  First expr1 is evaluated.  Then expr2 is evaluated. If expr2 is nonzero (true), othen statement is executed, oexpr3 is evaluated ocontrol passes back to the beginning of the for loop again, except that evaluation of expr1 is skipped. The process continues until expr2 is zero (false), at which point control passes to next statement. for(expr1; expr2; expr3){ statement } next statement

63 The for Statement  for (expr1; expr2; expr3)  Any of all of the expressions in a for statement can be missing, but the two semicolons must remain. If expr1 is missing, no initialization step is performed as part of the for loop When expr2 is mission, the rule is that the test is always true. for (expr1; expr2; expr3) statement Next statement

64 The do Statement  Semantics  First statement is executed, and expr is evaluated.  If the value of expr is nonzero (true), then control passes back to the beginning of the do statement, and process repeats itself.  When expr is zero (false), then control passes to next statement do statement while (expr); Next statement

65 The Break and Continue Statements  break statement  An exit from the innermost enclosing loop (such as a while loop) statement or switch statement  continue statement  Causes the current iteration of a loop to stop and the next iteration to begin immediately.

66 Review of Chapter 4

67 Outline of Chapter 4  How to write a function?  Function Invocation  Function Definition  The return Statement  Function Prototypes  More about function  Program Correctness: The assert() Macro  Function Declarations from the Compiler’s Viewpoint  Invocation and Call-by-Value  Developing a Large Problem

68 How to write a function? #include void prn_message(void); int main(void) { prn_message(); prn_message(); printf(“Back to main function\n”); return 0; } void prn_message(void) { printf(“A message for you: “); printf(“A message for you: “); printf(“Have a nice day!\n”); printf(“Have a nice day!\n”); return; return;}  How to give the compiler information about the function? Function prototype:  How to pass control to the function? Function Invocation  How to specify the function? Function Definition  How to get the control back? return statement

69 How to write a function? Preprocessing directives function prototype of fucntion1 function prototype of fucntion2 int main(void) { Body of function definition } Header of function1 definition { Body of function definition } Header of function2 definition { Body of function definition } function invocations

70 Program Correctness: The assert() Macro  C provides the assert() macro in assert.h to guarantee certain conditions  assert(exp) ensures the value of exp is true if exp is false, the system prints out a message and abort the program.

71 Function Declarations from the Compiler’s Viewpoint Preprocessing directives function prototype of fucntion 1 int main(void) { Body of function definition } Header of function1 definition { Body of function definition } Header of function1 definition { Body of function definition } Preprocessing directives Header of function1 definition { Body of function definition } Header of function1 definition { Body of function definition } int main(void) { Body of function definition } Give either the function definition or the function prototype or both before a function is used

72 Invocation and Call-by-Value  Function Invocation fun_name(exp1, exp2);  All arguments are passed call-by-value Each argument is evaluated, and its value is used locally in place of the corresponding formal parameter. If a variable is passed to a function, the stored value of that variable in the calling environment is not changed.

73 #include int min2(int a, int b); int min3(int a, int b, int c); min.h #include “min.h” int main(void) { printf("%d\n", min3(1,3,4) ); return 0; } main.c int min2(int a, int b) { if (a<b) return a; else return b; } int min3(int a, int b, int c) { int mofab = min2(a,b); return min2(mofab, c); } min.c f.h: function prototypes f1.c, f2.c, f3.c  function definitions  in each.c file #include “f.h”  gcc f1.c f2.c f3.c gcc main.c min.c Developing a Large Problem