1 / 19

CS113 Introduction to C

CS113 Introduction to C. Instructor: Ioannis A. Vetsikas E-mail: vetsikas@cs.cornell.edu. Administrative Issues. Note: All of this information is on web site! Office hours: TBA Prerequisite: CS100 or equivalent Assignments once a week (4 total), due on Friday

roz
Télécharger la présentation

CS113 Introduction to C

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. CS113Introduction to C Instructor: Ioannis A. VetsikasE-mail: vetsikas@cs.cornell.edu

  2. Administrative Issues • Note: All of this information is on web site! • Office hours: TBA • Prerequisite: CS100 or equivalent • Assignments once a week (4 total), due on Friday • Turn in printout of program + email it to me • I might give quizzes • Grades on Assignments: On a scale from 0-4. 4 = Perfect, 3=Very good, 2 = Good, 1 = Incomplete and/or significant errors, 0=no effort at all • You get an S if all assignments are completed with score of >= 2, and decent performance on quizzes • Failing to complete any assignment will probably result in a U. • If you have a problem and cannot complete the assignment on time let me know; I will allow one late assignment at most

  3. More Course Details • Textbooks: I recommend purchasing Kernighan & Ritchie • Kelley&Pohl optional • Compiler: Codewarrior is available on most campus lab, but any compiler will do • I’ll be using “gcc” for compiling your programs, but any correct ANSI C program will run on any compiler! • Collaboration: You may discuss programs with others, but all code submitted must be your own.

  4. Why learn C? • “Least common denominator” – good building block for learning other languages • Subset of C++, similar to Java • Closeness to machine allows one to learn about system-level details (e.g., C only has call-by-value) • Portable – compilers available for most any platform! • Very fast (almost as fast as assembly) • C/C++ are languages of choice for most programmers

  5. The canonical “first C program” #include <stdio.h> void main() { printf( “Hello, world!\n” ); } • All programs run from the ‘main’ function • ‘printf’ is a function in the library “stdio.h” • To include library functions use “#include” • All programs use library functions

  6. That wasn’t too hard, let’s try another! #include <stdio.h> void main() { int x = 1, y; int sum; y = 3; sum = x + y; /* adds x to y, places value in variable sum */ printf( “%d plus %d is %d\n”, x, y, sum ); }

  7. Comments • Any string of symbols placed between the delimiters /* and */. • Can span multiple lines • Can’t be nested! Be careful. • /* /* /* Hi */ is an example of a comment. • /* Hi */ */ is going to generate a parse error Keywords • Reserved words that cannot be used as variable names • OK within comments . . . • Examples: break, if, else, do, for, while, int, void • Exhaustive list in book

  8. Identifiers • A “token” (“word”) composed of a sequence of letters, digits, and underscore (“_”) character. (NO spaces.) • First character cannot be a digit • C is case sensitive, so beware (e.g. printfPrintf) • Used to give names to variables, functions, etc. • Identifiers such as “printf” normally would not be redefined; be careful • Only the first 31 characters matter Constants • 0, 77, 3.14 examples. • Strings: double quotes. “Hello” • Characters: single quotes. ‘a’ , ‘z’ • Have types implicitly associated with them… • 1234567890999 too large for most machines…

  9. Simple Data Types • void • Integer types (signed or unsigned): char, short int, int, long int • char is an 8 bit (=1 byte) number • Floating-point types: float, double, long double • No boolean types • Use 0=False and anything else(usually 1)=True

  10. Operators and “Punctuators” • These are special characters with particular meanings, potentially depending on context • + - * / are the usual arithmetic operators, and can be applied to integer types as well as floating-number types • % is “mod operator”: a % b is equal to the remainder when a is divided by b, and returns a value in the range 0…(b – 1). We won’t worry about what happens when either a or b is non-positive. • % character has different meaning in printf( “%d”, a ); • Punctuators include parentheses, braces, semicolons…

  11. Initializers • int j=1; • char c=‘a’; • Declare type of variable and assign a value to it Assignment • a=b=c=1; • Same as a=(b=(c=1)); • The whole expression c=1 has value equal to 1 • The whole expression b=c=1 has value equal to 1 • Also a=b=c=d+1; • Whole expression c=d+1 has value d+1 etc. • But cannot write a=b=c+1=d+1 • Left hand side of any assignment must be “lvalue”; c + 1 is not a valid lvalue

  12. Compound Statement { definitions-and-declarations (optional) Statement-list } • Used for grouping as function body and to restrict identifier visibility • Note: no semicolon after closing bracet • But every statement in C must be followed by ;

  13. Summing the numbers 1 thru 10. #include <stdio.h> void main() { int i = 1, sum = 0; while( i <= 10 ) { sum = sum + i; i = i + 1; } printf( "Sum = %d\n", sum ); } for( i = 1; i <= 10; i++ ) { sum = sum + i; } Alternative to while loop! Better, I think.

  14. for and while • for (init; cond; expr1) expr2; equivalent to: init; while (cond) {expr2; expr1;} • Init and expr1 can be any number of assignments while expr2 can be any statement Some special operators provided by C • Instead of writing i = i+1 one can write i += 1 • In general var op= expr means var = var op expr • Especially for var += 1 and var -= 1 one can write: var++ and var-- respectively

  15. ++ , the tricky guy int a = 10; printf( “%d”, a++ ); int a = 10; printf( “%d”, ++a ); • Two variants: before a variable and after. • I prefer personally not to mix in ++ or -- as part of a more complicated expression • Another complication: int a = 10; printf( “%d %d”, a++, a++ ); • This complication not limited to the ++, -- operators, but occur with any operator with side effects

  16. Precedence of Operators • You may have learned about this in the third grade: • 1 + 2 * 3 has the value of 1 + (2 * 3) • If we want the addition to be performed first, must parenthesize: (1 + 2) * 3. • We say that * has a higher precedence than +. • See K&R page 53. Associativity of Operators • Question number two: What about operators at the same precedence level? For instance, * and / ? • Is 12 / 6 * 2 equal to (12 / 6) * 2, or 12 / (6 * 2) ? • It’s the first: these operators are left associative (as are most) • Moral of story: I say parenthesize when in doubt.

  17. Input – Output #include <stdio.h> void main() { int x = 7, y; char id; printf( “Enter a letter, by which I will identify you\n” ); scanf( “%c”, &id ); printf( “Now enter a number!\n” ); scanf( “%d”, &y ); printf( “%c, if nothing else, I know this:” , id); printf( “%d times %d is %d\n”, x, y, x*y ); }

  18. Input & Output functions • Use printf, putchar etc. to write to the screen • Use scanf, getchar etc. to read from the keyboard or console • More on this on Monday

  19. Your Homework • Covered most of chapters 1&2 of K&R and some of chapter 3. • Write a “hello world” program and some other simple programs in the programming environment which you plan to use to make sure that you are comfortable • Start looking at the homework; most of the knowledge you need to complete it has been presented (the rest on Monday)

More Related