Fundamental data types

Slides:



Advertisements
Similar presentations
1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
Advertisements

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 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 Chapter 6 The Fundamental Data Types. 2 Outline  Declarations and expressions  Fundamental data types  Characters and the data type char  The Data.
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 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
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.
The fundamental data type 제 4 주 강의. Declarations, Expressions, and Assignments Declaring the type of a variable  set an appropriate amount of space in.
CS150 Introduction to Computer Science 1
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
1 Review of Chapter 6: The Fundamental Data Types.
Variables, Data Types and I/O in C
Introduction to programming Language C, Data Types, Variables, Constants. Basics of C –Every program consists of one or more functions and must have main.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
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 programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 3: Introduction to C: Input & Output, Assignments, Math functions.
Fundamental Data Types, Operators and Expressions Kernighan/Ritchie: Kelley/Pohl: Chapter 2 Chapter 2, 3.
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.
Introduction to Programming Lecture 4: Calculations.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
1 Programming a Computer Lecture Ten. 2 Outline  A quick introduction to the programming language C  Introduction to software packages: Matlab for numerical.
Administrative things
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 1.
Programming Fundamentals
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.
The Cast Operator The cast operator converts explicitly from one data type of an expression to another. For example, if x is of type int, the value of.
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.
CPT: Chars/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to look at how C processes characters 4. Character.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
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.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
CSCE 206 Structured Programming in C
Tokens in C Keywords Identifiers Constants
TMF1414 Introduction to Programming
7. BASIC TYPES.
Functions, Part 2 of 2 Topics Functions That Return a Value
INC 161 , CPE 100 Computer Programming
Fundamental Data Types
Data Type.
Lexical Elements, Operators, and the C Cystem
Introduction to the C Language
C Program Design Data Types
توابع ورودي-خروجي.
Data Type.
Lexical Elements, Operators, and the C Cystem
CS111 Computer Programming
Lectures on Numerical Methods
Basic Types Chapter 7 Copyright © 2008 W. W. Norton & Company.
Introduction to Programming
Chapter 4 Managing Input and Output Operations
Fundamental Data Types
Introduction to C Programming
What Actions Do We Have Part 1
Data Type.
DATA TYPES There are four basic data types associated with variables:
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

Fundamental data types Chapter 3 in ABC

#include <stdio.h> int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers: ” ); scanf( “%d%d”, &b, &c ); a = b + c; x = y + z; return 0; } Declaration Declaration with initialization always initialize!!! Function calls Assignment statements

Fundamental data types Integral (signed or unsigned) char short int long Floating point types float double

Fundamental Data Types Long form char signed char unsigned char signed short int signed int signed long int unsigned short int unsigned int unsigned long int float double long double Common (short) form short int long unsigned short unsigned unsigned long

Functionality Groups Integral Types: char signed char unsigned char short int long unsigned short unsigned unsigned long Floating Types: float double long double Arithmetic Types: integral types floating types

Constants ‘a’, ‘x’, ‘0’, ‘&’, ‘\n’ char 1, 0, -54, 4234567 int 0l, 123l, –7766L long 0u, 23u, 3000000000U unsigned 1.2f, .2f, 1.f, 3.14159f float 1.0, -3.1412 double 1.0l, –2.4433l long double

Some Character Constants and their Integer Values character: 'a' 'b' 'c' ... 'z' integer value: 97 98 99 ... 112 character: 'A' 'B' 'C' ... 'Z' integer value: 65 66 67 ... 90 character: '0' '1' '2' ... '9' integer value: 48 49 50 ... 57 character: '&' '*' '+' integer value: 38 42 43 The character ‘0‘ has a value of 48

Some Character Constants and their Integer Values Name of Character Written in C Integer Value alert ‘\a’ 7 backslash ‘\\’ 92 horizontal tab ‘\t’ 9 newline ‘\n’ 10 null character ‘\0’ quote ‘\” 39 The character ‘\0‘ has a value of 0

Some Character Constants and their Integer Values char c = 'a'; printf("%c", c); printf("%d", c); printf("%c%c%c", c, c+1, c+2); a is printed 97 is printed abc is printed

Some Character Constants and their Integer Values character: 'A' 'B' 'C' ... 'Z' integer value: 65 66 67 ... 90 char c = 0; int i = 0; for ( i = 'a'; i <= 'z'; ++i ) printf( “%c”, i ); for ( c = 65; c <= 90; ++c ) printf( “%c”, c ); for ( c = '0'; c <= '9'; ++c ) printf( “%d”, c ); abc ... z is printed ABC ... Z is printed 484950 ... 57 is printed

User Defined Constants #include <stdio.h> int main(void) { int c = 0; while ( ( c = getchar() ) != EOF ) putchar( c ); } return 0; Loop until the End Of the File in stdio.h appear the line: #define EOF (-1)

Constants Type Description Example char single character ‘g’ special character ‘\n’ hexadecimal code ‘\x1a’ int decimal -1, 2, 0, 123456 octal 010, -020, 080 hexadecimal 0xfffe, 0x1000, 0x1, 0x2, 0x4, 0x8 double int.frac .5, 1.0, -2.1712, 1.23 int.frac + e[+,-]exp 123e-2, 12.3e-2

Decimal, Hexadecimal, Octal conversions #include <stdio.h> int main(void) { printf( “%d %x %o\n”, 19, 19, 19 ); printf( “%d %x %o\n”, 0x1c, 0x1c, 0x1c); printf( “%d %x %o\n”, 017, 017, 017); printf( “%d\n”, 11 + 0x11 + 011); printf( “%x\n”, 2097151); printf( “%d\n”, 0x1FfFFf); return 0; } 19 13 23 28 1c 34 15 f 17 37 1fffff 2097151

compute the size of some fundamental types sizeof returns the number of bytes reserved for a variable type. #include <stdio.h> int main(void) { printf("The size of some fundamental types" " is computed.\n\n"); printf("char: %3d byte \n", sizeof(char) ); printf("short: %3d bytes\n", sizeof(short) ); printf("int: %3d bytes\n", sizeof(int) ); printf("long: %3d bytes\n", sizeof(long) ); printf("unsigned: %3d bytes\n", sizeof(unsigned) ); printf("float: %3d bytes\n", sizeof(float) ); printf("double: %3d bytes\n", sizeof(double) ); printf("long double:%3d bytes\n",sizeof(long double) ); return 0; } The difference between char, short, int, long is the number of bits they hold sizeof operator measures the number of bytes required to store an object. the size of variables are machine dependent!!!

compute the size of some fundamental types run on “PC Pentium 3": The size of some fundamental types is computed. char: 1 byte short: 2 bytes int: 4 bytes long: 4 bytes unsigned: 4 bytes float: 4 bytes double: 8 bytes long double: 8 bytes what is the output of the program on nova?

compute the size of some fundamental types sizeof(char) == 1 sizeof(short) <= sizeof(int) <= sizeof(long) sizeof(signed) == sizeof(unsigned) == sizeof(int) sizeof(float) <= sizeof(double) <= sizeof(long double)

Typically equal to 0xFFFFFFFF == 232-1 == 4294967295 Going over the Limit #include <stdio.h> #include <limits.h> int main( void ) { int i = 0; unsigned u = UINT_MAX; for ( i = 0; i < 5; ++i ) printf( "%u + %d = %u\n", u, i, u + i ); printf( "%u * %d = %u\n", u, i, u * i ); return 0; } Typically equal to 0xFFFFFFFF == 232-1 == 4294967295

The variable restarts itself Going over the Limit run on "scorpio": 4294967295 + 0 = 4294967295 4294967295 + 1 = 0 4294967295 + 2 = 1 4294967295 + 3 = 2 4294967295 + 4 = 3 4294967295 * 0 = 0 4294967295 * 1 = 4294967295 4294967295 * 2 = 4294967294 4294967295 * 3 = 4294967293 4294967295 * 4 = 4294967292 The variable restarts itself

Integers' Representation Binary value value Type vvvv vvvv unsigned char 0000 0000 1111 1111 255 0111 1111 127 1000 0000 128 svvv vvvv signed char 1111 1111 (2's complement) -1 1000 0000 (2's complement) -128 svvv vvvv vvvv vvvv signed short svvv vvvv vvvv vvvv vvvv vvvv vvvv vvvv signed int s means sign negative integers -in "2's complement" v means value

Integers' Representation (2) signed char sc = -128; unsigned char uc = 255; sc = sc - 1; uc = uc + 1; uc = 255; uc = uc*2; 1000 0000 1111 1111 0111 1111 = 127 Underflow 0000 0000 = 0 Overflow 1111 1111 1111 1110 = 254

Float representation seee eeee efff ffff ffff ffff ffff ffff Decimal base: int.frac*10exp 3/16 = 0.1875 = 1.875*10-1 1000/3 = 333.33333... =3.333...*102 Binary base: 1.frac*2exp 3/16 = 3*2-4 = 1.5*2-3 = 1.1*2-11 1/10 = 1.1001100110011...*2-100 Fixed size, limited accuracy. float = 4 bytes: seee eeee efff ffff ffff ffff ffff ffff double uses 8 bytes (64 = 1+11+52) http://steve.hollasch.net/cgindex/coding/ieeefloat.html base 2 standardized sign[1] exp+127[8] frac (mantissa)[23]

Float representation - limited accuracy #include <stdio.h> int main() { int i; float f = 0; for (i = 0; i < 100; ++i ) f += 0.01; printf( "%f\n", f ); return 0; } Learn more about in numerical analysis course (and numeric stability in geometric computing) Output: 0.999999

Special float values NaN – Not a Number. Represents an illegal value printf("%f\n", sqrt(-1)); will print -1.#IND00 or nan INF – infinity printf("%f\n", 1/0.0); will print 1.#INF00 or inf Use some special bit sequences to represent NaN and INF.

Mathematical Functions sqrt(x) pow(x,y) = xy exp(x) =eX log(x) sin(x) cos(x) tan(x) All the functions use doubles (floats: sqrtf, powf,expf,...) The functions are declared in <math.h> Requires linking with math library: gcc -lm

Infinite Loop #include <stdio.h> #include <math.h> int main(void) { double x = 0; printf( "\nThe square root of x and x raised" "\nto the x power will be computed.\n---\n\n" ); while ( 1 ) printf("Input x: "); scanf("%lf", &x ); if ( x >= 0.0 ) printf("\n%15s%22.15e\n%15s%22.15e\n%15s%22.15e\n\n" ,"x = ", x, "sqrt(x) = ", sqrt(x), "pow(x, x) =", pow(x, x) ); else printf("\nSorry, your number must be nonnegative.\n\n" ); } return 0; Infinite Loop

The Result of the Program The square root of x and x raised to the x power will be computed. --- Input x: 2 x = 2.000000000000000e+00 sqrt(x) = 1.414213562373095e+00 pow(x, x) = 4.000000000000000e+00 Input x:

The usual arithmetic conversion (promotion) If either operand is of type long double, double, float or unsigned long, the other operand is converted to long double, double, float or unsigned long appropriately. Otherwise, the "integral promotions" are performed on both operands, and the following rules are applied: If one operand has type long and the other operand has type unsigned then one of two possibilities occurs: If a long can represent all the values of an unsigned, then the operand of type unsigned is converted to long. If a long cannot represent all the values of an unsigned, then both operands are converted to unsigned long. Otherwise, if either operand is of type long, the other operand is converted to long. Otherwise, if either operand is of type unsigned, the other operand is converted to unsigned. Otherwise, both operands have type int.

Expressions and Types Expression Type c – s / i int u * 7 – i unsigned char c; short s; int i; unsigned u; unsigned long ul; float f; double d; long double ld; Expression Type c – s / i int u * 7 – i unsigned u * 2.0 – i double f * 7 – i float c + 3 7 * s * ul unsigned long c + 5.0 ld + c long double d + s u – ul 2 * i / l long u - l system dependent

Cast double d = 3.3; int i = 0; unsigned ui = 0; char c; i = (int)d; d = (double)i / 2; ui = (unsigned)i; c = 882; i == trunc(d) = 3 d == 1.5 882 = 0x372  overflow  c == 0x72=114 (behavior in case of overflow is undefined ANSI C)