5

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 printf?

8

In C, 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?

| improve this answer | |
  • many thanks for that i will just have to do it the long way then:) – biggrey54 Jul 11 '14 at 19:27
  • @biggrey54 You will find that in C, many things have to be done the "long way" compared to more modern languages. Also, welcome to Stack Exchange - you can vote posts up or down to show that they are useful, and accept an answer to your question if you want to show that it solved your problem. – Air Jul 11 '14 at 19:33
2

You can do this:

printf("%0*i", 20, 0);

This will print 0 20 times, as shown:

00000000000000000000

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.


TheBrainyOne out!


| improve this answer | |
  • This was exactly what I needed, thanks :^) – DerNamenvolle Nov 25 '18 at 19:39
1

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.

| improve this answer | |
  • ok for the answer many thanks – biggrey54 Jul 11 '14 at 19:28
0

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[10];
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 ] );
| improve this answer | |
-1

You can set up a string of 'X' using a for loop and an array of chars.

char arr[6];
for (int i = 0; i < 5; i++)
{
    arr[i] = 'X';
}
arr[5] = '\0';
| improve this answer | |
  • yeh the long way many thanks – biggrey54 Jul 11 '14 at 19:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .