7. BASIC TYPES.

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.
Introduction to C Programming CE Lecture 2 Basics of C 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.
Data types and variables
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Basic Elements of C++ Chapter 2.
Objectives You should be able to describe: Data Types
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.
1 Lecture04: Basic Types & Function Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
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.
Chapter 7: Basic Types Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 7 Basic Types.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
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.
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.
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.
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 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Primitive Types Four integer types: Two floating-point types:
C Formatted Input/Output
Chapter 2 Variables.
CSE 220 – C Programming Bitwise Operators.
Chapter Topics The Basics of a C++ Program Data Types
Basic Types Chapter 7 Copyright © 2008 W. W. Norton & Company.
Binary Numbers The arithmetic used by computers differs in some ways from that used by people. Computers perform operations on numbers with finite and.
Chapter 2: Introduction to C++
BASIC ELEMENTS OF A COMPUTER PROGRAM
ECE Application Programming
Tokens in C Keywords Identifiers Constants
Wel come.
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
TMF1414 Introduction to Programming
EPSII 59:006 Spring 2004.
Plan of the Day: More on type conversions scanf printf format strings
Fundamental Data Types
Basic Elements of C++ Chapter 2.
Input and Output Lecture 4.
Primitive and Reference Data Values
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
Chapter 2 Variables.
Lectures on Numerical Methods
Basic Types Chapter 7 Copyright © 2008 W. W. Norton & Company.
Fundamental Data Types
EECE.2160 ECE Application Programming
Chapter 2 Variables.
Lexical Elements & Operators
EECE.2160 ECE Application Programming
Variables and Constants
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Programming Fundamental-1
Presentation transcript:

7. BASIC TYPES

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 –263 to 263 – 1, and unsigned long long int, whose values typically range from 0 to 264 – 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.

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: 15 255 32767 Octal integer constants contain only digits between 0 and 7, and must begin with a zero: 017 0377 077777

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.

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: 57.0 57. Examples of scientific notation: 57.0e0 57.0E0 57e0 5.7e1 5.7e+1 .57e2 570.e-1

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: 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 */ 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.

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 */ x = i; /* Example 2 */ 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.

The Usual Arithmetic Conversions Examples of the usual arithmetic conversions: char c; short s; int i; long int l; float f; double d; long double ld; i = i + c; /* c is converted to int */ i = i + s; /* s is converted to int */ l = l + i; /* i is converted to long */ f = f + l; /* l 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 = 842.97; /* i is now 842 */ i = -842.97; /* i is now -842 */

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

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.