110 likes | 188 Views
Unix Continuum of Tools. Do something once: use the command line Do something many times: Use an alias Use a shell script Do something that is complex of with large amounts of data: Use a C program Examples Command: pipes and redirection to search files
E N D
Unix Continuum of Tools • Do something once: use the command line • Do something many times: • Use an alias • Use a shell script • Do something that is complex of with large amounts of data: Use a C program • Examples • Command: pipes and redirection to search files • Shell script: apply a particular kind of filter using different parameters at different times • C program: create a simulation program
C Advantages and Disadvantages • Advantages • Super fast • Concise set of syntax • Portable: all systems can compile c • Very popular • Disadvantages • Not object oriented meaning that procedures and functions are written to operate on data constructs
History • 1972 • Dennis Ritchie creates the c language • The classic book by Kernigham and Ritchie defines the syntax for this language. • 1990 ANSI C with some minor extensions • 1999 ANSI C with some more minor extensions • 1979 • C++ which is C with classes • Additional enhancements since then • 1990s: Java • programming language based on C++ syntax • 2002: C# • .net language with characteristics of Java and C++ Note:If you know Java, you know lots of C ("Just Like Java" or JLJ)
Making a C Program • Create your program >vi hello.c • Complile and link • The standard UNIX command: >cc <file> • The GNU command: >gcc <file> • Flags • -o <file> creates an executable named <file> instead of a.out • -g create symbolic information for the gdb debugger • -l <library> include a run time library • -L <path> search path for libraries • Debug: >gdb executable (we will cover this later) • To run: just type <executable> or ./<executable> if . is not in the search path
Development Cycle • Design and code • Editor to enter the code: program.c • Compile: program.o • Link: program • If link errors, return to step 2 • Execute program • If logic errors return to step 1 Note: Steps 3 and 4 is normally done by gcc in one step
/* hello.c prints "hello world" April 29, 2002: Kevin Sahr */ #include <stdio.h> int main(int argc, char* argv[]) { printf("hello world\n"); return 0; } /* main */ /* to */ are comments include merges source from stdio.h main function runs first return 0 tells shell result ok, return # indicates an error printf is like java println, but with more features the * is used to reference an array called argv argc is the number of command line arguments argv[0] has the program name, command line arguments are in argv[1] .. argv[n] { and } are JLJ Hello World Program JLJ: each statement ends with ;
C Program Structure • Comments • At top: File name, date, author, purpose • Throughout: clarify code as needed • Preprocessor directives • C programs run through a preprocessor phase first • This phase: Include other source files, perform modifications to the source file • Preprocessor directives do not end with ; • Include the main function • Include other functions (Subject for a later week)
Comments • C comments are /* … */ • Do not nest comments • In labs, include the name of the file, your name, and date at the top of every file you submit • Other comments will be optional, but use when the code is not easy to follow • The // format were introduced in C++ • They presently are not part of C • They are proposed for the next version of C
Header files: stdio.h and stdlib.h • Includes: analogous to Java Includes • stdio.h is a text file containing prototypes for stream based I/O functions • stdlib.h another text file containing prototypes to the C "standard library" • Prototypes: • analogous to Java Interfaces • function bodies completed when linking
main() function • main() • Every C program must have a main function • Analogous to Java "static void main(String[] args)" • Syntax: int main(int argc, char *argv[]) • argc: number of arguments • argv: an array of argument strings • return statement returns to the caller • main() function returns to the shell • return 0; tells the shell that the run was successful • return #; returns an error code to the shell
Converting String Data • Commonly used functions • Ascii to integer: atoi(string); • Ascii to float: atof(string); • String to double: strtod(char *start, char *end); • Example Int main(intargc, char *args[]) { int x = atoi(args[1]); double y = atof(args[2]); double z = strtod(args[2], NULL); // entire string }