LinuxQuestions.org
Help answer threads with 0 replies.
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-23-2009, 07:42 PM   #1
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
this is how printf works


i wrote my own verison of printf and wanted to show off
the first param is a function to print a null terminated string
Code:
#define PERCENT_D (10)
#define PERCENT_X (16)

void printf(void (*out)(char *), const char *format, ...){ //void instead of int
int v;
char u[10],t=0;
va_list va;
va_start(va, format);
while(format[t]){
if(format[t]!='%'){u[0]=format[t]; u[1]=0; out(u);} else {
t++;
switch(tolower(format[t])){
case 'u':
case 'd':
v=va_arg(va, int);
itoa(v, u, PERCENT_D);
out(u);
break;
case 's':
out(va_arg(va, char *));
break;
case 'f':
v=va_arg(va, float);
itoa(v, u, PERCENT_D);
out(u);
break;
case 'x':
case 'X':
v=va_arg(va, int);
itoa(v, u, PERCENT_X);
out(u);
break;
default:out("%");
}
}
t++;
}
va_end(va);
}

Last edited by smeezekitty; 09-24-2009 at 09:54 PM.
 
Old 09-24-2009, 12:38 PM   #2
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
Horrible.

P.S.: Constructivecriticism is my middle name.
 
Old 09-24-2009, 12:47 PM   #3
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Original Poster
Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by carbonfiber View Post
Horrible.

P.S.: Constructivecriticism is my middle name.
that isnt consturctive criticism
it would be if you say what is horrable
 
Old 09-24-2009, 01:16 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by smeezekitty View Post
i wrote my own verison of printf and wanted to show off
the first param is a function to print a null terminated string
Code:
void printf(void (*out)(char *), const char *format, ...){ //void instead of int
int v;
char u[10],t=0;
va_list va;
va_start(va, format);
while(format[t]){
if(format[t]!='%'){u[0]=format[t]; u[1]=0; out(u);} else {
t++;
switch(tolower(format[t])){
case 'u':
case 'd':
v=va_arg(va, int);
itoa(v, u, 9);
out(u);
break;
case 's':
out(va_arg(va, char *));
break;
case 'f':
v=va_arg(va, float);
itoa(v, u, 10);
out(u);
break;
case 'x':
case 'X':
v=va_arg(va, int);
itoa(v, u, 16);
out(u);
break;
default:out("%");
}
}
t++;
}
va_end(va);
}
Why did you write your own 'printf' and why should others consider the code ?

...


Whatever you write, if you have constants like 9, 10, 16, etc. instead of meaningful macro names, your code is bad.

The allowed constants are 0 (like you are starting from the number zero element in an array) and +1, -1 - like 'idx = idx + 1;' - meaning next index/value in an array. And because of ++, -- in "C" one rarely needs +1, -1.

Sometimes 2, 3 - like each second or each third element, but 3 is already suspicious.
 
Old 09-24-2009, 01:29 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by Sergei Steshenko View Post
Whatever you write, if you have constants like 9, 10, 16, etc. instead of meaningful macro names, your code is bad.
Here it is used as the radix of the conversion, one of the few cases I wouldn't consider bad, let alone "horrible"...
 
Old 09-24-2009, 05:23 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Hko View Post
Here it is used as the radix of the conversion, one of the few cases I wouldn't consider bad, let alone "horrible"...
Still,

Code:
#define D_RADIX ((unsigned)10) /* for %d conversions */
#define X_RADIX ((unsigned)16) /* for %x conversions */
, i.e. relationship between symbolic ('D', 'X') and numeric (10, 16) radix. And the relationship is reflected in a meaningful macro name.
 
  


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
How printf works dale_chip Programming 3 08-25-2007 04:13 PM
fork and printf Intimidator Programming 7 12-17-2006 08:21 AM
XAWTV works, videodog works, motion works but how to code my own? rylan76 Linux - Hardware 0 01-06-2006 06:30 AM
printf problem??? Mistro116@yahoo.com Programming 2 11-13-2005 08:45 PM
printf new_user10 Programming 3 09-09-2003 11:12 PM

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

All times are GMT -5. The time now is 06:45 PM.

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