LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-17-2002, 11:49 PM   #1
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Rep: Reputation: 30
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.
 
Old 08-17-2002, 11:58 PM   #2
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
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 ...

Last edited by purpleburple; 08-18-2002 at 12:00 AM.
 
Old 08-18-2002, 04:35 AM   #3
sarin
Member
 
Registered: May 2001
Location: India, Kerala, Thrissur
Distribution: FC 7-10
Posts: 354
Blog Entries: 2

Rep: Reputation: 34
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
 
Old 08-18-2002, 04:59 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,535

Rep: Reputation: 148Reputation: 148
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("*");
 
Old 08-18-2002, 07:52 PM   #5
Malicious
Member
 
Registered: Jan 2002
Location: Galveston Island
Distribution: suse, redhat
Posts: 208

Rep: Reputation: 30
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
 
Old 08-18-2002, 10:44 PM   #6
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
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.
 
Old 08-18-2002, 11:14 PM   #7
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 55
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 {}
 
Old 08-18-2002, 11:54 PM   #8
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
thnks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Limewire causes blank space in KDE tray hand of fate Linux - Software 4 04-17-2005 08:29 PM
how to enumerate first line of text file to standard output? zero79 Linux - General 1 07-07-2004 06:37 PM
Is linux *blank* up my windows space? mimithebrain General 0 06-13-2004 08:05 PM
Blank line - grep. liguorir Linux - Software 1 04-24-2004 09:03 AM
Non-standard letters and space in directory structure over AppleShare? Dog and Pony Linux - Networking 0 02-11-2002 03:59 AM


All times are GMT -5. The time now is 06:19 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration