Number Systems, Scalar Types, and Input and Output

Similar documents
공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -VARIABLE AND DATA TYPES- SPRING 2015, SEON-JU AHN, CNU EE

ESC101N Fundamentals of Computing

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Data Type Fall 2014 Jinkyu Jeong

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Work relative to other classes

PROGRAMMAZIONE I A.A. 2017/2018

Internal representation. Bitwise operators

Types, Variables, and Constants

A Fast Review of C Essentials Part I

Internal representation. Bitwise operators

Internal representation. Bitwise operators

Basic Types and Formatted I/O

File Handling in C. EECS 2031 Fall October 27, 2014

These are reserved words of the C language. For example int, float, if, else, for, while etc.

CS107, Lecture 2 Bits and Bytes; Integer Representations

Computer System and programming in C

Chapter 7. Basic Types

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Reserved Words and Identifiers

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Lecture 2 Tao Wang 1

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

BSM540 Basics of C Language

Types, Operators and Expressions

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Basic Elements of C. Staff Incharge: S.Sasirekha

The C++ Language. Arizona State University 1

The New C Standard (Excerpted material)

Types, Operators and Expressions

Computers Programming Course 5. Iulian Năstac

CSc Introduction to Computing

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

ANSI C Programming Simple Programs

Programming for Engineers Introduction to C

BITG 1233: Introduction to C++

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

6.096 Introduction to C++ January (IAP) 2009

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

THE FUNDAMENTAL DATA TYPES

Unit 4. Input/Output Functions

INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT. Dept. of Electronic Engineering, NCHU. Original slides are from

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

Programming. Elementary Concepts

BLM2031 Structured Programming. Zeyneb KURT

COP 3275: Chapter 07. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

Programming Language A

Numeric Data Types in C

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

Data Types and Variables in C language

JAVA Programming Fundamentals

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

INTRODUCTION 1 AND REVIEW

Java Basic Datatypees

Fundamental of Programming (C)

C: How to Program. Week /Mar/05

UNIT- 3 Introduction to C++

AWK - PRETTY PRINTING

Variables and Literals

Computer Organization & Systems Exam I Example Questions

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Fundamental of C programming. - Ompal Singh

Fundamentals of Programming Session 4

Programming and Data Structures

2. Numbers In, Numbers Out

EECS2031. Modifiers. Data Types. Lecture 2 Data types. signed (unsigned) int long int long long int int may be omitted sizeof()

Chapter 2 - Introduction to C Programming

Chapter 2: Introduction to C++

Programming in C Quick Start! Biostatistics 615 Lecture 4

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Number Representation 3/2/01 Lecture #

2. Numbers In, Numbers Out

C Programming

printf( Please enter another number: ); scanf( %d, &num2);

Fundamentals of Programming

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Presented By : Gaurav Juneja

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

PYTHON- AN INNOVATION

Programming in C++ 4. The lexical basis of C++

Fundamentals of Programming. Lecture 11: C Characters and Strings

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Java Notes. 10th ICSE. Saravanan Ganesh

LECTURE 02 INTRODUCTION TO C++

Lecture 3. More About C

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

CS102: Variables and Expressions

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Transcription:

Number Systems, Scalar Types, and Input and Output Outline: Binary, Octal, Hexadecimal, and Decimal Numbers Character Set Comments Declaration Data Types and Constants Integral Data Types Floating-Point Numbers

Binary, Octal, Hexadecimal, and Decimal Binary Binary numbering system has only two possible values for each digit: 0 and 1. For example, binary number decimal number 0 0 1 1 10 2 11 3 100 4 101 5 110 6 1100 1010 202

Decimal Numbers Decimal The digits' weight increases by powers of 10. The weighted values for each position is determined as follows: 10 4 10 3 10 2 10 1 10 0 10000 1000 100 10 1 For example, A decimal number 4261 can be thought of as follows. 4 * 1000 + 2 * 100 + 6 * 10 + 1 * 1 = 4000 + 200 + 60 + 1 = 4261 (decimal)

Binary Binary, Octal, Hexadecimal, and Decimal The digits' weight increases by powers of 2. The weighted values for each position is determined as follows: 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 128 64 32 16 8 4 2 1 For example, binary 10 is decimal 2. the binary value 1100 1010 represents the decimal value 202. 1 * 128 + 1 * 64 + 0 * 32 + 0 * 16 + 1 * 8 + 0 * 4 + 1 * 2 + 0 * 1 = 128 + 64 + 0 + 0 + 8 + 0 + 2 + 0 = 202 (decimal)

