Aims and Objectives
Data Types and Sizes
Constants
More on Declarations
Arithmetic, Relational and Logical Operators
Type Conversions
Bitwise Operations
char - single character (8 bits)
int - typ. 16 bits (8 or 32)
float - typ. 32 bits (16)
double - typ. 32 bits (16)
Long and Short Integers and doubles
Example of size reporting program
Signed and Unsigned Characters and Integers
Integer Constants
#define PI 3.1417F /* float constant */
#define BIG 12345678UL /* unsigned long int */
Character Constants
'x', '\n', etc.
'\677' Octal
'\xFF' Hex
String Constants
const float pi = 3.1417;
const extern float pi;
Arithmetic Operators
Relational Operators
i < lim - 1;
Logical Operators
if x = 1, !x = 0
int x = 0; /* binary x */
if (!x) {...} /* equivalent to unless (x) */
if (x == 0) {...}
The conditionals are equivalent, but the former is more succinct.
2 + 5 = ?
2.0 + 5 = ?
2 + 5.0 = ?
2.0 + 5.0 = ?
++nletters[c - 'a']
c >= '0' && c <= '9'
can be replaced by
isdigit(c);
On some machines, if a signed char is converted to an int it will be converted to a negative number (if the left most bit is a 1). On others, leading zeros are added to the char so that it can never be a negative int. ANSI C guarantees that printable characters will always be positive, but for other chars ....
a > b
if ((c = getchar()) != EOF) {...}
c = getchar() /* c ASCII value of char input */
c != EOF /* 1 if true, 0 if false (if c==EOF)*/
while (isdigit(c = getchar()) && c != EOF) {...}
c = getchar() /* same as before */
isdigit(...) /* 0 if false, else anything */
c != EOF /* as before */
... && ... /* 1 if both 1, 0 otherwise */
Forcing Type Conversion
for (i = 0; i < 26; ++i) { c = 'a' + i; putchar(c); printf(" = %d\n", nletters[i]); }
a = 2
b = 2
c = 2...
printf("%c = %d\n", c, nletters[i]);
printf("%c = %d\n",(char) 'a' + i,nletters[i]);
& bitwise AND
| bitwise inclusive OR
^ bitwise exclusive OR
<< left shift
>> right shift
~ one's complement
/* x = 01010101 (85, U) */
/* y = 01101101 (109, m) */
n = x & y /* = 11000111 (199, -71, <<, [[pi]]) */
n = x | y /* = 01111101 (125, }) */
n = x ^ y /* = 00111000 */
n = x << 2 /* = 01010100 (84, overflow!
340 otherwise (*4))*/
n = x >> 2 /* = 00010101 (21, (/4) )*/
n = ~x /* = 10101010 */