공학프로그래밍언어 (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