Binary Two s Complement The left-most bit is the sign bit. If it is 1, then the number is negative. Otherwise, it is positive. Give a negative value, represent it in binary two s complement form as follows. 1. write the number in its absolute value. 2. complement the binary number. 3. plus 1. Example, represent 2 in binary two s complement with 16 bits for short int. Binary value of 2: 0b0000 0000 0000 0010 Binary complement of 2: 0b1111 1111 1111 1101 Plus 1: +1 Binary two s complement representation of -2: 0b1111 1111 1111 1110

Give binary two s complement form of a negative number, find the absolute value of the negative value as follows. 1. Complement the binary number. 2. Plus 1. Example, find the decimal value of (0b1111 1111 1111 1110) 2 in binary two s complement form with 16 bits. Binary two s complement (0b1111 1111 1111 1110) 2 Binary complement (0b0000 0000 0000 0001) 2 Plus 1 Absolute value: +1 (0b0000 0000 0000 0010) 2 = 2 10 Negative value: -2

Subtraction of a value in the computer can be treated as addition of its two s complement. For example, the subtraction of (2-2) can be performed as 2+(-2) as follows: 0b0000 0000 0000 0010 (binary representation of 2) 0b1111 1111 11111110 (two s complement representation of -2) 0b0000 0000 0000 0000 (2+(-2))

Example > short i, j > i = 0b0000000000000010 2 > j = 0b1111111111111110-2 > i+j 0

Octal The octal system is based on the binary system with a 3-bit boundary. The octal number system uses base 8 includes 0 through 7. The weighted values for each position is as follows: 8 3 8 2 8 1 8 0 512 64 8 1 1. Binary to Octal Conversion Break the binary number into 3-bit sections from the least significant bit (LSB) to the most significant bit (MSB). Convert the 3-bit binary number to its octal equivalent. For example, the binary value 1 010 000 111 101 110 100 011 equals to octal value (12075643) 8.

2. Octal to Binary Conversion Convert the octal number to its 3-bit binary equivalent. Combine all the 3-bit sections. For example, the octal value 45761023 equals to binary value 100 101 111 110 001 000 010 011. 3. Octal to Decimal Conversion To convert octal number to decimal number, multiply the value in each position by its octal weight and add each value together. For example, the octal value (167) 8 represents decimal value 119. 1*64 + 6*8 + 7*1 = 119

Hexadecimal Similar to octal, the hexadecimal system is also based on the binary system but using 4-bit boundary. The hexadecimal number system uses base 16 including the digits 0 through 9 and the letters A, B, C, D, E, and F. The letters A through F represent the decimal numbers 10 through 15. For the decimal values from 0 to 15, the corresponding hexadecimal values are listed below. Decimal Hexadecimal 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 F E D C B A 9 8 7 6 5 4 3 2 1 0

The weighted values for each position is as follows: 16 3 16 2 16 1 16 0 4096 256 16 1 The conversion between binary value and hexadecimal value is similar to octal number,but using four-bit sections. The hexadecimal value 20A represents decimal value 522. 2*256 + 0*16 + 10*1 = 522

Following table provides all the information you need to convert from one type number into any other type number for the decimal values from 0 to16. Binary Octal Decimal Hex Binary Octal Decimal Hex 0000 00 00 00 1001 11 09 09 0001 01 01 01 1010 12 10 A 0010 02 02 02 1011 13 11 B 0011 03 03 03 1100 14 12 C 0100 04 04 04 1101 15 13 D 0101 05 05 05 1110 16 14 E 0110 06 06 06 1111 17 15 F 0111 07 07 07 10000 20 16 10 1000 10 08 08

Character Set The character set in C includes the following members: the 26 uppercase letters of the Latin alphabet A B C D E F G H I J K L M N O P Q R S T U V W X Y Z the 26 lowercase letters of the Latin alphabet a b c d e f g h i j k l m n o p q r s t u v w x y z the 10 decimal digits 0 1 2 3 4 5 6 7 8 9 the following 31 graphic characters! " # % & ' ( ) * +, -. / : ; < = >? [ \ ] ^ _ { } ~ $ `

Comments Comments of a C program can be enclosed within a pair of delimiters /* and */. The symbol // will comment out a subsequent text terminated at the end of a line. For example, /* This is a comment */ /* This is a comment across multiple lines */ printf( Hello, world\n ); // This is a comment terminated at the end of line

Declaration An identifier for a variable shall consist of lowercase and uppercase letters, underscore _, and digits. It shall not start with digits. A variable has to be declared before it can be used inside a program. The following format can be used to declare a variable of simple type. datatype varname; where datatype is one of valid data types and varname is an identifier. int main() { int i; i = 90; int j; j = 20; return 0; }

Data Types and Constants Integer Data Types Integer is a basic data type and can be represented by one of the following data types. char signed char unsigned char short signed short unsigned short int signed int unsigned int long signed long unsigned long long long signed long long unsigned long long

