Jump to content

Printf

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
36 replies to this topic

#1
mhm1984

mhm1984

    Newbie

  • Members
  • Pip
  • 2 posts
hi everbody

when i use this code in unix/windows and linux it apear in different output:
...
char* ch="12";
printf("%05s",ch);
...
in windows and unix it.ll be "00012" but in linux " 12".
if there any solution for linux to print "00012" in output?

Edited by Jaan, 22 November 2008 - 03:49 AM.
Use code tags when you're posting your codes!


#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Convert it to an integer and choose the zero-padding for an integer?

#3
mhm1984

mhm1984

    Newbie

  • Members
  • Pip
  • 2 posts
there is alot of printf in my project and i search for a way to set a flag or something like that

#4
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

mhm1984 said:

there is alot of printf in my project and i search for a way to set a flag or something like that
:confused:

"Zero-padded strings" is undefined behavior, I believe. I think this is just a "bite the bullet and do it correctly moment".

#5
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
Try typing "man fprintf " at the terminal.
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,713 posts
This is the best way that doesn't exploit irregularities in implementation. Be sure to include ctype.h.

printf("%05s",atoi(my_string));


#7
CPD

CPD

    Learning Programmer

  • Members
  • PipPipPip
  • 57 posts
What if the string doesn't represent an integer? This works the right way on all strings:

#include <stdio.h>


/* Anything less than 1 is evaluated as 0 */

#define pos_or_zero(expr) ((expr) > 0 ? (int)(expr) : 0)


/**

  @brief 

    Just like strlen except it stops at a specified

    length if a null character hasn't been found yet

*/

size_t short_strlen(const char *s, size_t stop_at)

{

  size_t i;


  for (i = 0; i < stop_at; i++) {

    if (*s++ == '\0')

      break;

  }


  return i;

}


void main()

{

  char *s = "12";


  /* Simulates a 0 filled field width for %s. */

  printf("%.*s%s\n", pos_or_zero(5 - short_strlen(s, 5)), "00000", s);

}

short_strlen is just an optimization for longer lengths of s. Since strings up to 5 characters are the only ones that matter for this problem, there's no point in counting more than 5.

#8
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

dargueta said:

This is the best way that doesn't exploit irregularities in implementation. Be sure to include ctype.h.


printf("%05s",atoi(my_string));

FWIW, atoi is fairly low on the totem pole.
Ignoring all characters - Dev Shed

#9
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,713 posts
I've never heard of strtol(). Is it standard?

#10
CPD

CPD

    Learning Programmer

  • Members
  • PipPipPip
  • 57 posts
strtol is standard, along with strtoul, strtoll (C99), strtoull (C99), strtof, strtod and strtold. All of the ato* functions are based off of the strto* functions, but they lack the extra arguments and error checking. Here's an example of strtol's extensive error handling capabilities:

#include <errno.h>

#include <stdio.h>

#include <stdlib.h> /* for strtol */


void main()

{

  char line[1024];

  int rc = scanf("%1023[^\n]%*[^\n]", line);


  if (rc != EOF)

    getchar(); /* Eat the newline */

  

  if (rc == 1) {

    char *end;

    long value;

    

    /* strtol sets errno to ERANGE if the value is too large for long int */

    errno = 0;


    /*

      The second argument points to the character where conversion stopped.

      The third argument is the radix for conversion, where 0 == 8, 10 and 16.

    */

    value = strtol(line, &end, 0);


    /* Check errno first because any library call can change it */

    if (errno == ERANGE)

      fputs("Value is out of range for type long\n", stderr);

    else if (end == line)

      fputs("Cannot extract an integer value\n", stderr);

    else {

      if (*end != '\0')

        printf("Unconverted: \"%s\"\n", end);


      printf("The value is %ld\n", value);

    }

  }

}



#11
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,713 posts
I wish I had known about those earlier. Thanks for the info.

#12
CPD

CPD

    Learning Programmer

  • Members
  • PipPipPip
  • 57 posts
Now you know. And knowing is half the battle. GI Joe!!!!!!!!