160 likes | 256 Views
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
E N D
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 • More later on (intargc, char *argv[])
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
Minimal C program with I/O #include <stdio.h> int main(void) { printf("Hello World\n") ; return 0 ; }
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
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
Escape sequences Allows inclusion of special characters
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
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
Variable names • Can contain • Letters (upper and lower) • Digits • Underscore • Cannot start with a digit • Cannot be a keyword • Like return
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 ;
Assignment statements • Gives a value to a variable • variable = expression ; • For our example int feet, inches ; feet = 10 ; inches = 12*feet ;
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 ; }
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 ;
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
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)