CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.

Slides:



Advertisements
Similar presentations
18-May-15 Numbers. 2 Bits and bytes A bit is a single two-valued quantity: yes or no, true or false, on or off, high or low, good or bad One bit can distinguish.
Advertisements

1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
1 Chapter 6 The Fundamental Data Types. 2 Outline  Declarations and expressions  Fundamental data types  Characters and the data type char  The Data.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
1 Review of Class on Oct Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The.
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.
Fundamental data types
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.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
1 Character Processing How characters can be treated as small integers? How characters are stored and manipulated in a machine? How use is made of certain.
The fundamental data type 제 4 주 강의. Declarations, Expressions, and Assignments Declaring the type of a variable  set an appropriate amount of space in.
Chapter 2 Data Types, Declarations, and Displays
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
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.
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
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.
CISC105 – General Computer Science Class 9 – 07/03/2006.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 3: Introduction to C: Input & Output, Assignments, Math functions.
Data Type. Syntax Rules Recap keywords breakdoubleifsizeofvoid caseelseintstatic..... Identifiers not#me123th scanfprintf _idso_am_igedd007 Constant ‘a’‘+’
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
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
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
Variables Symbol representing a place to store information
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations 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.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
CS 1704 Introduction to Data Structures and Software Engineering.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
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.
CSE 220 – C Programming Bitwise Operators.
Character Processing How characters can be treated as small integers?
Tokens in C Keywords Identifiers Constants
7. BASIC TYPES.
INC 161 , CPE 100 Computer Programming
EPSII 59:006 Spring 2004.
Variable Symbol represents a place to store information
By: Syed Shahrukh Haider
Fundamental Data Types
Data Type.
Input and Output Lecture 4.
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.
Data Type.
Simple Data Types and Function Calls
C Programming Variables.
Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei
Lectures on Numerical Methods
UMBC CMSC 104 – Section 01, Fall 2016
Basic Types Chapter 7 Copyright © 2008 W. W. Norton & Company.
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Character Processing How characters can be treated as small integers?
Fundamental Data Types
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Data Type.
Numbers 6-May-19.
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL Senem KUMOVA-METİN2 Variables and constant  Variables and constant are the objects that program manipulates…  In C, all variables must be declared, i.e., their data type, before they can be used int main(void) { int a,b,c ; /* declaration*/ float x= 3.14; /* declaration with initialization*/ …}

CS115 FALL Senem KUMOVA-METİN3 Declarations and Expressions Declaration: tell the compiler to set aside an appropriate amount of space in memory to hold values associated with variable int x=1;   float y=1;   Expression: meaningful combinations of constants, variables, operators and function calls a+b sqrt(3.444), i=

CS115 FALL Senem KUMOVA-METİN4 Assignment SOME ASSIGNMENT EXAMPLES :  i=7;  x=x+1; /* x is assigned the old value of x plus 1 */  y+=4;  x +2 =0; /* WRONG */  0=1;/* WRONG */

CS115 FALL Senem KUMOVA-METİN5 FUNDAMENTAL DATA TYPES 1 character char signed char unsigned char 2 integer shortintlong unsigned short unsigned long 3 float doublelong double

CS115 FALL Senem KUMOVA-METİN6 FUNDAMENTAL DATA TYPES 1 character char signed char unsigned char 2 integer shortintlong unsigned short unsigned long 3 float doublelong double

