Q:

unsigned int data types in c

Please help me in differentiating these codes in C:
Code 1:
#include<stdio.h>
#include <stdint.h>

uint8_t fb(int a)
{
    return -3;
}

int main()
{
    int a = fb(-3);
    printf("%d",a);
    return 0;
}

Code 2:
#include<stdio.h>

unsigned int fb(int a)
{
    return -3;
}

int main()
{
    int a = fb(-3);
    printf("%d",a);
    return 0;
}

The problem is the the first code returns 253 as expected but the second code returns -3 which is unexpected as return type is unsigned. Please help me how this is possible?
I have used mingw gcc compiler.

A:

In the first program,

When the function returns, int -3 (FFFFFFFD) is cast to uint8_t is 253 (FD). The higher order bytes are dropped so it fits.
When the assignment is performed, uint8_t 253 (FD) is cast to int by sign extension to 253 (000000FD).

In the second program,

When the function returns, int -3 (FFFFFFFD) cast to unsigned int 4294967293 (FFFFFFFD). No bytes needed to be dropped.
When the assignment is performed, unsigned int 4294967293 (FFFFFFFD) cast to int is -3 (FFFFFFFD).

Notes:

Assumes 32-bit ints, but it's a similar story with 64-bit ints.
This is an explanation of what happens for you; it isn't necessarily what the C spec calls to happen.

