Summary of C Programming
Summary of C Programming
C Programming Summary
void main()
{
enum { red_led_on = 1, green_led_on, both_leds_on };
OR
OR
Try to create a good programming
while (condition) habit to always include the { } though
statement; the while loop only consists of single
statement.
www.utm.my innovative ● entrepreneurial ● global 34
34
Do…While loop
do
{
statement1;
statement2;
…..
} while (condition);
OR
Equal with:
init;
while (condition)
{
statement1;
statement2;
…..
update;
}
(integer) a
Function definition:
return_type function_name (type arg1, type arg2, …type argn)
{
statement1;
statement2;
…….
statementn;
return (value);
}
p = &a
char *p; 0x1002 0x78 b
0x1003 0x8A
char a, b;
0x1004 0x10 *p
0x1005 0x00
p = &a;
BEFORE
b = *p;
*p = 5; Memory Address Memory (RAM) Variables
0x1000 0x05 (*p = 5) a
0x1001 0x36
p = &a
0x1002 0x35 (b = *p) b
0x1003 0x8A
0x1004 0x10 *p
0x1005 0x00
www.utm.my AFTER
innovative ● entrepreneurial ● global 53
53
Pointers
Pointers are effectively addresses, they offer the ability to
move, copy, and change memory in all sorts of ways.
int *iptr; // a pointer point to a integer variable
long *lptr; // a pointer point to a long variable
int block_A[2];
block_B [0] [0] [1]
int block_B[2][2];
int block_C[3][2][2]; [1] [0] [1]
block_C [0] [0] [1] [0] [0] [1] [0] [0] [1]
location_x = widget[23].bin.x;
location_y = widget[23].bin.y;
my_space.long_one = 0x12345678L;
my_space.character = 0x12;
my_space.integer = 0x1234;
www.utm.my innovative ● entrepreneurial ● global 70
70
Unions
Used as a method of preserving valuable memory space.
Define a “scratch pad” area of memory: If there are
variables that are used on a temporary basis, and there is
never a chance that they will be used at the same time
Used a s method of extracting smaller parts of data from a
larger data object
The data could be byte order swapped, word order
swapped, or both.
Could be used as a test for a compiler in order to find out
data organization in memory (big-endian or little endian)