1 / 64

CC510 Beginning of C Programming

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.

sauda
Download Presentation

CC510 Beginning of C Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CC510Beginning of C Programming Park, KyungMi kmpark@bulsai.kaist.ac.kr

  2. Contents • Using Visual Studio • Basic C Programming

  3. USING VISUAL STUDIO

  4. 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

  5. Start Page

  6. Create Project (1/5)

  7. Create Project (2/5)

  8. Create Project (3/5)

  9. Create Project (4/5)

  10. Create Project (5/5)

  11. Create Files (1/3)

  12. Create Files (2/3)

  13. Create Files (3/3)

  14. Programming

  15. Build

  16. Execute

  17. Results

  18. BASIC C PROGRAMMING

  19. 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

  20. 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

  21. 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

  22. 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

  23. From Source Code to Executable File Source Code Source File Text Editor Compiler Library Object File Executable File Linker Build

  24. 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

  25. 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)

  26. 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;

  27. 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 */

  28. 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

  29. 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 */

  30. Escape Sequence

  31. 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 */

  32. 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’.

  33. 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;

  34. 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: ";

  35. 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"); }

  36. 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;

  37. Arithmetic Conversion Rules double float long unsigned int char, short

  38. Conversion Rule: example char c; double d; float f; int i; long l; short s; unsigned u;

  39. 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)

  40. 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; }

  41. 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 */

  42. 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");

  43. 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; }

  44. 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); }

  45. 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 */ }

  46. 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;

  47. 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 */

  48. 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 */

  49. 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); }

  50. 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);

More Related