1

I'm converting my c++ program to c. It's a simple factorial function. I used an interesting method in the c++ program in order to space it less and less each time the function called itself. This is what that looked like:

cout << setw( number * 3 ) << "" << "number is: " << number << endl;

It's not translating into c as easily as I had hoped. My idea (which is probably incorrect) is

char c = "";
printf( "%*c number is: %lo\n", number * 3, c, number);

I'm getting an error that says: warning: initialization makes integer from pointer without a cast [enabled by default]

How would you achieve this same effect in c? I really liked the way I implemented it in c++ and would like to do it in a similar way.

1

3 Answers 3

4

The warning you get has nothing to do with your printing attempts. The warning comes from

char c = "";

You cannot initialize a character variable with a string literal. If you need a character, initialize it with a character constant

char c = ' ';

If you need a string, declare it as a pointer or as a character array

const char *c = "";
char c[] = "";

It really depends on what you are trying to do.

But in reality you don't need that c at all. The direct analogue of your C++ code in C would be a mere

printf("%*snumber is: %d\n", number * 3, "", number);

assuming your number is of int type.

2

in C, you use single quote for one character, you use double quotes for C style strings

So

char c = "" ;

is wrong.

you should use

char c = ' ';
1
  • You forgot to mention that he shouldn't be adding the padding manually anyway.
    – user529758
    Sep 27, 2013 at 17:04
0

How would you achieve this same effect in c?
Something like:

printf("%s: %d", "The number is", number * 3"; 

spaces can be edited into the printf statement, but if you want a char array space you can do it like:

char c[]={" "};

printf("%s:%s%d", "The number is", c, number * 3"; 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.