640 likes | 841 Views
CC510 Beginning of C Programming. Park, KyungMi kmpark@bulsai.kaist.ac.kr. Contents. Using Visual Studio Basic C Programming. USING VISUAL STUDIO. VISUAL STUDIO. Microsoft Visual Studio is a commercial integrated development environment (IDE) for the C.
E N D
CC510Beginning of C Programming Park, KyungMi kmpark@bulsai.kaist.ac.kr
Contents • Using Visual Studio • Basic C Programming
VISUAL STUDIO Microsoft Visual Studio is a commercial integrated development environment (IDE) for the C. An integrated development environment (IDE) is a software application that provides comprehensive facilities to computer programmers for software development. Visual Studio 2010 is provided on KAIST S/W homepage http://kftp.kaist.ac.kr Wikipedia
Definitions • Computer programming (often shortened to programming or coding) is the process of writing, testing, debugging/troubleshooting, and maintaining the source code of computer programs. • Computer programs (also software programs, or just programs) are instructions for a computer. • C is a general-purpose computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system. Wikipedia
Program Design • Five phases in developing a program • Specify the problem • Analyze the problem • Design a method of solution (algorithm) • Coding (using programming lang.) • Test the program
C History • 1970 : K Tompson wrote B for the first UNIX system on the PDP-7 • 1972 : C was designed as an extension of B • 1973 : UNIX OS was written in C over 90% • 1976 - 1977 : UNIX was ported to VAX • BCPL -> B -> C • S/Wused in UNIX is almost written in C
Genealogy of High-Level Language FORTRAN I FLOW-MATIC ALGOL 58 COMTRAN 1957 1960 1965 1970 1975 1980 1885 1990 FORTRAN II ALGOL 60 FORTRAN IV COBOL LISP BASIC CPL ALGOL-W PL/I BCPL ALGOL 68 B Pascal Prolog C FORTRAN 77 Smalltalk 80 Ada C++ ANSI C FORTRAN 90 Ada 95
From Source Code to Executable File Source Code Source File Text Editor Compiler Library Object File Executable File Linker Build
Sample Program #include <stdio.h> #define SCOPE 10 void main() { int x, y; /* Variables */ int sum; x = 10; y = 200; sum = SCOPE * x + y; printf(“Sum = %d\n”, sum); /* print function */ } Sum = 300
Name, Variables, Declarations • "identifier" : variable name • letters, digits, _, • First character : letter or _. • Long name with _ : help readability of program. (up to 31 characters) • Upper and lower case letters are distinct. • Variable name : lower case • Symbolic constant : upper case • Key words are reserved. (lower case only)
Data Types and Sizes • Char : a single byte(=8 bit), one character • int : integer ( 32 bit long in general ) • float : single-precision floating point precision: 6 digits, range: 10^(-38~+38) • Double: double-precision floating point precision: 12 digits, range: 10^(-308~+308) • short, long: specifying the size of integer short int sint; short sint; long int lint; long counter; int cntr;
Unsigned vs. Signed • unsigned int vs. signed int : range of value • unsigned char vs. signed char • Signed integer : 1 bit for the sign of the number • the rest for the magnitude of the number • Unsigned integer use all the bits for the magnitude, non-negative. unsigned short int x; /* 0 ~ 2^16-1 */ short int xx; /* -2^15 ~ 2^15-1 */ signed int y; /* -2^31 ~ 2^31-1 */ signed char zz; /* -128 ~ +127 */
Data Types and Values • Numeric data types : int, float, double ...... • Single ASCII character : char (also integer) Ex) 'A' '7' '+' 'a' '‘ • int • int e.g. 1234 938 -392 • long int : 1234567890123L 29438l /* L or l */ • unsigned int: 2132U 332u /* u or U */ • octal representation of integer: begin with 0 Ex) 0177 034 02222222222L 09932U • hexadecimal representation of ingeter: 0X or 0x Ex) 0X3ff 0x23 0X2B 0XFUL
Data Types and Values II • double : either an int, a decimal fraction, an exponent part or their combination (but not integer) Ex) -5.3e-3 1.0 1. .023 3.14159 314.159e-2 314158e-5 0.00314158e+3 0.00314158e3 0.00314158e003 0.0 0e0 .0e0 • Special notation for characters : Ex) '\007' /* ascii bell character */ '\0' /* null character */
String Constant (1/2) • String constant or string literal • A sequence of zero or more characters surrounded by “” Ex) "This is string" "Thisisstring" "This is\talso string" "This is string\n" "" /* empty string */
String Constant (2/2) • String constants can be concatenated at compile time Ex) "hello," " world" --- "hello, world" • String constant is an array of character terminated with Null character ’\0’.
Class declaration • Variables must be declared before use • class (storage class) • automatic (default within functions) • extern (global) • static • register • variable type • short, long, int, float, unsigned char, etc. • identifier_list • a list of variable names, separated by commas whose values are to be of the designed type and class. class type identifier_list;
Declaration: examples float x, sum, av; int n; static unsigned long int real_long, rl; • Initialization of variables at the DECLARATION TIME (must be a constant expression) • qualifier const #define MAXLINE 1000 char esc = '\\'; int i = 0; int limit = MAXLINE+1 /* constant expression*/ float eps = 1.0e-5 const double e = 2.71828182845905; const char msg[] = "warning: ";
Size of Data Types: program #include <stdio.h> /* compute the size of the fundamental types */ main() { printf("\n char: %d bytes", sizeof( char)); printf("\n short: %d bytes", sizeof( short)); printf("\n int: %d bytes", sizeof( int)); printf("\n long: %d bytes", sizeof( long)); printf("\n unsigned: %d bytes", sizeof(unsigned)); printf("\n float: %d bytes", sizeof( float)); printf("\n double: %d bytes", sizeof( double)); printf("\n\n"); }
Arithmetic Operators • binary arithmetic operators: • + - * / % (modulus operator) • relational and logical operators: • > >= < <= == != int year; if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) printf(%d is a leap year\n", year); else printf(%d is not a leap year\n", year); for (i=0; i<lim-1; i++) s[i] = c;
Arithmetic Conversion Rules double float long unsigned int char, short
Conversion Rule: example char c; double d; float f; int i; long l; short s; unsigned u;
Increment and Decrement • ++ (increment: add 1) • -- (decrement: substract 1) • ++n -> increment n before using its value • n++ -> increment n after its value has been used • only applied to variables (not to expresson or const)
Assignment Operators • compress form i = i + 2; i += 2; /* assignment operator */ • expr1 op= expr2 expr1 = (expr1) op (expr2) • op : + - * / % << >> & ^ | // x *= y + 1 means x = x * (y+1) rather than x = x*y + 1 /* bitcount: count 1 bits in x */ int bitcount(unsigned x) { int b; for (b = 0; x != 0; x >>=1) b++; return b; }
Statement and Block • Expression: combination of var, const and operators • statement : expression + semicolon • Block: { series of statement} if (x == 0) x = 0; /* expression & statement */ x = 0; i++; printf("%d %d\n", i,j); /*statement */ { x = 0; i++; printf("%d %d\n", i,j); } /* block */
Ambiguity of If-Then-Else • Part of if statement is optional : problem with nested if statements if (n > 0) { if (a > b) z = a; } else z = b; if (n >= 0) for ( i = 0; i < n; i++) if (s[i] > 0) { printf("..."); return i; } else /* WRONG */ printf("error --n is negative-n");
Else-If : multiple choice /* count blanks, digits, letters, newlines, and others */ #include <stdio.h> main() { int c, blank_cnt, digit_cnt, letter_cnt, nl_cnt, other_cnt; blank_cnt=digit_cnt=letter_cnt=nl_cnt=other_cnt = 0; while ((c = getchar()) != EOF) /* brace not necessary */ if (c == ' ') ++blank_cnt; else if ('0' <= c && c <= '9') ++digit_cnt; else if ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') ++letter_cnt; else if ( c == '\n') ++nl_cnt; else ++other_cnt; }
While • While ( expression ) : While expression is evaluated true(non zero), repeat statement. while (i++ < n) factorial = factorial * i; while ((c = getchar()) != EOF) { if ('a' <= c && c <= 'z') ++lower_case_letter_cnt; ++total_cnt; } while (++i < LIMIT) { j = 2 * i + 3; printf("\n%d", j); }
While : continued • repeat statement zero or more times. • Control is passed to the next statement of while when expression is false (zero). • If expression is zero at first, skip while loop. int n; printf("\ninput an integer: "); scanf("%d", &n); while (--n) { ........ /* do something */ } /* for positive integer OK, but for negative ....*/ while (--n > 0) { /* do something */ }
FOR for ( init_expr; test_expr; update_expr) statement next_statement ... int i, sum; sum = 0; for (i = 1; i <= 10; ++i) /* say evaluation seq */ sum += i;
FOR : examples for (i = 1; i <= n; ++i) factorial *= i; for (j = 2; k % j == 0; ++j) { printf("\n%d is a divisor of %d", j, k); sum += j; } i = 0; sum = 0; for ( ; i <= 10; ++i) sum += i; i = 1; sum = 0; for ( ; i <= 10 ; ) sum += i++; for ( ; ; ) ; /* loop forever */
COMMA operator • expression1, expression2 • evaluate expression1, and then expression2, left to right, value of operation is value of expression2 • comma operator : extends the flexibility of for loop • allowing you to include initialize, test or update expression in a for loop sum = 0, i = 1 for ( sum = 0, i = 1; i <= n; ++i) sum += i; for ( sum = 0, i = 1; i <= n; sum += i, ++i) ; /* empty statement */
COMMA and FOR #include <stdio.h> main() { int even_sum, odd_sum; int cnt, j, k, n; scanf("%d", &n); /* get the number */ even_sum = odd_sum = 0; for (cnt = 0, j = 2, k = 1; cnt < n; ++cnt, j +=2, k += 2) { even_sum += j; odd_sum += k; } printf("%7d%7d\n", even_sum, odd_sum); }
Do while statement : exit condition loop • do statement while (expression); • evaluate statement first and then evaluate expression • If expression is true (nonzero) , repeat statement. • If expression is false (zero), control passes to next statement. do { printf("\n\ninput a positive integer: "); scanf("%d", &n); } while (n <= 0);