Type of array and printf specifiers
-
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.
-
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.
In:
printf("%s",result[i])
result[i]
is achar
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
-
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.
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
-
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.
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