C: Correct way to print “unsigned long” in hex

You’re doing it right. From the manual page: o, u, x, X The unsigned int argument is converted to unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal (x and X) notation. So the value for x should always be unsigned. To make it long in size, use: l (ell) A following integer conversion corresponds … Read more

C/C++ printf() before scanf() issue

Your output is being buffered. You have 4 options: explicit flush fflush after each write to profit from the buffer and still enforce the desiredbehavior/display explicitly. fflush( stdout ); have the buffer only buffer lines-wise useful for when you know that it is enough to print only complete lines setlinebuf(stdout); disable the buffer setbuf(stdout, NULL); … Read more

Print the structure fields and values in C

What you’re looking for is reflection. Java and other virtual languages has reflection so you can print out the variable names and function names for any given class. Because the compiler builds these reflection functions automatically. C does not have reflection. You must do everything manually.

How to check that two format strings are compatible?

Checking if 2 printf() format strings are compatible is an exercise in format parsing. C, at least, has no standard run-time compare function such as: int format_cmp(const char *f1, const char *f2); // Does not exist Formats like “%d %f” and “%i %e” are obviously compatible in that both expect an int and float/double. Note: … Read more

How do I align a number like this in C?

Why is printf(“%8d\n”, intval); not working for you? It should… You did not show the format strings for any of your “not working” examples, so I’m not sure what else to tell you. #include <stdio.h> int main(void) { int i; for (i = 1; i <= 10000; i*=10) { printf(“[%8d]\n”, i); } return (0); } … Read more