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 (int argc, 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 Worldn") ;
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 inchesn") ;
– 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
n New line
t Horizontal tab
r Line feed
' Single quote
" Double quote
 Backslash
And a few more….
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.4fn", 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 inchesn",
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)

Minimal standard c program

  • 1.
    Minimal standard Cprogram 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 • Delimitedby “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 programwith I/O #include <stdio.h> int main(void) { printf("Hello Worldn") ; return 0 ; }
  • 5.
    Standard libraries • Chas 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 inchesn") ; – 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 inclusionof special characters n New line t Horizontal tab r Line feed ' Single quote " Double quote Backslash And a few more….
  • 8.
    Formatted I/O • Allowsthe 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.4fn", PI) ; – Easy as ____3.1416
  • 9.
    Variables • Names andplaces 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 • Cancontain – Letters (upper and lower) – Digits – Underscore • Cannot start with a digit • Cannot be a keyword – Like return
  • 11.
    Variable declarations • Typein 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 • Givesa 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 inchesn", feet, inches) ; return 0 ; }
  • 14.
    Things that gowrong • 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)