-5

Is it possible to print '#' n times using a for loop in C?

I am used to print(n * '#') in Python.

Is there an equivalent for that in C?

for(i=0; i < h; i++) {
    printf("%s\n", i * h);
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

3 Answers3

1

No. It's not possible by any mechanism in standard C. If you want a string of some sub-string repeated n times, you will have to construct it yourself.

Simple example:

const char *substring = "foo";

// We could use `sizeof` rather than `strlen` because it's
// a static string, but this is more general.
char   buf[8192];
size_t len = strlen(substring);
char  *ptr = buf;

// Ideally you should check whether there's still room in the buffer.
for (int i = 0; i < 10; ++i) {
    memcpy(ptr, substring, len);
    ptr += len;
}
*ptr = '\0';

printf("%s\n", buf);
Roflcopter4
  • 679
  • 6
  • 16
0

To output the symbol '#' n times in the for loop there is no need to create dynamically a string. It will be inefficient.

Just use the standard function putchar like

for ( int i = 0; i < n; i++ )
{
    putchar( '#' );
}
putchar( '\n' );

Here is a demonstrative program.

#include <stdio.h>

int main(void) 
{
    const char c = '#';
    
    while ( 1 )
    {
        printf( "Enter a non-negative number (0 - exit): " );
        
        unsigned int n;
        
        if ( scanf( "%u", &n ) != 1  || n == 0 ) break;
        
        putchar( '\n' );
        
        for ( unsigned int i = 0; i < n; i++ )
        {
            for ( unsigned int j = 0; j < i + 1; j++ )
            {
                putchar( c );
            }
            putchar( '\n' );
        }
        
        putchar( '\n' );
    }
    return 0;
}

The program output might look like

Enter a non-negative number (0 - exit): 10

#
##
###
####
#####
######
#######
########
#########
##########

Enter a non-negative number (0 - exit): 0

If you need to send the symbol in a file stream then use the function

int fputc(int c, FILE *stream);

instead of putchar.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Yes, using %.*s format specifier.

int h = 10; //upper limit
char pat[h*h];

memset(pat, '#', sizeof pat);

for (int i=0;i<h;i++)
{
  printf("%.*s\n",i*h, pat);
}

output:

##########
####################
##############################
########################################
##################################################
############################################################
######################################################################
################################################################################
##########################################################################################

Please excuse me if I misunderstood your question.

kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
  • Is this actually a standard format specifier? I was under the impression that it's an extension. – Roflcopter4 Sep 09 '20 at 10:47
  • It's not listed in the man page for printf or here "http://www.cplusplus.com/reference/cstdio/printf". I believe it is an extension. – Roflcopter4 Sep 09 '20 at 10:53
  • @Roflcopter4 it is listed as part of it see `width` table. – kiran Biradar Sep 09 '20 at 10:55
  • Read it more carefully. That is only to specify the minimum width of the string for formatting. – Roflcopter4 Sep 09 '20 at 10:59
  • @user3121023 Not quite. That specifies the maximum number of characters to print (unless a NUL is found earlier). You might use it to print a string that isn't NUL terminated. It does not print the string n times. – Roflcopter4 Sep 09 '20 at 11:12
  • Yeah. I'm an idiot. For having proven I didn't really read the original code I'll just quietly show myself out of this thread. – Roflcopter4 Sep 09 '20 at 11:43