|
COMPUTER | |
|
||
C Core
|
Operators
%[
flags] [width] [.precision] [prefix] type
Type |
Meaning |
%c |
character |
%s |
string |
%d |
signed integer (decimal) |
%f |
double (float) - decimal notation - form "[ – ]dddd.dddd" |
%u |
unsigned integer |
%X |
unsigned hexadecimal integer - form " ABCD ..." |
%p |
pointer to void - form "SSSSOOOO" - segment, offset in uppercase hexa |
%e |
double - scientific notation - form "[ – ]d.dddd e [sign]ddd" |
%g |
f or e type by decision , e is used if the exponent > 4 or <-4, zeros are truncated, the decimal point appears only when some digits folow it |
Flag |
Meaning |
Default |
0 |
zeros adding until the defined width |
no prefix zeros |
- |
the left align within the defined width |
right align |
+ |
the prefix of the output value with sign +/- |
only negative values sign ' -' |
' ' |
(space) positive values will be prefixed with ' ' |
no space |
Width |
Meaning |
1..XXX |
minimum printed characters. If the number of characters is less, spaces are added. If the number of characters is greater, all characters are printed (no truncated). |
* |
the width specification will be supplied by argument value - variable |
Precision |
Type - Meaning |
1..XXX |
f - the number of digits after the decimal point. The last digit is rounded. d - the minimum number of digits. If the number of digits is less, zeros are added to the left s - the maximum number of characters. If the string is longer, it's TRUNCATED. |
* |
the width specification will be supplied by argument value - variable |
Prefixes are only Microsoft specific, not ANSI compatible.
Prefix |
Type |
Meaning |
l |
d,u |
long int |
h |
d,u |
short int |
Esc seq |
Meaning |
\n |
new line |
\r |
carriage return |
\f |
form feed |
\t |
tab |
\' |
single quotation |
\" |
double quotation |
\\ |
backslash |
\? |
question mark |
\xhhh |
ASCII char in hexa notation |
\a |
Alert = Bell |
Type Name |
Bytes |
Other Names |
Range of Values |
int |
* |
signed, signed int |
System dependent |
unsigned int |
* |
unsigned |
System dependent |
char |
1 |
signed char |
-128 to 127 |
unsigned char |
1 |
none |
0 to 255 |
short |
2 |
short int signed short int |
-32,768 to 32,767 |
unsigned short |
2 |
unsigned short int |
0 to 65,535 |
long |
4 |
long int signed long int |
-2,147,483,648 to 2,147,483,647 |
unsigned long |
4 |
unsigned long int |
0 to 4,294,967,295 |
enum |
2 |
none |
-32,768 to 32,767 |
float |
4 |
none |
3.4E +/- 38 (7 digits) |
double |
8 |
none |
1.7E +/- 308 (15 digits) |
long double |
10 |
none |
1.2E +/- 4932 (19 digits) |
* (pointer) |
4 |
none |
seg 0 to 65,535 : offset 0 to 65,535 |
type |
Initialization |
char |
'A' |
string |
"text" |
integer |
1234u or U - unsigned 1234l or L - long |
hexadecimal |
0x2F |
float |
12.34f or F -25E-4 = -0.0025 |
long double |
12.34l or L |
array |
[2][3]={{1,2,3},{4,5,6}}; [2][10]={"John","Michael"}; |
struct |
struct guns pistol={"Uzi",30,7}; |
Priority |
MASTER type |
SLAVE type |
SLAVE conversion |
1 |
long double |
any |
long double |
2 |
double |
any |
double |
3 |
float |
any |
float |
4 |
unsigned long |
any integer |
unsigned long |
5 |
long |
unsigned int |
unsigned long |
6 |
long |
any integer |
long |
7 |
unsigned int |
any integer |
unsigned int |
8 |
any integer |
any integer |
int |
Operator |
Meaning |
== |
equal |
!= |
not equal |
> |
greater than |
< |
less than |
>= |
gtreater or equal than |
<= |
less or equal than |
Operator |
Meaning |
&& |
AND |
|| |
OR |
! |
NOT |
TRUE != 0 FALSE == 0
Operator |
Meaning |
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise XOR |
<< |
Left shift |
>> |
Right shift |
~ |
Negation all bits |
Operator |
Meaning |
(type) |
Type cast x=(int)y; |
sizeof |
Size of object x=sizeof(y); |
% |
Modulus - rest after integer dividing. x=10%3; /* x==1 */ |
. |
Structure (record) member rec.mem |
-> |
Pointer to structure member |
, |
Sequential evaluation |
Symbol |
Type of Operation |
Annotations |
() [] –> . |
Expression |
-> pointer to structure member |
! ~ ++ –– sizeof (type) & * + – |
Unary |
! NOT (logical) ~ negation all bits (bitwise) (type) type cast & address of an object * pointer |
* / % |
Multiplicative |
% modulus - rest after integer dividing |
+ – |
Additive |
|
<< >> |
Bitwise shift |
|
< > <= >= |
Relational |
|
== != |
Equality |
!= not equal |
& |
Bitwise-AND |
|
^ |
Bitwise-exclusive-OR |
|
| |
Bitwise-OR |
|
&& |
Logical-AND |
|
|| |
Logical-OR |
|
? : |
Conditional-expression |
|
= *= /= %= += –= <<= >>= &= ^= |= |
Simple and compound assignment (right to left associativity) |
x+=3; is equal x=x+3; |
, |
Sequential evaluation |
The short table with the precedence of symbols used by declarations:
Priority |
Symbol |
Note |
1 |
() |
left to right associativity |
2 |
[] |
left to right associativity |
3 |
* |
right to left associativity |
The short table with the precedence of symbols used by arithmetic operations:
Priority |
Symbol |
1 |
++ -- |
2 |
* / % |
3 |
+ - |
The short table with the precedence of symbols used by relational operations:
Priority |
Symbol |
1 |
< > <= >= |
2 |
== != |
3 |
&& |
4 |
|| |
Statement |
Description |
if else |
if (expression) statement1; else statement2; |
switch |
switch (expression) { case const1: statement1; break; ... case constx: statementx; break; default: statement; break; } |
for |
for (init_expression;cond_expression;loop_expression) statement; |
break |
Terminates the loop without evaluation loop_expression. |
continue |
Causes a jump to the start and evaluation loop_expression. |
while |
while (expression) statement; |
do - while |
do statement; while (expression); |
{ } |
Block (compound statement).
{ statement1; statement2; } |
Data type |
Description |
array |
int multi[2][3] = { {00,01,02}, {10,11,12} };/* INIT */ |
string |
unsigned char sa[]="abc"; |
struct |
struct type_name { type variable_name1; type variable_name2; } structure_variable; struct type_name structure_variable; |
union |
union type_name { type variable_name1; type variable_name2; } union_variable; |
bit field |
struct { unsigned short icon : 8; /* 8 bits field */ unsigned short color : 4; unsigned short underline : 1; unsigned short blink : 1; } screen; |
enum |
enum type_name{ name1, name2=init_val } enum_variable; |
enum {ROWS=3,COLS=3}; int multi[ROWS][COLS] = { {00,01,02}, {10,11,12}, {20,21,22} }; /* INIT */Logical representation:
Memory representation:
│<─ row 0 ─>│<─ row 1 ─>│<─ row 2 ─>│Pointers and Memory Representation
A variable storing in the memory
example : unsigned long i=257;Memory address structure:
SEGMENT : OFFSET address : 00 40 : 00 20 Hi Lo : Hi Lo byte
int x=1,y=8; /* x, y == variables - containes data */ int *pointx=&x; /* int * = pointer to int */ /* &x = address of the variable x */ /* variable pointx contains address of variable x */┌─────────┐ ┌─────────┐ ┌─────────┐
enum {MAX=5}; int arr[MAX] = {00,01,02,03,04}; int * parr = arr; &arr[0]==arr==parr // the same address access arr[3]==3 // the same data access parr[3]==3 *(parr+3)==3 *(arr+3)==3
Example:
int val=5; int *pval=&val; int **ppval=&pval; val==5 /* stored value */ &val==0040:0030 /* address of variable val */ *pval==5 /* point to value */ pval==0040:0030==&val /* stored address of variable val */ &pval==0040:0020 /* address of variable pval */ **ppval==5 /* point to point to value */ *ppval==0040:0030==&val /* point to variable pval with */ /* stored address of variable val */ ppval==0040:0020==&pval /* stored address of variable pval */ &ppval==0040:0010 /* address of variable ppval */
Next examples:
int *var[5]; /* (1) var name is (2) an array (3) of pointers */ ^ ^ ^ ^ /* (4) to int values */ 4 3 1 2
The array brackets "
[]" have higher priority than the pointer asterisk "*".int (*var)[5]; /* Pointer to array of int values */
The parentheses "
()" have higher priority than the brackets "[]".long *var( long, long ); /* Function returning pointer to long */
The function modifiers "
()" also have higher priority than the pointer asterisk "*".long (*var)( long, long ); /* Pointer to function returning long */
A variable storing on the disk:
struct rec { int x; int y; } rec1; rec1.x=15; /* 15==0x0F */ rec1.y=255; /* 255==0xFF */ /* written to the binary file */File position: ┌00┬01┬02┬03╥04┬05┬06┬07┐