1 / 16

Minimal standard C program

Minimal standard C program. int main(void) { return 0 ; }. Function header. int main(void) Return type Integer according to ANSI C standard Return 0, for success Or better yet, return EXIT_SUCCESS Return non-zero, otherwise Name of function Argument types void means no arguments

nemo
Download Presentation

Minimal standard C program

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. Minimal standard C program int main(void) { return 0 ; }

  2. Function header int main(void) • Return type • Integer according to ANSI C standard • Return 0, for success • Or better yet, return EXIT_SUCCESS • Return non-zero, otherwise • Name of function • Argument types • void means no arguments • More later on (intargc, char *argv[])

  3. C block • Delimited by “curlies” – { } • Encloses C statements (more later) • Starts with variable declarations (more later) • C90 and C++ are more forgiving • Can be nested • Function code must be in a block

  4. Minimal C program with I/O #include <stdio.h> int main(void) { printf("Hello World\n") ; return 0 ; }

  5. Standard libraries • C has many “standard” libraries • But they really aren’t part of the “language” • C preprocessor (cpp) • Processes lines starting with # • And some other things • #include <stdio.h> • Adds ~900 lines of code needed to use standard I/O • cpp -E prog.c • Shows what is added

  6. Printing a line • printf("3 feet is 36 inches\n") ; • Writes a line to standard output • 3 built-in FILE’s of C • Standard output (stdout) • Normal terminal output • Standard error (stderr) • Terminal output for errors • Standard input (stdin) • Terminal input

  7. Escape sequences Allows inclusion of special characters

  8. Formatted I/O • Allows the pretty printing of variable data • Easy to get started • But it takes time to master • Inspired by FORTRAN I/O • So it enables printing of aligned tables • printf("Easy as %8.4f\n", PI) ; • Easy as __3.1416

  9. Variables • Names and places for data • Global variables • Declared outside of blocks • Local variables • Declared at beginning of blocks • In K&R and ANSI C • Stored on the stack

  10. Variable names • Can contain • Letters (upper and lower) • Digits • Underscore • Cannot start with a digit • Cannot be a keyword • Like return

  11. Variable declarations • Type in C • Integer, floating point, character, … • Determines space needed for variable • Determines what kind of add to do • Integer or IEEE floating? • Declaration statements • int lengthInFeet ; • int x, y, Y, z ;

  12. Assignment statements • Gives a value to a variable • variable = expression ; • For our example int feet, inches ; feet = 10 ; inches = 12*feet ;

  13. The whole program #include <stdio.h> int main(void) { int feet, inches ; feet = 5 ; inches = 12*feet ; printf("% d feet is %d inches\n", feet, inches) ; return 0 ; }

  14. Things that go wrong • Syntactic error • Your program isn’t in legal C • inches = 12 feet ; • Semantic error • Your program has a bug • inches = feet/12 ;

  15. Debuggers • Programs to “step” through your code • Linux gcc has gdb • Must compile with debugging symbols • gcc -g …….. • Many features • Set breakpoint • Examine variables • Hard to master • But worth the effort to learn a little

  16. Phases • Compiling • Preprocessor • Expands #include statements • Lexical analysis • Finds the tokens (or words) • Syntactic analysis • Finds the expressions, statements, blocks, etc. • Code generation • Linking • Adds in the library routines • Loading • Processes DLL’s and relocation (if needed)

More Related