Comments
/* */ comment: possibly over multiple lines
// comment: ignore rest of line
Libraries
#include <stdio.h> load the system library called stdio
Main Program
int main(void)
{
statements;
....
return 0;
}
- main program block
- gets nothing (void) from operating system
- returns an integer (int) to operating system
- in this case a 0 is returned
printf Function
printf("format string",variable list);
- displays formatted data as specified by format string between inverted commas
- % corresponds to a variable in the variable list whose contents are to be displayed at that position
- % indicates a format specifier giving type and other information for corresponding variable
- type: given by following table
| Format Type Specifier | Variable Type | Displayed As |
| d or i | int | decimal (base 10) |
| o | unsigned int | octal (base 8) |
| u | unsigned int | decimal (base 10) |
| x or X | unsigned int | hexadecimal (base 16) |
| f or F | float or double | decimal point but no exponent |
| e or E | float or double | decimal point and exponent |
| g or G | float or double | uses exponent if needed |
| a or A | float or double | hexadecimal (base 16) floating point |
| c | int or char | character |
| s | array of char's | character string |
| p | pointer | address in hexadecimal |
| n | integer | store number of characters appearing so far |
-size: given by following table
| Format Size Specifier | Size |
| h | short |
| l | long |
| ll | long long |
| hh | (convert char to int code) |
-precision:
-width:
-flags:
-special characters:
| Special Character | |
| %% | percent sign |
| \n | carriage return |
| \t | tab |
| \0 | end of string |
getchar Function
getchar();
- gets single char from user
- returns the actual character obtained
scanf Function
scanf("format string",variable list);
- reads a string from the user in a given format
- returns number of variables successfully filled with data matching format string
- use ampersands in front of variable names unless using arrays
- a % in the format string corresponds to a pointer to a variable in the variable list which can store data of the specified type
- % indicates a format specifier
- type: as for printf except
- width: maximum number of characters to be read
- * do not record data input for this variable
Arrays
type name[size];
- set aside size locations in memory, each of type type and set name to be a pointer to the first such location