LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 09-09-2003, 09:50 PM   #1
new_user10
Member
 
Registered: Jun 2003
Posts: 30

Rep: Reputation: 15
printf


hi everyone,

i am trying to print a certain string repeatedly x number of times using printf and its many options. i don't want to use iteration

for (....)
printf("..")

is there a way i can type:

printf("....", 2)

and have printf print out something like 22222222? what are the appropriate printf modifiers? thanks in advance.
 
Old 09-09-2003, 09:56 PM   #2
DrOzz
Senior Member
 
Registered: May 2003
Location: Sydney, Nova Scotia, Canada
Distribution: slackware
Posts: 4,185

Rep: Reputation: 60
i don't quite understand exactly what you want without some sample code that you have or something, but couldn't you just use a for loop and exit the loop at the time you want to, as i said i dont quite grasp what your saying so i dont' know when you would want to exit the loop, so if you have a little more info or sample code then someone could elaborate...
if its just the number 22222222, couldn't you just declare an integer to be 22222222....
so instead of me spitting out what might be stupid ideas, tell what you want the program to accomplish...
 
Old 09-09-2003, 10:01 PM   #3
new_user10
Member
 
Registered: Jun 2003
Posts: 30

Original Poster
Rep: Reputation: 15
sorry about the lack of clarity.

basically, printf usually takes in a format string with a bunch of %_, and you would supply an equal number of arguments as there are %_s right? i am trying to discover a command/option with printf where i can do:

printf("<some special option>", first_param)

and have printf output first_param a certain number of times(specified in the <special option>) in a row.

example:

printf("%..3s", "string") ---> stringstringstring

the '%..3s' is the part i am not sure about to achieve this functionality. thanks.
 
Old 09-09-2003, 11:12 PM   #4
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
Printf doesnt have the capacity of its own to repeat certain strings. The simplest way to repeat a string , say, 2 times is to do this...

Code:
for(x=1;x<3;x++) { printf("WHATEVER"); }
another way is to define a wrapper function. This would be the easiest way if you were going need a "repeating printf" often. This code defines the wrapper function printfr, which repeats a string a certain number of times.

Code:
#include <stdio.h>  
void printfr(char* string,int xs);  

main(){ 
char *str="hey\n"; 
printfr(str,4); 
}  

void printfr(char* string,int xs){ 
int x; 
for(x=0;x<xs;x++){ 
printf("%s",string); 
} 
}
A better printfr wrapper could be made with using using argument lists, but I have no idea how C proficient you are.

Last edited by jinksys; 09-11-2003 at 10:00 PM.
 
  


Reply



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
printf problem??? Mistro116@yahoo.com Programming 2 11-13-2005 08:45 PM
is a printf wrapper possible? Thinking Programming 2 10-21-2005 07:44 AM
How does printf work? Ephracis Programming 1 12-25-2004 03:06 AM
c printf question. fredgt Programming 5 09-09-2004 05:06 AM
More arguments in printf() AMMullan Programming 3 02-23-2004 02:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:47 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration