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

Similar documents
Number Systems, Scalar Types, and Input and Output

ESC101N Fundamentals of Computing

Programming Language A

Data Type Fall 2014 Jinkyu Jeong

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

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

BSM540 Basics of C Language

Types, Operators and Expressions

Types, Operators and Expressions

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

Chapter 7. Basic Types

Programming. Elementary Concepts

Work relative to other classes

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

CSC 1107: Structured Programming

Basic Types and Formatted I/O

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

Types, Variables, and Constants

THE FUNDAMENTAL DATA TYPES

Programming in C++ 5. Integral data types

COMP327 Mobile Computing Session: Tutorial 2 - Operators and Control Flow

PROGRAMMAZIONE I A.A. 2017/2018

Fundamental of Programming (C)

Chapter 3 Basic Data Types. Lecture 3 1

BSM540 Basics of C Language

A Fast Review of C Essentials Part I

Internal representation. Bitwise operators

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Internal representation. Bitwise operators

Internal representation. Bitwise operators

Programming and Data Structures

CS1100 Introduction to Programming

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

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

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Fundamentals of Programming

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

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

Programming in C Quick Start! Biostatistics 615 Lecture 4

Computer System and programming in C

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -STRUCTURE- SPRING 2015 SEON-JU AHN, CNU EE

First of all, it is a variable, just like other variables you studied

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

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

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

BLM2031 Structured Programming. Zeyneb KURT

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Chapter 3. Fundamental Data Types

Binary Representations and Arithmetic

CSE 1320 INTERMEDIATE PROGRAMMING - OVERVIEW AND DATA TYPES

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

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

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

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

Engineering Computing I

Lecture 03 Bits, Bytes and Data Types

Computers Programming Course 5. Iulian Năstac

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Lecture 02 C FUNDAMENTALS

C: How to Program. Week /Mar/05

Lecture 3. More About C

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

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

IT 1033: Fundamentals of Programming Data types & variables

Chapter 2 - Introduction to C Programming

l l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers

Should you know scanf and printf?

EL2310 Scientific Programming

2. Numbers In, Numbers Out

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

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

Data Types. Data Types. Integer Types. Signed Integers

Differentiate Between Keywords and Identifiers

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

Syntax and Variables

CS102: Variables and Expressions

Computer Programming 3 th Week Variables, constant, and expressions

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:

Fundamentals of C Programming

Data Types and Variables in C language

The New C Standard (Excerpted material)

ET156 Introduction to C Programming

Reserved Words and Identifiers

Applied C and C++ Programming

int main() { return 0; }

Fundamentals of C. Structure of a C Program

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

Lesson 3 Introduction to Programming in C

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

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

Programming in C with CCATSL

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

3. Types of Algorithmic and Program Instructions

2. Numbers In, Numbers Out

Unit 4. Input/Output Functions

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS)

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Introduction to C. Systems Programming Concepts

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

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

Transcription:

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

COMMENT Comment Comments are usually added with the purpose of making the source code easier to understand Those annotations are potentially significant to programmers but typically ignorable to compilers /* This is a comment */ /* These are multiple line comments */ // This is a one-line comment /* This is a comment /* This is another comment*/ */ (X) /* This is a comment // This is a one-line comment */ (O) 1

VARIABLE Variable( 변수 ) Location in memory where a value can be stored for use by a program Variable naming rules Names are made up of letters and digits; the first character must be a letter The underscore _ counts as a letter Don't begin variable names with underscore, however, since library routines often use such names. Upper and lower case letters are distinct, so x and X are two different names Traditional C practice is to use lower case for variable names, and all upper case for symbolic constants 2

VARIABLE It's wise to choose variable names that are related to the purpose of the variable We tend to use short names for local variables, especially loop indices, and longer names for external variables <Wrong naming examples and reasons> Examples int 7th_val Reasons begin with a number int live_inthe# Special character like # int kor year Space between variable names 3

KEYWORDS Keywords( 예약어 ) These words have special meaning to the C compiler You can t use them as variable names They must be in lower case <Keywords in C> 4

VARIABLE DECLARATIONS Declarations( 변수선언 ) All variables must be declared before use A declaration specifies a type, and contains a list of one or more variables of that type int char lower, upper, step; c, line[1000]; int lower; int upper; int step; char c; char line[1000]; 5

VARIABLE DECLARATIONS Initialization( 초기화 ) A variable may also be initialized in its declaration. If the name is followed by an equals sign and an expression, the expression serves as an initializer char esc = '\\'; int i = 0; int limit = MAXLINE+1; float eps = 1.0e-5; Variables should be declared at the head of each block 6

VARIABLE DECLARATIONS #include <stdio.h> int main(void) { int a, b; a = 010; b = 0x1a; int c = 3, d = 4; printf("%d %d \n", a, b); printf("%d %d \n", c, d); return 0; } Error 7

CONST const The keyword const can be applied to the declaration of any variable to specify that its value will not be changed #include <stdio.h> int main(void) { const double RATE = 0.056; const int MIN_BALANCE = 1000; RATE = 0.05; Error printf("rate is: %f\n", RATE); printf("minimum Balance is : %d\n", MIN_BALANCE); return 0; } 8

DATA TYPES Data Types in C( 자료형 ) Character type char : a single byte, capable of holding one character in the local character set Integer type: int : an integer, typically reflecting the natural size of integers on the host machine Floating point data type float : single-precision floating point double : double-precision floating point 9

INTEGER TYPE Integer variable keywords short, int, long signed, unsigned 10

INTEGER TYPE signed vs. unsigned [0, positive, negative] vs. [0, positive] Keyword Size Range signed short 2 bytes -32768(-2 15 ) ~ 32767(2 15-1) signed int 4 bytes -2147483648(-2 31 ) ~ 2147483647(2 31-1) signed long 4 bytes -2147483648(-2 31 ) ~ 2147483647(2 31-1) unsigned short 2 bytes 0~65535(2 16-1) unsigned int 4 bytes 0~4294967295(2 32-1) unsigned long 4 bytes 0~4294967295(2 32-1) 11

INTEGER TYPE Two's complement system Negating a number (whether negative or positive) is done by inverting all the bits and then adding 1 to that result sign bit 12

INTEGER CONSTANTS Integer Constant Integer Constant is a decimal (base 10), octal (base 8), or hexadecimal (base 16) number that represents an integral value. Use integer constants to represent integer values that cannot be changed. decimal-constant : 1 2 3 4 5 6 7 8 9 octal-digit : begins with 0 : 010 = 8 hexadecimal-digit : begins with 0x or 0X : 0x1a = 26 unsigned-suffix : u or U long-suffix : l or L unsigned long-suffix : ul or UL 13

FLOATING POINT DATA TYPE Floating Point Data Type float, double, long double 14

CHARACTER TYPE Character Type char, signed char, unsigned char ASCII Code American Standard Code for Information Interchange 15

ASCII CODE TABLE 16

CHARACTER TYPE #include <stdio.h> int main(void) { char c1 = 'a'; char c2 = 65; //65 = A char c3 = 0x5a; //0x5a = 90 = Z printf(" 저장값 ( 문자 ): %c %c %c\n", c1, c2, c3); printf(" 저장값 ( 숫자 ): %d %d %d\n", c1, c2, c3); } 17

DATA TYPE SIZE The unary operator sizeof is used to calculate the sizes of datatypes, in number of bytes sizeof (datatype keyword) sizeof variable name, sizeof (variable name) sizeof constant, sizeof (constant) #include <stdio.h> int main(void) { char c1 = 'A'; printf("size of char: %d %d\n", sizeof (char), sizeof c1); printf("size of integer: %d %d\n", sizeof (int), sizeof 10); printf("size of float: %d %d\n", sizeof 3.14f, sizeof (4.56)); } 18

DATA TYPE SIZE limits.h : define maximum and minimum number of character and integer type float.h : define maximum and minimum number of floating point data type #include <stdio.h> #include <limits.h> #include <float.h> int main(void) { printf("char 범위 : %d %d\n", CHAR_MIN, CHAR_MAX); printf("unsigned char 범위 : %d %d\n", 0, UCHAR_MAX); printf("int 범위 : %d %d\n", INT_MIN, INT_MAX); printf("unsigned short 범위 : %d %u\n", 0, USHRT_MAX); printf("float 범위 : %.10e %.10e\n", FLT_MIN, FLT_MAX); printf("double 범위 : %.10e %.10e\n", DBL_MIN, DBL_MAX); } 19

FUNCTION PRINTF() Format-Control Character(Format specifier) A format specifier starts with the character `%' and ends with a format-control letter; it tells the printf statement how to output one item. printf( %~ %~, a, b); 20

FUNCTION PRINTF() main() { double x = 1234.56789012; } printf("%f\n",x); printf("%10f\n",x); printf("%10.2f\n",x); printf("%-10.2f\n",x); printf("%+10.2f\n",x); printf("%10.2e\n",x); 21

FUNCTION SCANF() scanf() : scanf function reads from the standard input, which is usually the keyboard scanf( %~, &variable) format control character : %d, %c, %f, %lf d : integer, c : character, f : float, lf : double & : address operator The ampersand(&), when combined with the variable name, tells scanf the location (or address) in memory at which the variable is stored. The computer then stores the value for the variable at that location 22