| | |
sprintf, leading zeros
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2007
Posts: 13
Reputation:
Solved Threads: 0
I know this thread is "a little bit" old, but i would like if you can help me.
Im using sprintf(). This is my code:
However if the first digit of "inteiro" is a 0, this function doesnt count it. I wonder why?
(sorry about my eventual bad english)
Im using sprintf(). This is my code:
C Syntax (Toggle Plain Text)
int countDigits(int inteiro,int max){ char *string; string = (char *) calloc(max+1,sizeof(char)); sprintf(string,"%d",inteiro); return strlen(string); }
However if the first digit of "inteiro" is a 0, this function doesnt count it. I wonder why?
(sorry about my eventual bad english)
•
•
Join Date: Feb 2005
Posts: 31
Reputation:
Solved Threads: 2
•
•
•
•
I know this thread is "a little bit" old, but i would like if you can help me.
Im using sprintf(). This is my code:
C Syntax (Toggle Plain Text)
int countDigits(int inteiro,int max){ char *string; string = (char *) calloc(max+1,sizeof(char)); sprintf(string,"%d",inteiro); return strlen(string); }
However if the first digit of "inteiro" is a 0, this function doesnt count it. I wonder why?
(sorry about my eventual bad english)
The printf/sprintf/sscanf functions use format specifers that indicate the how the data should be displayed. In your case you a simple value of integer which will be the word size of the machine. At the same time since there is no "precision" the string will have zeroes supressed. You can definitely tell the printf() family of functions on how wide the string should be, and weather or not you want leading zeroes. For example, %5d would get you a five digit wide string that is zero supressed whereas %05D gives you the zeroes. You can also justify but beyond this, I highly recommend a good C text like K&R. These days you can probably download it as PDF.
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Reading from file, passing into function.
- Next Thread: Saving file names from directory to array
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks bash binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h




