Keywords:
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Identifier:
The name for a variable, function, etc.
Sample Identifiers:
i0, j1, abc, stu_score, __st__, data_t, MAXOF, MINOF ...
Identifier Naming Style:
- Separate words with `_' or capitalize the first character
- Use all UPPERCASE for symbolic constant, macro definitions, etc
- Be consistent
- Be meaningful
Rules for identifiers:
- a-z, A-Z, 0-9, and _
- Case sensitive
- The first character must be a letter or _
- Keywords are reserved words, and may not be used as identifiers
Types:
Range of Basic Types
char 1 bytes -128 to 127
unsigned char 1 bytes 0 to 255
short 2 bytes -32768 to 32767
unsigned short 2 bytes 0 to 65535
int 4 bytes -2147483648 to 2147483647
unsigned int 4 bytes 0 to 4294967295
long 4 bytes -2147483648 to 2147483647
unsigned long 4 bytes 0 to 4294967295
float 4 bytes 1.175494e-38 to 3.402823e+38
double 8 bytes 2.225074e-308 to 1.797693e+308
Sample Constants
char 'A', 'B'
int 123, -1, 2147483647, 040 (octal), 0xab (hexadecimal)
unsigned int 123u, 2107433648, 040U (octal), 0X02 (hexadecimal)
long 123L, 0x1FFFl (hexadecimal)
unsigned long 123ul, 0777UL (octal)
float 1.23F, 3.14e+0f
double 1.23, 2.718281828
long double 1.23L, 9.99E-9L
- Special characters: \\ (backslash), \n (newline), \t (tab), \', \", ...
Strings or Character Arrays:
char line[6]="ABCDE";
char *s = "ABCDE";
Representation:
Variables Declarations
- Purpose:
define a variable before it is used.
- Format:
type identifier [, identifier] ;
Each identifier can also be assigned (initialized) with a value.
int i, j, k;
char a, b, c;
int i = 123;
float f = 3.1415926535;
double f = 3.1415926535;
char s[5] = "ABCD", t[5] = "BCD";
- Type conversion:
aka, type casting, Coercion, be very cautious.
(type) identifier;
int i = 258;
char a; /* range -128 to 127 */
a = (char) i; /* What is the value of a? */
Basic IO
- Character based: getchar(), putchar(c).
- Formatted: scantf(), printf();
Format Specifiers:
d,i int signed decimal integer
x, X int unsigned hex (no leading 0x or 0X)
u int unsigned decimal integer
c int single character
s char * characters from a string
f double floating point [-]dddd.pppp
p void * pointer
- Sample program: Basic IO
Last updated:
Mon Apr 4 07:17:11 EDT 2005