7. BASIC TYPES. Systems of numeration Numeric Types C’s basic types include integer types and floating types. Integer types can be either signed or unsigned.

Slides:



Advertisements
Similar presentations
Type Conversion. C provides two methods of changing the type of an expression: Type conversion (done implicitly) Cast expressions (done explicitly) Examples.
Advertisements

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)
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.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Data types and variables
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Basic Elements of C++ Chapter 2.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Objectives You should be able to describe: Data Types
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
© 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.
Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 4 Expressions.
Programming III SPRING 2015
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.
IT253: Computer Organization
1 Lecture04: Basic Types & Function Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
C Tokens Identifiers Keywords Constants Operators Special symbols.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Chapter 7: Basic Types Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 7 Basic Types.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Simple Data Types Built-In and User Defined Chapter 10.
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
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.
Copyright © – Curt Hill Types What they do.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Administrative things
Chapter 7: Basic Types Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 7 Basic Types Last item in Page 89: check stdint.h.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
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.
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.
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.
1 Objects Types, Variables, and Constants Chapter 3.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
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.
C Formatted Input/Output
Basic Types Chapter 7 Copyright © 2008 W. W. Norton & Company.
ECE Application Programming
Tokens in C Keywords Identifiers Constants
7. BASIC TYPES.
EPSII 59:006 Spring 2004.
Plan of the Day: More on type conversions scanf printf format strings
Fundamental Data Types
Chapter 2 - Data Types and Storage Classes
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.
Rock, paper, scissors example
Lectures on Numerical Methods
Basic Types Chapter 7 Copyright © 2008 W. W. Norton & Company.
Fundamental Data Types
Presentation transcript:

7. BASIC TYPES

Systems of numeration

Numeric Types C’s basic types include integer types and floating types. Integer types can be either signed or unsigned. A value of a signed type can be either negative, zero, or positive. A value of an unsigned type must be zero or positive.

Integer Types C provides six integer types: C99 also supports the long long int type, whose values typically range from –2 63 to 2 63 – 1, and unsigned long long int, whose values typically range from 0 to 2 64 – 1.

Integer Types Integer types can be abbreviated by omitting the word int: long i; /* same as long int i; */ In general, avoid using unsigned types. INT_MIN INT_MAX

Integer Constants Integer constants may be written in decimal (base 10), octal (base 8), or hexadecimal (base 16). Decimal integer constants contain digits, but must not begin with a zero: Octal integer constants contain only digits between 0 and 7, and must begin with a zero:

Integer Constants Hexadecimal integer constants contain digits between 0 and 9 and letters between A and F (or a and f), and always begin with 0x: 0xf 0xff 0x7fff The letters in a hexadecimal constant may be either upper or lower case. Force type 15L 0377L 0x7fffL 15U 0377U 0x7fffU 0xffffffffUL

Integer Overflow Signed Unsigned

Reading and Writing Integers When reading or writing an unsigned integer, use the letter u (decimal), o (octal), or x (hex) in the conversion specification: unsigned int u; printf("%u", u); /* writes u in base 10 */ printf("%o", u); /* writes u in base 8 */ printf("%x", u); /* writes u in base 16 */ When reading or writing a short integer, put the letter h in front of d, o, x, or u: short int s; printf("%hd", s);

Reading and Writing Integers When reading or writing a long integer, put the letter l in front of d, o, x, or u: long int l; printf("%ld", l);

Floating Types C supports three floating types: (Ranges shown are typical.)

Floating Constants Floating constants can be written in either fixed-point notation or scientific notation. Examples of fixed-point notation: Examples of scientific notation: 57.0e0 57.0E0 57e0 5.7e1 5.7e+1.57e2 570.e-1 Floating constants are by default represente as double: 57.0e0 57.0E0 57e0 5.7e1 5.7e+1.57e2 570.e-1 Force type: 57.0f 57.0L

Reading and Writing Floating- Point Numbers When reading (not writing) a value of type double, put the letter l in front of e, f, or g: double d; scanf("%lf", &d); Warning: double values must be written using e, f, or g, just like float values: double d; printf("%f", d); In C99, however, using %le, %lf, or %lg to print a double value is legal.

Reading and Writing Floating- Point Numbers When reading or writing a value of type long double, put the letter L in front of e, f, or g: long double ld; printf("%Lf", ld);

The char Type In addition to the numeric types, C provides one other basic type: char, the character type. A variable of type char can be assigned any character in the underlying character set: char c; c = 'a'; c = '0'; c = ' '; Character constants are written in single quotes, not double quotes.

The char Type C represents values of type char as integers. Any operation that is allowed for integers is also legal for characters: if ('a' <= c && c <= 'z') c = c - 'a' + 'A'; /* converts c to upper case */ c=toupper(c); Also, characters and integers can be mixed in expressions: c = 'a'; c = c + 1; /* c now contains the letter 'b' */

Numeric Escapes Storing a special character in a variable requires the use of an escape sequence: c = '\n'; /* store new-line character in c */ Numeric escapes are used for characters that cannot be represented by character escapes. There are two kinds of numeric escapes: Octal Hexadecimal

