Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between %lx and %ld when printing an address from pointer?

Tags:

c

I have tried googling the first one for %lx, but I have no good results, BUT I have successfully searched up %ld which is just long int. Necessary for printing addresses I guess, but what is %lx for?

This is where I am confused:

int main()
{
    int value = 25;
    int *pointer = &value;
    printf("%ld\n", pointer);  // prints out the address of variable value( I hope)
    printf("0x%lx\n", pointer); // Completely confused here, is this perhaps address in hex?
}

Would be awesome if someone can clear this confusion I am having!

I have ran this code, and I have the results, but I am still not sure what the lx does..I have seriously tried googling this "%lx" in google, but no results explaining it.

Edit: if I use 'p' to print address then have I been wrong in thinking %ld prints address? Confused.

like image 516
Ray Avatar asked Oct 06 '14 19:10

Ray


People also ask

What does %LX mean C?

%lx means to print an argument of type unsigned long in hexadecimal. (There is no format to print a signed argument in hexadecimal.)

What is %ld in printf?

%d is for signed int, Use %u specifically for unsigned int. Long and int types are same. %ld is for long int. printf Type Field Characters (CRT)

What is the difference between %p and %U in C?

Here we will see what are the differences between %p and %x in C or C++. The %p is used to print the pointer value, and %x is used to print hexadecimal values. Though pointers can also be displayed using %u, or %x. If we want to print some value using %p and %x then we will not feel any major differences.

What is the difference between %U and %D?

%d is a signed integer, while %u is an unsigned integer. Pointers (when treated as numbers) are usually non-negative. If you actually want to display a pointer, use the %p format specifier.

How do I print the printer pointer address?

You can print a pointer value using printf with the %p format specifier. To do so, you should convert the pointer to type void * first using a cast (see below for void * pointers), although on machines that don't have different representations for different pointer types, this may not be necessary.


1 Answers

They're both undefined behavior.

To print a pointer with printf, you should cast the pointer to void * and use "%p".

That being said:

We can talk about the difference between "%ld" and "%lx" when trying to print integers. %ld expects a variable of type long int, and %lx expects a variable of type long unsigned int.

More or less though, The difference between x, o, d and u are about how numbers are going to be printed.

  • x prints an unsigned number in hexadecimal.
  • o prints an unsigned number in octal.
  • u prints an unsigned number in decimal.
  • d prints a signed number in decimal.
  • i prints a signed number in decimal.

We can then attach l to the format string for formats like %lx to specify that instead of an int, we're using a long int (That is, an unsigned long int, or long int).

There is a table at cppreference that has additional information: http://en.cppreference.com/w/c/io/fprintf

like image 178
Bill Lynch Avatar answered Sep 21 '22 04:09

Bill Lynch