The sizeof operator gives the size of types or expression in bytes. One byte equals 8 bits. > int i > sizeof(int) 4 > sizeof(i) 4 > sizeof(2*i) 4 > sizeof(long long) 8

Int Data Representation An int data is a signed integer. An int number is a whole number that can be negative, positive, or zero. An int data uses 4 bytes for storage with 1 bit for the sign. The int ranges from INT_MIN to INT_MAX, which are 2147483648( 2 31 ) and 2147483647(2 31 1), respectively. The unsigned int ranges from 0 to UINT_MAX, which is equal to 4294967295(2 32 1). INT_MAX 0b01111111 11111111 11111111 11111111 INT_MIN 0b10000000 00000000 00000000 00000000 UINT_MAX 0b11111111 11111111 11111111 11111111

Program: limits.c Output: #include <stdio.h> #include <limits.h> Int main() { printf( INT_MAX = %d\n, INT_MAX); printf( INT_MIN = %d\n, INT_MIN); printf( UINT_MAX = %d\n, UINT_MAX); return 0; } INT_MAX = 2147483647 INT_MIN = -2147483648 UINT_MAX = 4294967295

Short Data Representation A short data uses 2 bytes for storage. The macros for minimum and maximum values of signed short are SHRT_MIN and SHRT_MAX defined in header file limits.h. SHRT_MIN is equal to -32768(-2 15 ) and SHRT_MAX is equal to 32767(2 15-1). The macro USHRT_MAX, defined in the header file limits.h, specifies the maximum value of unsigned short. It is equal to 65535(2 16-1).

Long Long Data Representation Data of long long integral type contains 8 bytes. They have the similar representation as the data type of int. The long long int ranges from LLONG_MIN to LLONG_MAX, which are -9223372036854775808LL (-2 63 ) and 9223372036854775807LL (2 63 1), respectively. The long long int ranges from 0 to ULLONG_MAX, which is 2 64 1.

Integer Constants An integer can be specified in decimal, binary, octal, or hexadecimal. A leading 0(zero) on an integer constant indicates an octal integer. A leading 0x or 0X indicates a hexadecimal integer. A leading 0b or 0B indicates a binary integer (in Ch only). Example: 30 (decimal) = 036 (octal) = 0X1e or 0x1E (hexadecimal) = 0b11110 or 0B11110 (binary)

Example: C:/Ch> int i C:/Ch> i = 036 30 C:/Ch> i = 0x1e 30 C:/Ch> i = 0b11110 30 C:/Ch> printf( i = %d\n, i) i = 30 C:/Ch> printf( i = 0%o\n, i) i = 036 C:/Ch> printf( i = 0x%x\n, i) i = 0x1e C:/Ch> printf( i = 0b%b\n, i) i = 0b11110

Boolean Type The keyword bool of a declarator in header file stdbool.h can be used to declare variables with boolean data type. For example, the following statement bool b; declares a boolean variable b. A boolean variable only has only two possible values: 1 and 0. Value 1 stands for true and value 0 stands for false. Macro true and false are defined in header file stdbool.h.

Char Data Representation The char data are used to store characters such as letters and punctuation. An array of char can be used to store a string. A character is stored as an integer according to a certain numerical code such as the ASCII code that ranges from 0 to 127, which only requires 7 bits to represent it. Typically, a char constant or variable occupies 1-byte (8 bits) of unit memory. Memory Address Binary value Character 20000 0 1 0 0 1 0 0 0 H 20001 0 1 1 0 0 1 0 1 e 20002 0 1 1 0 1 1 0 0 l 20003 0 1 1 0 1 1 0 0 l 20004 0 1 1 0 1 1 1 1 o 20005 0 0 1 0 0 0 0 1!

The macros for minimum and maximum values of signed char are CHAR_MIN and CHAR_MAX defined in header file limits.h. CHAR_MIN is equal to -128(-2 7 ) and CHAR_MAX is equal to 127(2 7-1). The macro UCHAR_MAX, defined in the header file limits.h, specifies the maximum value of unsigned short. It is equal to 255(2 8-1). CHAR_MAX = 127 0b0111 1111 CHAR_MIN = -128 0b1000 0000 UCHAR_MAX = 255 0b1111 1111

Character Constants A character constant, stored as an integer, can be written as one character within a pair of single quotes like x. A character constant can be assigned to the variable of type char. For example, > char c = x > c x

