Click here to Skip to main content
15,885,182 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have an integer value of 18 characters, which i declare as a long long int, say long long int license. The license length should be actually 20 characters( a valid license in my case will always be 20 characters), so for convenience, I append 00 to the license and input.

While printing, it is missing the leading 00's. How can I print the leading 00 also in the output?

What I have tried:

eg:
long long int license;
how i pass it as input =00123456789876543219
expected o/p: 00123456789876543219
actual output:123456789876543219
Posted
Updated 27-Mar-19 23:38pm
v2

I would suggest to store your license as a string, because that's how you treat it. You haven't mentioned any need to do numerical calculations on that number, so there should be no need to ever convert that string. Storing and printing then should be trivial.
 
Share this answer
 
Comments
Member 14161770 28-Mar-19 5:03am    
The same thing if u run in online compiler is working.
So, any settings is missing?Mine is AIX platform
use sprintf to format it with leading zeros.
 
Share this answer
 
Comments
k5054 27-Mar-19 13:53pm    
ie.
long long license = 12345678987654321L;
printf("%020lld", license)


output 00012345678987654321
Member 14161770 27-Mar-19 13:59pm    
getting warning as "(W) Integer constant 123456789876543219L out of range"
#realJSOP 27-Mar-19 14:06pm    
No integer type (except a decimal) can be 20 digits long. Try casting your value to a decimal before using printf.
Member 14161770 27-Mar-19 14:15pm    
Is casting there in C?
Richard MacCutchan 28-Mar-19 6:11am    
The correct format is %.20lld.
Mr. Simmons is on the right track and k5054 also. I use a variant of that to do it :
C++
sprintf( outputBuffer, "%020I64d", sixtyfourbitvalue );
That will be twenty digits with leading zeros.

Incidentally, in stdint.h it has this definition :
C++
#define INT64_MAX        9223372036854775807i64
so you might want to try a similar format. That is, if you are using visual studio. Those are not standard formats for 64-bit values.

For a fun exercise, try writing a function to accept a 64-bit integer and load a text buffer with a comma-separated string. For example: 1,234,456,890. Depending on your location, you might use dots for separating the thousands so for bonus credit use the locale to select the appropriate separation character.
 
Share this answer
 
Comments
Member 14161770 28-Mar-19 3:00am    
What I tried is long long i=0;
printf("\nenter num\n");
scanf("%lld",&i);
printf("%0020lld",i);


but in o/p i am getting 2 digits extra:

eg:
i/p:100872063196938702
o/p:0010087206319693870246

What is the error?
Richard MacCutchan 28-Mar-19 6:09am    
See my solution below.
You need to specify the minimum number of digits to print in your format, so use the following:
C++
printf("%.20lld\n", license);

That will correctly print all values, e.g.:
00123456789876543219

00000000000123456789

However, unless you are using this value in calculations you could just input it as a string and then prepend the requisite number of zeroes.
 
Share this answer
 
Comments
Member 14161770 28-Mar-19 8:42am    
Nothing seems to be working and interesting part is its part is its printing some random value. But on a normal compiler this is working

eg:
printf("%020lld",i);
i/p: 102012000000000001
o/p : 0010201200000000000120
Richard MacCutchan 28-Mar-19 8:46am    
I tested the code on a number of different values so I am sure it works correctly. So you need to show us your code, we cannot guess what is wrong.
Richard MacCutchan 28-Mar-19 8:49am    
Your format string is still wrong. You need to use the precision specifier, which uses a period (full stop character) in front of the value for number of characters, i.e. %.20lld
Member 14161770 28-Mar-19 12:10pm    
If I try the same in my env. its not working. I tried using online compiler, its working
Richard MacCutchan 28-Mar-19 12:13pm    
I am sorry but I have no idea what your code looks like or what happens when you run it. What is your environment, what compiler are you using, what actually happens in your code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900