Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Type of array and printf specifiers

Type of array and printf specifiers

Scheduled Pinned Locked Moved C / C++ / MFC
databasedata-structuresquestion
4 Posts 4 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 1144427
    wrote on last edited by
    #1

    Hi there, I can't understand why my char type array isn't displayed with printf using string specifier %s. Here is my code.

    #include
    #include
    #include

    #define SIZE 10

    void findbinary(int number, char result[], int index);

    int main (void)
    {
    int someNumber = 233;
    char result[SIZE];
    int index = 0;
    size_t i;
    findbinary(someNumber, result,0);
    printf("Decimal %d in binary = %c ",someNumber);
    for(i = 0; result[i] != '\0'; i++)
    printf("%s",result[i]);
    }

    void findbinary(int number, char result[], int index)
    {

    if(number == 0){
        return;
    }
    result\[index\] = number % 2;
    findbinary(number / 2, result, index + 1);
    

    }

    It displayed properly if I use %d specifier, but... this is char array..? Thank you.

    Mircea NeacsuM K CPalliniC 3 Replies Last reply
    0
    • U User 1144427

      Hi there, I can't understand why my char type array isn't displayed with printf using string specifier %s. Here is my code.

      #include
      #include
      #include

      #define SIZE 10

      void findbinary(int number, char result[], int index);

      int main (void)
      {
      int someNumber = 233;
      char result[SIZE];
      int index = 0;
      size_t i;
      findbinary(someNumber, result,0);
      printf("Decimal %d in binary = %c ",someNumber);
      for(i = 0; result[i] != '\0'; i++)
      printf("%s",result[i]);
      }

      void findbinary(int number, char result[], int index)
      {

      if(number == 0){
          return;
      }
      result\[index\] = number % 2;
      findbinary(number / 2, result, index + 1);
      

      }

      It displayed properly if I use %d specifier, but... this is char array..? Thank you.

      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      In:

      printf("%s",result[i])

      result[i] is a char not a string. It should be printed with '%c' not '%s'. Also in:

      printf("Decimal %d in binary = %c ",someNumber);

      you have 2 format specifiers (%d and %c) and only one argument. All hell can break loose when program accesses nonexistent argument.

      Mircea

      1 Reply Last reply
      0
      • U User 1144427

        Hi there, I can't understand why my char type array isn't displayed with printf using string specifier %s. Here is my code.

        #include
        #include
        #include

        #define SIZE 10

        void findbinary(int number, char result[], int index);

        int main (void)
        {
        int someNumber = 233;
        char result[SIZE];
        int index = 0;
        size_t i;
        findbinary(someNumber, result,0);
        printf("Decimal %d in binary = %c ",someNumber);
        for(i = 0; result[i] != '\0'; i++)
        printf("%s",result[i]);
        }

        void findbinary(int number, char result[], int index)
        {

        if(number == 0){
            return;
        }
        result\[index\] = number % 2;
        findbinary(number / 2, result, index + 1);
        

        }

        It displayed properly if I use %d specifier, but... this is char array..? Thank you.

        K Offline
        K Offline
        k5054
        wrote on last edited by
        #3

        Your compiler will warn you about issues with printf formats not agreeing with the arguments provided. MS C seems to do this even without any additional warning flags. If you're using GCC (linux) or clang (Apple?) then you can add -Wall to the command line. The -Wall flag will generate warnings for the most often occurring code issues that are usually the cause of bugs. It's probably a good idea to add -Wextra to the command line, too.

        Keep Calm and Carry On

        1 Reply Last reply
        0
        • U User 1144427

          Hi there, I can't understand why my char type array isn't displayed with printf using string specifier %s. Here is my code.

          #include
          #include
          #include

          #define SIZE 10

          void findbinary(int number, char result[], int index);

          int main (void)
          {
          int someNumber = 233;
          char result[SIZE];
          int index = 0;
          size_t i;
          findbinary(someNumber, result,0);
          printf("Decimal %d in binary = %c ",someNumber);
          for(i = 0; result[i] != '\0'; i++)
          printf("%s",result[i]);
          }

          void findbinary(int number, char result[], int index)
          {

          if(number == 0){
              return;
          }
          result\[index\] = number % 2;
          findbinary(number / 2, result, index + 1);
          

          }

          It displayed properly if I use %d specifier, but... this is char array..? Thank you.

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Probably you meant something similar to

          #include #include #include #define SIZE 10

          void findbinary(int number, char result[], int index);

          int main (void)
          {
          int someNumber = 233;
          char result[SIZE];
          findbinary(someNumber, result,0);
          printf("Decimal %d in reversed binary %s\n", someNumber, result);
          return 0;
          }

          void findbinary(int number, char result[], int index)
          {

          if ( index == SIZE)
            exit(-1); //TODO: notify the error
          
          if(number == 0){
              result\[index\] = '\\0'; // append theterminator
              return;
          }
          result\[index\] = (number % 2) + '0'; // obtain either the CHARACTER '0' or '1'
          findbinary(number / 2, result, index + 1);
          

          }

          Note you are representing the binary number 'reversed' (that is leftmost bit is the least significant).

          "In testa che avete, Signor di Ceprano?" -- Rigoletto

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups