프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
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 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
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/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Fundamental data types
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
The fundamental data type 제 4 주 강의. Declarations, Expressions, and Assignments Declaring the type of a variable  set an appropriate amount of space in.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
1 Review of Chapter 6: The Fundamental Data Types.
Introduction to programming Language C, Data Types, Variables, Constants. Basics of C –Every program consists of one or more functions and must have main.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Chapter 3 Expressions and Interactivity Department of Computer Science Missouri State Univeristy.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
OPERATORS.
CISC105 – General Computer Science Class 9 – 07/03/2006.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Fundamental Data Types, Operators and Expressions Kernighan/Ritchie: Kelley/Pohl: Chapter 2 Chapter 2, 3.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
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.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Introduction to Programming Lecture 4: Calculations.
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.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
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
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 1.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
Variables Symbol representing a place to store information
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
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.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
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
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 INTRODUCTION Data Types and Arithmetic Calculations.
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Variables, Operators, and Expressions
Lecture 3 Expressions, Type Conversion, Math and String
Arithmetic Expressions
CSCE 206 Structured Programming in C
The Machine Model Memory
Tokens in C Keywords Identifiers Constants
7. BASIC TYPES.
Functions, Part 2 of 2 Topics Functions That Return a Value
Relational Operations
C Short Overview Lembit Jürimägi.
Fundamental Data Types
Data Type.
Conversions of the type of the value of an expression
Data Type.
Lecture 8.
Chapter-3 Operators.
Lectures on Numerical Methods
Basic Types Chapter 7 Copyright © 2008 W. W. Norton & Company.
Introduction to Programming
Fundamental Data Types
Summary of what we learned yesterday
Data Type.
Variables and Constants
Presentation transcript:

프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

Chapter 6 The Fundamental Data Types 2 Contents  Declarations and Expressions  The Fundamental Data Types  Characters and the Data Type char  The Data Type int  The Integral Types short, long, and unsigned  The Floating Types  The sizeof Operator  Mathematical Functions  Conversions and Casts

Chapter 6 The Fundamental Data Types 3 Declarations and Expression  Declarations –C 에서 모든 variable 은 사용 전에 declaration 되어야 한다. – 선언에 의해 compiler 가 variable 을 위한 memory 를 할당. –compiler 가 정확한 instruction 을 수행하도록 한다. [Ex] int month; int type 의 value 를 저장하기 위한 memory(4B) 를 할당 => ‘month’ 에 의해 참조 [Ex] int a=1, b=2, c; float d=1.0, e=2.0, f; c=a + b; d=e + f; int + int 연산이므로 결과가 int 가 됨 float + float 연산이므로 결과가 float 가 됨

Chapter 6 The Fundamental Data Types 4 Declarations and Expression  Expression –Expression 은 variables, constants, function call 로 이루어진다. – 모든 Expression 은 산출 결과인 value 와 그 value 의 type 을 갖 는다. –Expression 의 type 은 구성 요소인 constants, variable, function return type 에 의해 결정된다. [Ex] int a=1, b=2; float d=1.0, e=2.0; a + b;/* type : int, value : 3 */ e + f; /* type : float, value : 3.0 */ ;/* type : double, value : 5.0 실수형의 constant 는 double */

Chapter 6 The Fundamental Data Types 5 The Fundamental Data Types  C 에서 제공하는 Data Types – 모든 type 은 keyword 이다. (identifier 로 사용할 수 없다.) –‘signed’ keyword 는 생략 가능. –short int, long int, unsigned int 에서 int 의 생략 가능 : short, long, unsigned 로 사용 Fundamental data types charsigned charunsigned char signed short intsigned intsigned long int unsigned short intunsigned intunsigned long int floatdoublelong double [Ex] ‘int’ 와 ‘signed int’ 는 같은 의미

Chapter 6 The Fundamental Data Types 6 The Fundamental Data Types  기능별로 분류 Fundamental types grouped by functionality Integral typescharsigned charunsigned char shortintlong unsigned shortunsignedunsigned long Floating typesfloatdoublelong double Arithmetic typesIntegral types + Floating types

