1Outline• Administrative trivia• Goals of the class• Introduction to C2Compiling, interpreting, and running3The C Programming Language• Systems programming language Originally used to write Unix and Unix tools Data types and control structures close to most machines Now also a popular application programming language• Pros and cons Can do whatever you want: flexible and efficient Can do whatever you want: can shoot yourself in the foot• Notable features All functions are call-by-value Pointer (address) arithmetic Simple scope structure I/O and memory management facilities provided by libraries• History BCPL B C K&R C ANSI C1960 1970 1972 1978 1988 LISP Smalltalk C++ Java 4Java vs. C• Abstraction C exposes the raw machine Java hides a lot of it• Bad things you can do in C that you can’t do in Java Shoot yourself in the foot (safety) Others shoot you in the foot (security) Ignoring wounds (error handling)• Dangerous things you have to do in C that you don’t in Java memory management: malloc/free• Good things that you can do in C but you might not; Java makesyou objected-oriented methodology• Good things that you can’t do in C but you can in Java portability5Java vs. CRunCompileProgram% a.out% Hello, world%% java hello% Hello, world%% gcc hello.c% ls% a.out hello.c%% javac hello.java% ls% hello.java hello.class%hello.c:#include <stdio.h>main() {printf(“Hello, world\n”);}hello.java:public class hello {public static voidmain(String[] args) {System.out.println(“Hello, world”);}} CJAVA6Java vs. C, cont’dint A[10];float B[5][20];int [] A = new int [10];float [][] B = new float [5][20];Arrays/* no run-time check */// run-time checkingBound checkchar /* 8 bits */char // 16-bit unicodeChar typevoid// no equivalentVoid typeintbooleanBoolean#define MAX 1000final int MAX = 1000;ConstantFloating point typesInteger typesfloatdoublefloat // 32 bitsdouble // 64 bitsshortintlongbyte // 8 bitsshort // 16 bitsint // 32 bitslong // 64 bitsCJAVA7Java vs. C, cont’d#include <string.h>strcat( s1, s2 );s1 + s2String concatenate+, -, *, /, %, unary -+, -, *, /, %, unary -Arithmetic>>, <<, &, |, ^>>, <<, >>>, &, |, ^Bit-wise opsint *p;// no pointerPointer typestruct r {int x;float y;}class r {int x;float y;}Record type=, !=, >, <, >=, <==, !=, >, <, >=, <=CompareLogical String type&&, ||, !&&, ||, !char *s1 = “Hello”;char s2[6];strcpy( s2, “hello” );String s1 = “Hello”;String s2 = newString( “hello” ); CJAVA8Java vs. C, cont’dreturn 5;return 5;Function returnFoo( x, y, z );Foo( x, y, z );Function / procedure call{statement1;statement2;}{statement1;statement2;}BlockProcedurereturnAssignmentsCommentsreturn;return; =, *=, /=, +=, -=, <<=, >>=, =, ^=, |=, %==, *=, /=, +=, -=, <<=, >>=, >>>=, =, ^=, |=, %=/* comments *//* comments */// another kindCJAVA9Java vs. C, cont’dgoto L;// no equivalent“goto”switch (n) {case 1: ...break; case 2: ...break; default:...}switch (n) {case 1: ...break; case 2: ...break; default:...}Switch/* no equivalent */throw, try-catch-finallyExceptionConditionalif (expression)statement1elsestatement2;if (expression)statement1elsestatement2;CJAVA10Java vs. C, cont’dcontinue;continue;Terminate a loop bodywhile (expression)statement;while (expression)statement;“while” loopTerminate a loop“do- while” loop“for” loopbreak;break;do {statement;…} while (expression)do {statement;…} while (expression)int i;for (i=0; i<10; i++)statement;for (int i=0;i<10;i++)statement;CJAVA11Standard I/O• Three standard I/O streams stdin stdout Stderr• Binding Flexible/dynamic binding of streams to actual devices or files Default binding– stdin bound to keyboard– stdout and stderr bound to the terminal screenstdinprogramstdoutstderr12Standard I/O in C• Three standard I/O streams stdin stdout stderr• Basic calls for standard I/O int getchar(void); int putchar(int c); int puts(const char *s); char *gets(char *s);• Use “man” pages% man getchar#include <stdio.h>main() {int c;c = getchar();while (c != EOF) {putchar(c);c = getchar();}}% a.out < file1 > file2% a.out < file1 | a.out > file2% a.out < file1 | a.out | a.out > file2copyfile.c:13pipestdina.outstdoutstderrstdina.outstdoutstderr% a.out < file1 | a.out > file214What’s all this good for?• In the old days: hard-code input/output devices into programs• Hard to program • and very hard to port to different input/output devices15What’s all this good for?• Along came Unix (early 1970s)• First OS to have complete features of standard I/O redirection and pipes• Standard I/O redirection Write program once Same program can be made to work for different input/output devices at run time• Good practice of modularitystdinprogramstdoutstderr16What’s all this good for?• Pipes Write small programs that specialize in very simple tasks Connect lots of smaller programs to make bigger programs Makes bigger programs easier to write Earliest and best success story of programming with components• Standard I/O redirection and pipes: big part of Unix success• Good practice of modularity is a learned artstdinprogram1stdoutstderrstdinprogram2stdoutstderr17Formatted Output: printf•int printf(char *format, ...); Translate arguments into characters according to “format” Output the formatted string to stdout• Conversions (read “man printf” for more) %d −−−− integer %f −−−− float %lf −−−− double %3f −−−− float with 3 decimal places %% −−−−percent• Examples int x = 217;printf( “Course number is: %d”, x );18Formatted Input: scanf•int scanf(const char *format, ...); Read characters from stdin Interpret them according to “format” and put them into the arguments• Conversions (read “man scanf” for more) %d −−−− integer %f −−−− float %lf −−−− double %% −−−− literal %• Example double v;scanf( “%lf”, &v ); int day, month, year;scanf( “%d/%d/%d”, &month, &day, &year);19Standard Error Handing: stderr•stderr is the second output stream for output errors• Some functions to use stderr int fprintf(FILE *stream, const char *format, ...);– Same as printf except the file stream int fputc(int c, FILE *stream);– putc() is the same as
View Full Document