Before I ask my question let me just say that I am a newbie to C, and do not know how to do a lot in it.
Anyway, the problem is that I need to print a specific number of characters. I first used Python, because that was a language that I was familiar with, and wrote this very simple program.
x = 5
print('#' * x)
This is what I want to achieve, but in C. Sorry if this is a duplicate or a stupid question, but I have been puzzled and without answers, even after looking on the internet.
char is a numeric type, same as int but shorter. It holds a numerical representation of the symbol (ASCII code). Multiplying it with an integer gives you an integer.
When you multiply a char by an int , the result is an int , not a char .
Yes error it says correct, you can't multiply string and int. You need to use Integer. parseInt() to convert String to integer before you can multiply.
What is Character arithmetic ? Character arithmetic is used to implement arithmetic operations like addition, subtraction ,multiplication ,division on characters in C and C++ language.
for ( size_t ii = 0; ii < 5; ++ii )
putchar('#');
Use a loop to print it multiple times.
In C, a symbol between ''
has a type char
, a character, not a string. char
is a numeric type, same as int
but shorter. It holds a numerical representation of the symbol (ASCII code). Multiplying it with an integer gives you an integer.
A string, contained between ""
is an array of characters. The variable will store a pointer to the first character.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With