Escape Characters Escape Code Description \a (alert) Produces an audible or visible alert. The active position shall not be changed. \b (backspace) Moves the active position to the previous position on the current line. \f (form feed) Moves the active position to the initial position at the start of the next logical page. \n (new line) Moves the active position to the initial position of the next line. \r (carriage return) Moves the active position to the initial position of the current line. \t (horizontal tab) Moves the active position to the next horizontal tabulation position on the current line. \v (vertical tab) Moves the active position to the initial position of the next vertical tabulation position. \\ (backslash) Produces a backslash character \. \ (single quote) Produces a single quote character. \ (double quote) Produces a double quote character. \? (question mark) Produces a question mark character?.

String Literals A character string literal is a sequence of zero or more multibyte characters enclosed in double quotes, such as xyz. Remember that strings represented as character arrays end with \0. Using an array of characters to define a string variable. > char str1[6] = abcde // The last one is \0 > char str2[] = This is a string.

Arithmetic operations of two integers are still an integer > 2+3 5 > 3/2 1 > 2/3 0 > 2.0/3 0.6667

Floating-Point Types Floating-point numbers have three representations: float, double, and long double. The float data type uses 32 bits (4 bytes) for its storage. The minimum and maximum values of float data type are defined as macros FLT_MIN and FLT_MAX, respectively, in header file float.h. The double data type uses 64 bits(8 bytes) as its storage. The minimum and maximum values of double data type are defined as macros DBL_MIN and DBL_MAX, respectively, in header file float.h The long double should have at least as many bits as double.

Metanumbers Below is a list of metanumbers for floating-point numbers and their mathematical equivalent. Metanumbers Mathematical Representation -0.0 0 - +0.0 0 + -Inf - +Inf + NaN Not-a-Number (Invalid value)

Examples for NaN and Inf > 0.0/0.0 nan > 1.0/0.0 inf > -1.0/0.0 -inf > sqrt(4) 2.000 > sqrt(-4) nan

Printing Multiple Numerical Values in a Single Printing Statement Use multiple format specifiers. Each format specifier corresponds to an argument. > printf( integer is %d, floating-point number is %f, 10, 12.34) integer is 10, floating-point number is 12.340000

Precision of Floating-Point Numbers The precision of a floating-point number specifies the number of digits after the decimal point character. The precision typically takes the form of a period (.) followed by an decimal integer. For example, the format %.2f specifies the precision with 2 digits after the decimal point. > printf( %.2f, 12.1234) 12.12 > printf( %.2f, 12.5678) 12.57 > printf( %.20f, 0.2) 0.20000000000000001110 The fractional part after the specified precision number is rounded up. A floating-point number may not be represented exactly.

Field Width of Numbers The field width is the size of a field in which data are printed. An integer width is inserted between % and the conversion specifier to indicate the field width For example, %6d specifies the field width of 6 for an integer, 8.4 specifies a field width of 8 with 4 digits after the decimal point. > printf( %6d, 12) 12 > printf( %8.4f, 5.12345) 5.1234 To print a %, use %% in the format string. > printf( 10%% of 100 is 10\n ) 10% of 100 is 10

Function scanf() Precise formatting input is accomplished using the input function scanf. The scanf function has following form scanf( format-control-string, arguments ); Format-control-string: Using specifications to describe input format. Each specification begins with a percent sign(%), ends with conversion specifier and is enclosed in quotation marks. The format-control-string is similar to format-control-string discussed in printf function. arguments: pointers to variables in which the input value will be stored.

Table below lists the format-control-string of different argument types for function scanf(). Using an extra \n such as %d\n is a common mistake. char short unsigned short int unsigned int long long Argument Type unsigned long long %c %hd %uhd %d %u %lld %ulld Format-Control-String float double string pointer %f %lf %s %p

Example: > int i > float f > double d > char c > scanf( %d, &i); 10 > i 10 > scanf( %f, &f); 10.2 > f 10.20 > scanf( %lf, &d); 15 > d 15.0000 > scanf( %c, &c); a > c a

Example: > int i > scanf( %d, &i); // input number in decimal 4261 > Long long l > scanf( %lld, &l); // input into long long number 4261 > l 4261 > scanf( %b, &i); // input number in binary in Ch 1000010100101 // or 0b1000010100101 > i 4261 > scanf( %o, &i); // input number in octal 10245 // or 010245 > i 4261 > scanf( %x, &i); // input number in hexadecimal 10A5 > i 4261 // or 0x10A5 or 0X10A5

Example: Program scanf.c /* File: scanfc.c for input and output example */ #include <stdio.h> int main() { int num; double d; } printf("please input an integer and one floating-point number\n); scanf("%d%lf",&num, &d); printf("your input values are %d and %f\n, num, d); return 0; Interactive execution of program scanf.c > scanfc.c Please input one integer and one floating-point number 10 12.3456 Your input number is 10 and 12.345600 >