Important points

  1. How programs in c would be compiled are compiler and system dependent.
  2. Programs begin executing at the beginning of main
  3. printf never supplies a newline automatically
  4. \t for tab, \b for backspace, \" for double quote, \\ for backslash itself
  5. printf() 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 are declarations, operators, expressions, statements, pointer, arithmetic etc.
  6. 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}}$

1
int y = -9;

$-9$ would be represented in $2’s$ complement format.

  • $9 = 000 \dots 0001001$
  • $-9 = 111 \dots 1110111$
Note
By default int is signed
1
2
3
4
int y = -9;

printf("%d", y) // -9
printf("%u", y) // huge number (why?) [111...10111] would be treated as unsigned and its huge.
  • %d: This will treat y as signed number.
  • %u: This will treat y as unsigned number
Important
printf() doesnt' use the information regarding type of data

$\underline{\textbf{Example 2}}$

1
2
3
4
unsigned int y = -9;

printf("%d", y) // -9
printf("%u", y) // huge number again

Extension and Truncation

For extra information

1
2
3
short int x = 9;
short x = 9;
short signed int x = 9;

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)


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