Chapter 6 The Fundamental Data Types 7 Characters and the Data Type char  Characters – 모든 integral type 의 변수는 character 의 저장 가능 – 한 character 를 저장하기 위해 int 또는 char 로 선언. –Character Constant 는 ASCII code 값으로 전환되므로 int 와 호환 가능하다. [Ex1]int c; c= ‘A’+5;/* ‘A’ 의 ASCII code : 65 */ printf(“%c %d\n”, c, c); [Ex2]c= ‘A’; printf(“%c %d %c %d\n”, c, c, ++c, c); [Ex3]for(c= ‘A’; c<= ‘Z’; c++) printf(“%c ”,c); F 70 A 65 B 66 A B C D…X Y Z

Chapter 6 The Fundamental Data Types 8 Characters and the Data Type char  The Data Type char –C 에서 char type 은 1byte(8bit) 의 메모리를 할당. –8bit 는 2 8 = 256 개의 character 를 표현할 수 있다. –char type 의 값의 범위 char(signed char) : -128(-2 7 ) ~ 127(2 7 -1) unsigned char : 0 ~ 255 [Ex] char c = ‘a’ ; /* ‘a’  97 => (2) */ 실제 메모리에 저장되는 형태

Chapter 6 The Fundamental Data Types 9 The Data Type int  The Data Type int –machine 의 word size 에 따라 할당되는 메모리 size 결정 2bytes machine : (-2 15 ) ~ 32767( ) 4bytes machine : (-2 31 ) ~ ( ) [Ex] #include #define BIG int main(void) { int a, b=BIG, c=BIG; a = b + c; printf("a=%d\n", a); return 0; } Overflow 발생 : int type 이 표현할 수 있는 최대값보다 큰 값이므로 정확한 값이 저장될 수 없다. a=

Chapter 6 The Fundamental Data Types 10 The Integral Types – short, long, unsigned  short –4bytes machine 일 경우 int 보다 적은 메모리 사용 (2bytes) –range : (-2 15 ) ~ 32767( ) – 메모리 size 가 문제되는 경우 사용  long –2bytes machine 일 경우 int 보다 더 큰 수를 표현 할 수 있다. (4bytes 메모리 사용 ) –4bytes machine : (-2 31 ) ~ ( )

Chapter 6 The Fundamental Data Types 11 The Integral Types  unsigned – 양의 정수 만을 저장 –unsigned 의 값의 범위 (0 ~ 2 wordsize -1) 2bytes : 0 ~ 65535( ) 4bytes : 0~ ( )  suffixes – 특별한 type 의 integral constant 만들기 위하여 사용 combining long and unsigned SuffixTypeExample u or Uunsigned37U l or Llong37L ul or ULunsigned long37UL

Chapter 6 The Fundamental Data Types 12 The Floating Types  float, double, long double – 실수 형의 data 저장 –integral type 과 달리 정확한 값이 아닌 근사값의 표현 –suffix 가 없는 floating constant type 은 double 이다. –Exponential Notation combining long and unsigned SuffixTypeExample f or Ffloat3.7F l or Llong double3.7L [Ex] e5 = x 10 5 integer : 1 fraction : exponent : 5

Chapter 6 The Fundamental Data Types 13 The Floating Types  float –4bytes 의 메모리 할당 – 유효숫자 6 자리의 정확도 – 값의 범위 : ~  double –8bytes 의 메모리 할당 – 유효숫자 15 자리의 정확도 – 값의 범위 : ~ [Ex] double a = ; /* 20 digits */ 실제 메모리에 저장되는 데이터 a = e3 /* 15digts */

Chapter 6 The Fundamental Data Types 14 The sizeof Operator  The sizeof Operator –variable 에 할당된 메모리 size, type 에 따른 메모리 size 를 byte 단위로 표현 –unary operator 의 precedence, associativity 를 갖는다. –value type 은 unsigned int 이다. [Ex] int i,j; sizeof(char);/* 1 */ sizeof(int); /* 4 */ sizeof(45);/* 4 */ sizeof(4.5);/* 8 */ sizeof(i); /* 4 */ sizeof(int j);/* 4 */ integer Constant double Constant type : unsigned

