Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - Multiply char by int

Tags:

c

string

int

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.

like image 263
Argarak Avatar asked Feb 11 '15 22:02

Argarak


People also ask

Can I multiply char with int in C?

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.

What happens when you multiply an int with a char?

When you multiply a char by an int , the result is an int , not a char .

Can you multiply string by int?

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.

Can we multiply two characters in C?

What is Character arithmetic ? Character arithmetic is used to implement arithmetic operations like addition, subtraction ,multiplication ,division on characters in C and C++ language.


2 Answers

for ( size_t ii = 0; ii < 5; ++ii )
    putchar('#');
like image 154
M.M Avatar answered Sep 20 '22 05:09

M.M


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.

like image 36
ftynse Avatar answered Sep 22 '22 05:09

ftynse