I am new to the C-Headers - stdint.h
and inttypes.h
. I was trying out some code to get an inkling of how uint8_t
works. But, it seems to have encountered a problem.
I have declared 4 uint8_t
integers with the boundary values 0, 255, 256, -1 respectively and performed some simple arithmetic operations on it. I did this since I wanted to know what errors/warnings does the c-compiler (I am using gcc 5.4.0
on linux) generate. And if not, I was interested in knowing what the output looked like. The code in given below.
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
int main() {
int a = 10;
printf("%d\n", a++);
printf("%d\n\n", a);
// 8 bit unsigned integer -> range[0, 255]
uint8_t ua81=0, ua82=255, ua83=256, ua84=-1;
printf("--------STDINT.H----uint8_t--DEMO--------\nua81 = %" PRIu8 "\n", ua81);
printf("ua82 = %" PRIu8 "\nua83 = %" PRIu8 "\nua84 = %" PRIu8 "\n\n", ua82, ua83, ua84);
printf("ua81+1 = %" PRIu8 "\nua82-3 = %" PRIu8 "\nua83-4+7 = %" PRIu8 "\nua84-1+20 = %" PRIu8 "\n----------\n\n", ua81+1, ua82-3, ua83-4+7, ua84-1+20);
return 0;
}
The output of this code is as follows:
vagrant@ubuntu-xenial:~/Documents/Coding Practice/p_c$ vi stdint_h.c
vagrant@ubuntu-xenial:~/Documents/Coding Practice/p_c$ gcc -Wall stdint_h.c -o a
stdint_h.c: In function ‘main’:
stdint_h.c:11:33: warning: large integer implicitly truncated to unsigned type [-Woverflow]
uint8_t ua81=0, ua82=255, ua83=256, ua84=-1;
^
vagrant@ubuntu-xenial:~/Documents/Coding Practice/p_c$ ./a
10
11
--------STDINT.H----uint8_t--DEMO--------
ua81 = 0
ua82 = 255
ua83 = 0
ua84 = 255
ua81+1 = 1
ua82-3 = 252
ua83-4+7 = 3
ua84-1+20 = 274
----------
As mentioned earlier, I am using a Linux machine for this with the gcc 5.4.0
compiler.
vagrant@ubuntu-xenial:~/Documents/Coding Practice/p_c$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I am not able to understand why, for the variable ua84
, the value is not rolling over to 0
after reaching 255
, while the same is working for ua83
. I think the value, considering the rollover after 255, of ua84
should have been 18
.
I feel this has got something to do with the Implicit Truncation mentioned in the warning
that was generated pertaining to the variable ua83
while compiling. I don't know exactly what is wrong with that. I would also like to know how to go about that I still want to continue using uint8_t
.