2

I want to know that how to print the complete integer value with zero also in C only.

And i am in kernel space ,want to print some values in kernel module.

Like if a is a 32 bit integer then

int a = 14

then output will be like this

value of a = 0x0000000e

in hexa-decimal.

Thanks in advance.

goodies
  • 521
  • 2
  • 6
  • 14
  • 1
    Note, you'll get `0000000E` if you use `%08X` and `0000000e` if you use `%08x`. Big X does big ABCDEF and little x does little abcdef. I prefer the caps style myself. – Travis Griggs May 04 '13 at 05:00

3 Answers3

17

Put a 0 in the format:

printf("value of a = 0x%08x", a);

From the printf(3) man page:

'0' (zero)

Zero padding. For all conversions except n, the converted value is padded on the left with zeros rather than blanks. If a precision is given with a numeric conversion (d, i, o, u, i, x, and X), the 0 flag is ignored.

Nothing like the documentation to answer questions for you - at least if the documentation is any good.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • 1
    ... and if the documentation is any good, it'll tell you that an *unsigned int* corresponds, and "If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined." (§7.21.6.1p9 of n1570.pdf)... – autistic May 04 '13 at 05:39
  • That's true, strictly speaking. In practice, are you aware of any implementations that don't behave reasonably? That is, as if there were an explicit cast? – Carl Norum May 04 '13 at 05:44
  • 1
    +1 but perhaps one should note also that there is `#` as a modifier for an "alternate" form that provides the `0x` in front. – Jens Gustedt May 04 '13 at 08:43
  • @CarlNorum [I guess so](http://stackoverflow.com/questions/14120197/are-there-any-existing-c-implementations-having-padding-bit-in-unsigned-intege/)... – autistic May 04 '13 at 12:09
  • @CarlNorum The problem is that `printf` is a variadic function, and because of this there is no conversion from `int` to `unsigned int`, implicit or explicit. Push an `int`, pop an `unsigned int` with the exact same representation... Hence, `printf("%X\n", -1);` will also invariably react differently depending upon whether ones complement, twos complement or sign-and-magnitude representation is used, in addition to the possibility for padding in the integer representation to have an impact, as raised in the link in my previous comment. – autistic May 04 '13 at 12:25
  • Yeah, I'm familiar with the reasoning. I just wondered if you'd ever really experienced the problem in practice. It seems like there's a lot of convergence on some of these technically nonstandard things. – Carl Norum May 04 '13 at 13:46
1
#include <stdio.h>

int main(int argc, char *argv[])
{
  int a = 14;

  printf("value of a = 0x%08x\n", a);

  return 0;
}
John Leimon
  • 1,063
  • 1
  • 9
  • 13
-1
//print 32 bit no in hex
 uint32_t n = 0x10203040;
 printf("32_bit no 0x%08x",n);


32_bit no 0x10203040