I am converting some code from C into delphi pascal and have come to a brick wall. As my understanding of C is quite limited i am unsure of what '%*.*ld' is doing or how to do the same thing in Dephi. The full line is a bit long to put in here, but this is a similar one i am having trouble with:

MGRS_String := sprintf(MGRS+MGRS_String,'%2.2ld',Zone)

where zone is an integer. Any help would be appreciated
-J

Recommended Answers

All 5 Replies

%ld is a long integer. You can add a field width that specifies the minimum number of characters to print. So %5ld will pad numbers with fewer than five digits using spaces. You can add a precision using a period and another number that specifies the minimum number of digits in the number. So if you print the value 1 with %5.3ld, you'll get 001 padded with two spaces because the number is now 3 characters in length.

%2.2ld is effectively equivalent to %02ld because the field width negates the precision and the precision negates the field width. So if you print 1, you'd get 01 with no padding.

So what about the '*' ? is it like a wild card?

It's a placeholder for an argument that you supply later on:

printf("%*d\n", w, x);

Instead of specifying the width explicitly, printf takes it from the matching argument, which is w in this case.

I had to use this function a few day ago. It works pretty slick to create a string from int, long an so on.

It's a placeholder for an argument that you supply later on:

printf("%*d\n", w, x);

Instead of specifying the width explicitly, printf takes it from the matching argument, which is w in this case.

It's a shame that scanf doesn't have the same feature for string input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.