CS115 FALL Senem KUMOVA-METİN7 1. Characters and the char Data Type ( 1/4)  “char” is a 1-byte (1byte= 8 bits)  2 8= 256 distinct values Mostly used for representing characters Can also be used to represent also integers !!  signed char : have values between -128 and 127 ( in a two's complement machine)  unsigned char : have values between 0 and 255  Printable characters are always positive !!

CS115 FALL Senem KUMOVA-METİN8 Some char constant and their integer values (ASCII Table in appendix D ) Character constant ‘a’ ‘b’ ‘c’ … ‘z’ Corresponding value … 112 Character constant ‘A’ ‘B’ ‘C’ … ‘Z’ Corresponding value … 90 Character constant ‘0’ ‘1’ ‘2’ … ‘9’ Corresponding value … 57 Character constant ‘&’ ‘*’ ’+’ Corresponding value Characters and the char Data Type ( 2/4)

CS115 FALL Senem KUMOVA-METİN9 1. Characters and the char Data Type (3/4)

CS115 FALL Senem KUMOVA-METİN10 EXAMPLE: char c = 'a'; /* ASCII code for 'a' is ( ) 2 = (97) 10 */ printf("%c", c); /* a is printed */ printf("%d", c); /* 97 is printed */ printf("%c%c%c",c,c+1,c+2); /* abc is printed */  HOW CAN WE CONVERT a CHAR FROM LOWER CASE TO UPPER CASE??? 1. Characters and the char Data Type (4/4)

CS115 FALL Senem KUMOVA-METİN11 FUNDAMENTAL DATA TYPES 1 character char signed char unsigned char 2 integer shortintlong unsigned short unsigned long 3 float doublelong double

CS115 FALL Senem KUMOVA-METİN12  Represents integer values  Typically an int is stored in 2 bytes (16 bits) or in 4 bytes (32 bits)  Use sizeof() function 2. The Data Type int (1/4) #include main() {int x=350000; printf("x= %d\n”, x); printf("sizeof(x)= %d\n", sizeof(x));}

CS115 FALL Senem KUMOVA-METİN13 EXAMPLE : IF a computer has 4 bytes for int THEN There are 2 32 distinct states for integers Half of the states can be used for positive numbers, the other half for negative numbers -2 31, , …, -2, -1, 0, 1, 2, …, In this machine integers can vary between -2(~-2 31 )billion to +2 (~ ) billion IF an integer is assigned a value greater than 2 billions or less than -2 billions an integer overflow occurs.. 2. The Data Type int (2/4)

CS115 FALL Senem KUMOVA-METİN14 2. The Data Type int (3/4)  long  4 bytes (2 32 distinct values)  short  2 bytes (2 16 distinct values)  int  On machines with 4byte words int has the same size with long On machines with 2byte words int has the same size with short  unsigned  has no sign (always positive and has the same size with int)

CS115 FALL Senem KUMOVA-METİN15  Suffixes are used to specify type of integer  EXAMPLE long int x = 37L; unsigned long int y= 37UL; 2. The Data Type int (4/4)

CS115 FALL Senem KUMOVA-METİN16 FUNDAMENTAL DATA TYPES 1 character char signed char unsigned char 2 integer shortintlong unsigned short unsigned long 3 float doublelong double

CS115 FALL Senem KUMOVA-METİN17 3. The floating data types (1/4)  3 floating types : float (mostly 4 bytes) double (mostly 8 bytes) long double  Some examples f = e-2F /* float */ 0e0 /* floating point zero 0.0 of type double */ 1. /* double 1.0 */ /* double */ SuffixTypeExample f or Ffloat3.7F l or L long double3.7L

CS115 FALL Senem KUMOVA-METİN e-10 = * Floating point constant parts for e-10 IntegerFractionExponent e The floating data types (2/4)

CS115 FALL Senem KUMOVA-METİN19  Precision : Number of significant decimal places a floating value carries  Range : Limits of the largest & smallest positive floating values that can be represented by a variable of that type EXAMPLE : IF precision is 6 and range is to for type X THEN X can be 0.d 1 d 2 d 3 d 4 d 5 d 6 * 10 n and -38 < = n <=38 3. The floating data types (3/4)

CS115 FALL Senem KUMOVA-METİN20  For double floating numbers : precision = 6, range = to d 1 d 2 d 3 d 4 d 5 d 6 * 10 n and -38 < = n <=38  For long floating numbers : precision = 15, range = to d 1 d 2 d 3 ….d 15 * 10 n and -308 < = n <= The floating data types (4/4)

CS115 FALL Senem KUMOVA-METİN21 The use of typedef  Programmer can explicitly associate a type with an identifier #include main() { typedef char uppercase;/* associate type char with identifier “uppercase” */ uppercase u='A'; /* declare character “u” */ typedef int myint;/* associate type int with identifier “myint” */ myint x =90; /* declare integer “myint” */ printf("%c %d \n", u, x);}

CS115 FALL Senem KUMOVA-METİN22 The sizeof operator  sizeof(object)  Returns an integer that represent the number of bytes needed to store an object in memory  EXAMPLE: int x=4; printf(“%d”, sizeof(x)); printf(“%d”, sizeof(x+3));  Check what sizeof() will return for int, double, char, unsigned … etc. ????

CS115 FALL Senem KUMOVA-METİN23 getchar( ) & putchar( ) (1/2)  Macros defined in “stdio.h” that are used to read characters from keyboard and to print characters on screen #include int main(void) { int c; if((c = getchar())!= EOF) /* scanf(“%c”, &c);*/ { putchar(c); /* printf(“%c”, c);*/ putchar(c); } return 0; /* EOF = end-of-file =-1 */ }

CS115 FALL Senem KUMOVA-METİN24 getchar( ) & putchar( ) (2/2) #include int main(void) { int c; while((c = getchar())!=EOF)/* scanf(“%c”, &c);*/ { putchar(c); /* printf(“%c”, c);*/ putchar(c); } return 0; /* EOF = end-of-file =-1 */ /* EOF  CTRL +Z }

CS115 FALL Senem KUMOVA-METİN25 Mathematical functions, Printing tricks math.h  sqrt(), pow(), exp(),log(), sin() … #include int main(void) { double x = ; printf("x=%12.4e\n", x); /* 4 digits for fraction part, totally 12 characters*/ printf("square root of x=%10.1e", sqrt(x)); /* 1 digits for fraction part, totally 10 characters*/ } OUTPUT : x= e+004 square root of x= 2.0e+002

CS115 FALL Senem KUMOVA-METİN26 Arithmetic Conversions : Review any_type op long double -> long double any_type op double -> double  int x; double y;  (x+y)  double any_type op float -> float any_type op unsigned long -> unsigned long …… Check the table (pg.133)

CS115 FALL Senem KUMOVA-METİN27 CASTS : Forces explicit converions # include void main(void) { int x=3, y=10; double divv; divv=10/3; printf("10/3 = %f\n", divv); divv =y/x; printf("y/x = %f\n", divv); divv=(double)10/3; //FORCE TO STORE 10/3 IN DOUBLE printf("(double)10/3 = %f\n", divv);} OUTPUT :10/3 = y/x = (double)10/3 =

CS115 FALL Senem KUMOVA-METİN28 Hexadecimal and Octal constants (1/4) hexadecimal digit 123… decimal value 123…9ABCDEF Octal digit decimal value

CS115 FALL Senem KUMOVA-METİN29 EXAMPLE : A0F3C = A * * F * * C * 16 0 = 10 * * * * * 16 0 = Hexadecimal and Octal constants (2/4)

CS115 FALL Senem KUMOVA-METİN30  In C source code, positive integer constants prefaced with 0 are integers in octal notation 0x are integers in hexadecimal notation  In printf() function %d --- decimals %o --- octals %x --- hexadecimals Hexadecimal and Octal constants (3/4)

CS115 FALL Senem KUMOVA-METİN31 #include void main(void) { int x=056; int y= 0xA01 printf("%d \n", x); //46 printf("%o \n", x); //56 printf("%x \n\n", x); //2e printf("%d \n", y);//2561 printf("%o \n", y);//5001 printf("%x \n", y);//a01 } Hexadecimal and Octal constants (4/4)

CS115 FALL Senem KUMOVA-METİN32 Exercise Write a program that prints the values of each digit in a 3-digit integer number. Sample output: input a positive integer number: 376 first digit of the number is: 3 second digit of the number is: 7 third digit of the number is: 6