Martund
Martund

Reputation: 301

Clarification on various format specifiers in C program

I am not understanding what is major difference between %p,%u,%x,%d, except that %x shows hexadecimal,%u is used for unsigned integer and that %d is for any integer. I am very much confused after I took a integer variable and printed its address and its value (positive integer) separately, then irrespective of whatever format specifier I use, it was correctly printing the output (except of the difference in hexadecimal and decimal number system). So what is a major difference?

And if there is not much difference then which format specifiers are preferable for printing what type of variables?

Another doubt is that: Whether pointer of all multiplicity (I mean int *p; int **p; int ***p; etc.) occupy the same size (which is the size needed to store a valid address in the machine)? If not, then what is the size of these pointers?

Thanks for your help.

Upvotes: 0

Views: 328

Answers (2)

Motun
Motun

Reputation: 2149

With format specifiers, you tell the computer how to interpret the given variable/data.

A quick demo:

#include <stdio.h>

int main(void)
{
    int x = -5;
    printf("x value as int: [%d]\n", x);
    printf("x value as unsigned int: [%u]\n", x);
    printf("x value as hexadecimal: [%x]\n", x);
    printf("x value as pointer: [%p]\n", x);

    return 0;
}

Output:

x value as int: [-5]
x value as unsigned int: [4294967291]
x value as hexadecimal: [fffffffb]
x value as pointer: [0xfffffffb]

It's the same value given every time, i.e. x = -5.

  • We see the exact representation when given the right format specifier (the first case).

  • In second case we see a very big number. The answer to "Why" is a bit long to explain here, but you should look up how negative integers are represented in 2's complement system.

  • In the third case we see the hexadecimal representation of the number 4294967291. Hexadecimal numbers are usually shown with 0x at the beginning but %x doesn't do that.

  • The last one just shows how would the variable x seem if it were an address in the memory, again in hexadecimal format of course.

Upvotes: 0

dbush
dbush

Reputation: 223872

The %u, %x, %d, and %p format specifiers are used as follows:

  • %u: expects an unsigned int as a parameter and prints it in decimal format.
  • %x: expects an unsigned int as a parameter and prints it in hexadecimal format.
  • %d: expects an int as a parameter and prints it in decimal format.
  • %p: expects a void * as a parameter and prints it in an implementation defined way (typically as a hexadecimal number)

Additionally, %u, %x, %d can be prefixed with a length modifier:

  • l: denotes a long int or unsigned long int
  • ll: denotes a long long int or unsigned long long int
  • h: denotes a short int or unsigned short int
  • hh: denotes a signed char or unsigned char

Regarding pointer sizes, int *, int **, int ***, etc. are not required to be the same size, although on most implementations they will be.

Upvotes: 2

Related Questions