Important points
- How programs in c would be compiled are compiler and system dependent.
- Programs begin executing at the beginning of
main
printf
never supplies a newline automatically\t
for tab,\b
for backspace,\"
for double quote,\\
for backslash itselfprintf()
is not a part of C language, its’ part of the C standard library, which has very close relation to C language. Parts of C languages aredeclarations
,operators
,expressions
,statements
,pointer
,arithmetic
etc.- Text input or output regardless of its origination is dealt with streams of characters.
Unsigned and Signed Integers
Lets’ take an example code:
$\underline{\textbf{Example 1}}$
|
|
$-9$ would be represented in $2’s$ complement format.
- $9 = 000 \dots 0001001$
- $-9 = 111 \dots 1110111$
int
is signed
|
|
- %d: This will treat y as
signed
number. - %u: This will treat y as
unsigned
number
printf()
doesnt' use the information regarding type of data
$\underline{\textbf{Example 2}}$
|
|
Extension and Truncation
For extra information
|
|
All the above definitions are same.
Extension
Copying a lower bit number to higher bit number, e.g. short to int
$\underline{\textbf{Example 1}}$
short int x = 9;
$32$-bit extension
$16$ bits | $16$ bits |
---|---|
0000 0000 0000 1001 |
32 bit extension | |
16 bits | 16 bits |
0000 0000 0000 1001 |
we want to copy this x to new variable,
int ix = x;
32 bit extension | |
16 bits | 16 bits |
0000 0000 0000 0000 | 0000 0000 0000 1001 |
copy depends on the source
and not on what we are extending it to (destination)
$\underline{\textbf{Example 2}}$
short int x = -9;
32 bit extension | |
16 bits | 16 bits |
1111 1111 1111 0111 |
we want to copy this x to new variable,
int ix = x;
32 bit extension | |
16 bits | 16 bits |
1111 1111 1111 1111 | 1111 1111 1111 0111 |
Source is signed so I copied all $1’s$
$\underline{\textbf{Example 3}}$
short int x = -9;
32 bit extension | |
16 bits | 16 bits |
1111 1111 1111 0111 |
we want to copy this x to new variable,
unsigned int ix = x;
32 bit extension | |
16 bits | 16 bits |
1111 1111 1111 1111 | 1111 1111 1111 0111 |
Destination (unsigned
) doesnt’ matter.
$\underline{\textbf{Example 4}}$
unsigned short int x = -9;
32 bit extension | |
16 bits | 16 bits |
1111 1111 1111 0111 |
we want to copy this x to new variable,
int ix = x;
32 bit extension | |
16 bits | 16 bits |
0000 0000 0000 0000 | 1111 1111 1111 0111 |
$\underline{\textbf{Example 5}}$
unsigned short int x = -9;
32 bit extension | |
16 bits | 16 bits |
1111 1111 1111 0111 |
we want to copy this x to new variable,
unsigned int ix = x;
32 bit extension | |
16 bits | 16 bits |
0000 0000 0000 0000 | 1111 1111 1111 0111 |
Extension depends on RHS(source
)
- Promotion always happens according to the source variables’ type
- Signed:
sign extension
(copy MSB-0 or 1 to fill new spaces) - Unsigned:
zero fill
(copy 0’s to fill new space)
- Signed:
Truncation
Copying higher bit number to lower bit number
[!IMPORTANT]
- Regardless of source/destination or signed/unsigned type, truncation always just truncates.
- This can cause number to change drastically in sign and value