LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how do I print 'blank space' to standard out in C on same line? (https://www.linuxquestions.org/questions/programming-9/how-do-i-print-blank-space-to-standard-out-in-c-on-same-line-28158/)

purpleburple 08-17-2002 11:49 PM

how do I print 'blank space' to standard out in C on same line?
 
Hi. New to C. I am making a program that prints a '*' to standard out then cycles thru a 'for' loop printing 'blank space' to the same line and then finally finishes on that line with a '*' again. I am asking user to pick a number from 1 to 30 and printing '*' on 1 and 'users number' of that same line on standard out. I Know about using the for loop to print to the same line incrementally <--- is that a word? but don't know how to go about printing the 'blanks' . I could use printf("*\t\t*"); but don't know if this tab format would always end on the number the user selected etc. I checked C escape sequences in my C book but it doesn't explain how to print blank space to standard out. Then if I knew how I would have to have that blank space in a variable form to variate the blanks depending on the users input.

thnks
:)

purpleburple 08-17-2002 11:58 PM

posted to soon ... sorry (duh!) ...

printf(" "); prints a space

now I can use a for loop to print that blank space to the same line over and over on standard out.

Am I go about this simple procedure correctly?
Is there another or better way to do this?

thanks ...

sarin 08-18-2002 04:35 AM

You can use a string and memset function to fill it with any byte. Then you change the first and last bytes. Do a man memset for more info.
--Sarin

Mara 08-18-2002 04:59 PM

Small example. Let's say that you've got the number of blank spces to print in 'numberofblanks':
Code:

int i=0;
printf("*");
for(i=0;i<numberofblanks;i++)
    printf(" ");
printf("*");


Malicious 08-18-2002 07:52 PM

Or, without the for loop...

Code:

int i=0;
char blanks[MAXBLANKS];
int numberofblanks = 10;

memset(blanks,' ',MAXBLANKS);
/* assuming "numberofblanks" is less than MAXBLANKS */
printf("*%*.*s*",
        numberofblanks,
        numberofblanks,
        blanks);

man printf is your friend

purpleburple 08-18-2002 10:44 PM

thanks Mara! That 'for' loop you displayed is exactly what I had in mind for implementing this. Sorry Malicious ... that memset is a little above my head right now but I will learn it soon enough im sure. I will type 'man printf' and 'man memset' a little later to check this stuff out.


thanks guys ... and girls (mara) :)

Oh Yeah! Had quick question if possible.... Do I 'have' to put braces around a 'for' loop?? It seems some people put them and others don't. Why?

neo77777 08-18-2002 11:14 PM

If the for loop's body is a oneliner you don't require to put braces around it, if the body of a loop has more than one line than body needs to be encapsulated in {}

purpleburple 08-18-2002 11:54 PM

thnks :)


All times are GMT -5. The time now is 04:07 PM.