how do I print 'blank space' to standard out in C on same line?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
Last edited by purpleburple; 08-17-2002 at 11:51 PM.
int i=0;
char blanks[MAXBLANKS];
int numberofblanks = 10;
memset(blanks,' ',MAXBLANKS);
/* assuming "numberofblanks" is less than MAXBLANKS */
printf("*%*.*s*",
numberofblanks,
numberofblanks,
blanks);
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?
Last edited by purpleburple; 08-18-2002 at 10:46 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 {}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.