The Format of C
- Statements are terminated with semicolons
- Indentation is ignored by the compiler like Comment
- C is case sensitive - all keywords and Standard Library functions are lowercase
- Strings are placed in double quotes
- Newlines are handled via \n
- Programs are capable of flagging success or error, those forgetting to do so have one or other chosen randomly!
Another Example
here int keyword is used to create integer variables. here two variables a and b are created.
Variables
- Variables must be declared before use immediately after begin “{“.
- valid characters are letters, digits and ”_”.
- First character cannot be a digit.
- 31 characters recognised for local variables (more can be used, but are ignored).
- Some implementations recognise only 6 characters in global variables (and function names)!.
- Upper and lower case letters are distinct.
printf and scanf
- printf writes integer values to screen when %i is used.
- scanf reads integer values from the keyboard when %i is used.
- “&“ VERY important with scanf (required to change the parameter, this will be investigated later) - absence will make program very ill.
- “&“ not necessary with printf because current value of parameter is used.
Integer Types in C
- C supports different kinds of integers
- maxima and minima defined in “limits.h”
type format bytes
char %c 1
signed char %c 1
unsigned char %c 1
short [int] %hi 2
unsigned short %hu 2
int %i 2
unsigned int %u 2or4
long [int] %li 4
unsigned long %lu 4
Integer Example
Output
minimum int = -32768, maximum int = 32767
maximum unsigned = 65535
maximum long int = 2147483647
maximum unsigned long = 4294967295
Integer With Different Bases
It is possible to work in octal (base 8) and hexadecimal (base 16)
Output
dec=20, oct=16. hex=32
dec=20, oct=20, hex=20
Named Constants
Named constants may be created using const
Preprocessor Constants
Named constants may also be created using the Preprocessor
Needs to be in “search and replace” mode
Historically these constants consist of capital letters