Presentation is loading. Please wait.

Presentation is loading. Please wait.

Minimal standard C program int main(void) { return 0 ; }

Similar presentations


Presentation on theme: "Minimal standard C program int main(void) { return 0 ; }"— 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 (int argc, 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 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 – 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 \n New line \t Horizontal tab \r Line feed \' Single quote \" Double quote \\ Backslash And a few more….

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 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)


Download ppt "Minimal standard C program int main(void) { return 0 ; }"

Similar presentations


Ads by Google