Chapter 6 The Fundamental Data Types 15 Mathematical Functions  Mathematical Functions –sqrt(), pow(), exp(), log(), sin(), cos(), tan() 등 …. –Header file “math.h” 의 삽입 필요 : Function Prototypes 존재 – 모든 math function 은 double type 의 argument 와 double type 의 return value 를 갖는다. –Compile 시 math library 를 link 하기 위해 “-lm” 을 옵션으로 주 어야 한다. [Ex] cc filename.c -lm [Ex] double sin(double); /* 모든 angle 은 radian 으로 표시 */ [Ex] double pow(double, double);

Chapter 6 The Fundamental Data Types 16 Mathematical Functions [Ex]#include #include #define PI int main() { double r = PI / (2*90); /* 1 도에 해당하는 radian 값 */ int i; for(i=0; i<=90; i+=5) { printf("cos(%d) = %f\t", i, cos(r*i)); printf("sin(%d) = %f\t", i, sin(r*i)); printf("tan(%d) = %f\n", i, tan(r*i)); } cos(0) = sin(0) = tan(0) = … cos(45) = sin(45) = tan(45) = … cos(90) = sin(90) = tan(90) =

Chapter 6 The Fundamental Data Types 17 Conversions and Casts  The Integral Promotion – 모든 integral type 은 expression 에서 자동 int 로 convert 된다. [Ex] short + short → int char + char → int unsigned short + unsigned short → unsigned [Ex] char c = ‘a’; printf(“%d, %c\n”, c, c); Char 를 %d format 으로 출력 시 자동 int 로 convert 97, a

Chapter 6 The Fundamental Data Types 18  Type Promotion in Expression –Expression 에서 다른 type 의 operands 사용 시 자동 largest operand type 으로 convert Conversions and Casts float  double  long double int  unsigned int  long int  unsigned long int int 와 double 의 혼용  전부 double 로 convert [Ex] int a=1, b=2; double x=3.0, z; float y=4.0; z = a + b + x + y; /* 모든 operands 의 type 이 double 로 convert 되어 계산됨 */

Chapter 6 The Fundamental Data Types 19 Conversions and Casts  Conversion during Assignment –Assignment statement 에서 right side 는 left side 의 type 으로 자동 convert [Ex]int i; double d=3.7 i = d;/* i = 3 */ d = i;/* d = 3.0 */ left side 가 int type 이므로 3.7 이 int type 으로 convert => 소수점 이하 자리는 잘림. left side 가 double type 이므로, double type 인 3.0 으로 convert int = char;/* int 로 변환 */ double = int;/* double 변환 */ int = float;/* int 로 변환 */...

Chapter 6 The Fundamental Data Types 20 Conversions and Casts  Casts –expression 에서 operand 의 type 을 convert –(type 명 )expression [Ex1] int a=3, b=2; double c = a / b; printf(“c=%f\n”, c); [Ex2] int a=3, b=2; double c = (double) a / b; printf(“c=%f\n”, c); c= c= a,b 모두 int 이므로 (int/int) 의 결과는 int 이며 left side type 인 double type 으로 convert. 3/2 = 1 ==> 1.0 이 c 에 assign 됨 a 는 double 로 cast 되어 (dobule/int) expression 이 되고 int 가 double 로 promotion 된다. (double/double) 의 결과는 double 3.0 / 2.0 = 1.5

Chapter 6 The Fundamental Data Types 21 Conversions and Casts [Ex3] int a=3, b=2; double c = (double) (a / b); printf(“c=%f\n”, c); c= a, b 모두 int 이므로 (int/int) 의 결과는 int. 3/2 = 1 그리고 그 결과를 double 로 cast 하므로 1.0 이 c 에 assign 됨

수고하셨습니다 The Fundamental Data Types