Numeric Escapes An octal escape sequence is written \ddd, where ddd is a one-, two-, or threedigit octal number representing the position of the character in the underlying character set (usually ASCII). Examples: null character \0 escape character \33 A hexadecimal escape sequence consists of \x followed by a hexadecimal number. Examples: null character \x0 escape character \x1b (or \x1B) A numeric escape is enclosed in single quotes when used as a character constant: c = '\0'; /* store null character in c */ c = '\x1b'; /* store escape character in c */

Universal Character Names C99 also provides universal character names, which have the form \udddd or \Udddddddd, where each d is a hexadecimal digit. Each dddd or dddddddd sequence is a UCS (Universal Character Set) code. Unicode is closely related to UCS, and its codes are compatible with those of UCS. Universal character names that represent letters and digits are allowed in identifiers.

Reading and Writing Characters To read or write a character using scanf or printf, use the %c conversion specification: scanf("%c", &c); printf("%c", c); A faster alternative: Use getchar and putchar instead of scanf and printf : c = getchar(); putchar(c);

Reading and Writing Characters Neither scanf nor getchar skips white space before reading a character. To read the first nonblank character, use scanf in the following way: scanf(" %c", &c); /* skip white space, then read c */ To detect the end of an input line, test whether the character read by scanf or getchar is a new-line character.

Examples while (getchar() == ' '); while (getchar() != '\n'); while ((ch = getchar()) != '\n') {//process ch }

Type Conversion C provides two methods of changing the type of an expression: Type conversion (done implicitly) Cast expressions (done explicitly) Examples of implicit conversion: float x; int i; if (x < i)... /* Example 1 (operand)*/ x = i; /* Example 2 (assignment) */ In each case, i is implicitly converted to float type.

The Usual Arithmetic Conversions The usual arithmetic conversions are applied to operands in an arithmetic or logical expression. Operands are converted to the “smallest” type that will safely accommodate both values. This is often done by converting (promoting) the operand of the “smaller” type to the type of the other operand. Integral promotion

The Usual Arithmetic Conversions Examples of the usual arithmetic conversions: char c; short int s; int i; unsigned int u; long int l; unsigned long int ul; float f; double d; long double ld; i = i + c; /* c is converted to int */ i = i + s; /* s is converted to int */ u = u + i; /* i is converted to unsigned int */ l = l + u; /* u is converted to long int */ ul = ul + l; /* l is converted to unsigned long int */ f = f + ul; /* ul is converted to float */ d = d + f; /* f is converted to double */ ld = ld + d; /* d is converted to long double */

Conversion During Assignment The usual arithmetic conversions do not apply to assignment. Instead, the value on the right side of the assignment is converted to the type of the variable on the left side. Warning: Converting from a “large” type to a “small” type may not always produce a meaningful result: int i; float f; i = f; This assignment is meaningful only if the value of f —when converted to an integer—lies between the smallest and largest values allowed for an int variable. If f is too large or too small, i will be assigned an apparently meaningless number.

Conversion During Assignment The conversion of a floating-point number to an integer is done by dropping the fractional part of the number (not by rounding to the nearest integer): int i; i = ; /* i is now 842 */ i = ; /* i is now -842 */

char c; int i; float f; double d; i = c; /* c is converted to int */ f = i; /* i is converted to float */ d = f; /* f is converted to double */ c = 10000; /*** WRONG ***/ i = 1.0e20; /*** WRONG ***/ f = 1.0e100; /*** WRONG ***/

Casting Casting is used for explicit type conversion. A cast expression has the form ( type-name ) expression Example 1: float f = 3.5, frac_part; frac_part = f - (int) f; /* frac_part is now 0.5 */ Example 2: int i = 5, j = 2; float f; f = i/j; /* f is now 2.0 */ f = ((float) i)/j; /* f is now 2.5 */

Avoiding overflow Casts are sometimes necessary to avoid overflow: long i; int j = 1000; i = j * j; /* overflow may occur */ Using a cast avoids the problem: i = (long) j * j; The statement i = (long) (j * j); /*** WRONG ***/ wouldn’t work, since the overflow would already have occurred by the time of the cast.

The sizeof Operator The sizeof operator can be used to determine how much memory a particular type requires: printf("Size of char: %lu\n", (unsigned long) sizeof(char)); printf("Size of int: %lu\n", (unsigned long) sizeof(int)); printf("Size of long int: %lu\n", (unsigned long) sizeof(long)); sizeof returns its answer in bytes. sizeof returns an implementation-defined unsigned integer type. As a result, it is best to cast the value returned by sizeof to unsigned long when trying to print it.

The sizeof Operator sizeof works with variables as well as types: int i; printf("Size of i: %lu\n", (unsigned long) sizeof(i));

Type Definitions Type definitions create new names for types: typedef float Dollars; typedef int Bool; A common convention is to capitalize the first letter of a typedef name. Once a type has been defined, it can be used in the same way as the built-in type names: Dollars cash_in, cash_out; Bool flag; Advantages of type definitions: Can make programs more understandable. Can make programs easier to modify. Can make programs more portable.