printf() formatting for hexadecimal

The # part gives you a 0x in the output string. The 0 and the x count against your “8” characters listed in the 08 part. You need to ask for 10 characters if you want it to be the same. int i = 7; printf(“%#010x\n”, i); // gives 0x00000007 printf(“0x%08x\n”, i); // gives 0x00000007 … Read more

Printing 1 to 1000 without loop or conditionals

This one actually compiles to assembly that doesn’t have any conditionals: #include <stdio.h> #include <stdlib.h> void main(int j) { printf(“%d\n”, j); (&main + (&exit – &main)*(j/1000))(j+1); } Edit: Added ‘&’ so it will consider the address hence evading the pointer errors. This version of the above in standard C, since it doesn’t rely on arithmetic … Read more

Printing leading 0’s in C

printf(“%05d”, zipCode); The 0 indicates what you are padding with and the 5 shows the width of the integer number. Example 1: If you use “%02d” (useful for dates) this would only pad zeros for numbers in the ones column. E.g., 06 instead of 6. Example 2: “%03d” would pad 2 zeros for one number … Read more

‘printf’ vs. ‘cout’ in C++

I’m surprised that everyone in this question claims that std::cout is way better than printf, even if the question just asked for differences. Now, there is a difference – std::cout is C++, and printf is C (however, you can use it in C++, just like almost anything else from C). Now, I’ll be honest here; … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)