C - Unsigned Char (format specifier)
3 hours ago, wasab said:you can simply use %d in a printf to print out their ascii values. Alternatively, you can cast them to an int
#include <stdio.h> #include <string.h> typedef unsigned char* byte_pointer; // if least significant 8 bits are the first byte then this means your cpu is little Endian void show_bytes(byte_pointer start, size_t len) { int i; for (i = 0; i < len; i++) { printf("\nbyte %d: %.2x", i, start[i]); } printf("\n"); } void show_int(int x){ show_bytes((byte_pointer) &x, sizeof(int)); } void show_float(float x){ show_bytes((byte_pointer) &x, sizeof(float)); } void show_pointer(void* x){ show_bytes((byte_pointer) &x, sizeof(void*)); } int main() { unsigned char x = 'A'; // 65 unsigned char y = 'Y'; // 88 unsigned int z = (unsigned int)(x + y); printf("decimal value is %d", z); printf("\nbytes in hexadecimals are\n"); show_int(z); return 0; }
The problem isn't in the printing but in the scanf.
7 hours ago, Hi P said:I ran gcc -v and it says: gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
am I using an old one? (i seriously don't know), if so, how do I install or use C99 / C11?
I tried the following but it didn't work, it didn't compile
gcc -o -std=c99 test.exe test.c
The output filename should immediately follow the -o switch:
gcc -std=c99 -o test.exe test.c
That should compile. Even then it might not work as intended however, as the problem would be in the scanf implementation of the runtime library that is being used. I'd not worry about it and just use a regular int and %d.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now