In Python you can have a print statement print out a number of the same char by using
print("X" * 5) which will give you
XXXXX. Can this be done in C with
char is an integer type; if you multiply a character by an integer, you're really multiplying two integer values. But the result may not be what you expect! For instance, try running this code:
int x = 'a' * 2; char y = 'a' * 2; printf("x: %d\n", x); printf("y: %d\n", y);
On ideone.com, the output is:
x: 194 y: -62
Not only that, a string in C is represented by a character array, and there is no built-in support for multiplying arrays; you have to do it yourself with a loop. So even if you have the
string type provided in the CS50 library, you're out of luck.
Python isn't the only language that has this functionality, but it's a feature that you should only expect to see in higher-level languages than C, particularly those referred to as "scripting" languages.
The default implementation of Python is actually built on C code, and your question piqued my curiosity, so I asked a question on StackOverflow to find out exactly how the Python feature is implemented in C. This is definitely more advanced, and not the way you would necessarily repeat a string in pure C code, but if you're interested: How is string multiplication implemented in CPython?
You can do this:
printf("%0*i", 20, 0);
This will print 0 20 times, as shown:
However, the 'long way', for loops, is probably the way to go, as the way I showed above is a bit tedious, and complicated even for the later parts of the C section of the course.
No, you can't do this in 'C' as far as I think.In python multiplication of a string by a number is defined as 'repetition' of the string that number of times.
One way you can do this in 'C++'(a superset of 'C' language) is through 'operator overloading'.By this you can redefine
* operator's functionality for a custom made string or character class.This is known as C++ "object oriented programming".
'C' does not support operator overloading,so there might be some other arcane way to do it with the
* operator but no straight forward way as far as I think.
If you are only interested in the result of repeating the string/char a number of times without using the
* operator , than a function with some loop would do it.
Unfortunately, there's no way to do it with the multiplcation operator. However, if you have an idea of how many times you'll need the character, you can use something like this to get it done:
//Assuming you need five tabs char buf; memset(buf, '\t', 10); buf[ sizeof(buf) - 1 ] = '\0'; printf( "omg, look at all these tabs: %s\n", &buf[ 4 ]);
This works by filling a buffer with the character you'll need, and using an arbitrary position in the buffer to control how many characters should be printed. printf will stop when it sees a NUL character ('\0'). So if you start at position 9, you'll see nothing. Starting at position 8, you'll get one tab. Position 7, you'll get two and so on...
The following will also work if you're pretty certain you will only be using a specific character. Its uses are a bit limited, but it comes in handy when doing things like formatting strings in a loop...
//Assuming you need five tabs printf( "omg, look at all the tabs: %s\n", &"\t\t\t\t\t\t\t\t\t\t"[ 4 ] );