Download presentation
Presentation is loading. Please wait.
1
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
2
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
3
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)
4
Making a C Program Create your program >vi hello.c Complile and link –The standard UNIX command: >cc –The GNU command: >gcc –Flags -o creates an executable named instead of a.out -g create symbolic information for the gdb debugger -l include a run time library -L search path for libraries Debug: >gdb executable (we will cover this later) To run: just type or./ if. is not in the search path
5
Development Cycle 1.Design and code 2.Editor to enter the code: program.c 3.Compile: program.o 4.Link: program 5.If link errors, return to step 2 6.Execute program 7.If logic errors return to step 1 Note: Steps 3 and 4 is normally done by gcc in one step
6
Hello World Program /* hello.c prints "hello world" April 29, 2002: Kevin Sahr */ #include int main(int argc, char* argv[]) { printf("hello world\n"); return 0; } /* main */ 1./* to */ are comments 2.include merges source from stdio.h 3.main function runs first 4.return 0 tells shell result ok, return # indicates an error 5.printf is like java println, but with more features 6.the * is used to reference an array called argv 7.argc is the number of command line arguments 8.argv[0] has the program name, command line arguments are in argv[1].. argv[n] 9.{ and } are JLJ JLJ: each statement ends with ;
7
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)
8
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
9
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
10
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
11
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(int argc, char *args[]) { int x = atoi(args[1]); double y = atof(args[2]); double z = strtod(args[2], NULL); // entire string }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.