explanation for source code of printf

Reply

Join Date: May 2007
Posts: 12
Reputation: narendharg is an unknown quantity at this point 
Solved Threads: 0
narendharg narendharg is offline Offline
Newbie Poster

explanation for source code of printf

 
-1
  #1
May 31st, 2007
  1. /*
  2.  Copyright 2001, 2002 Georges Menie (<URL snipped>)
  3.   This program is free software; you can redistribute it and/or modify
  4.   it under the terms of the GNU Lesser General Public License as published by
  5.   the Free Software Foundation; either version 2 of the License, or
  6.   (at your option) any later version.
  7.   This program is distributed in the hope that it will be useful,
  8.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10.   GNU Lesser General Public License for more details.
  11.   You should have received a copy of the GNU Lesser General Public License
  12.   along with this program; if not, write to the Free Software
  13.   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. /*
  16.  putchar is the only external dependency for this file,
  17.  if you have a working putchar, just remove the following
  18.  define. If the function should be called something else,
  19.  replace outbyte(c) by your own function call.
  20. */
  21. #define putchar(c) outbyte(c)
  22. static void printchar(char **str, int c)
  23. {
  24. extern int putchar(int c);
  25. if (str) {
  26. **str = c;
  27. ++(*str);
  28. }
  29. else (void)putchar(c);
  30. }
  31. #define PAD_RIGHT 1
  32. #define PAD_ZERO 2
  33. static int prints(char **out, const char *string, int width, int pad)
  34. {
  35. register int pc = 0, padchar = ' ';
  36. if (width > 0) {
  37. register int len = 0;
  38. register const char *ptr;
  39. for (ptr = string; *ptr; ++ptr) ++len;
  40. if (len >= width) width = 0;
  41. else width -= len;
  42. if (pad & PAD_ZERO) padchar = '0';
  43. }
  44. if (!(pad & PAD_RIGHT)) {
  45. for ( ; width > 0; --width) {
  46. printchar (out, padchar);
  47. ++pc;
  48. }
  49. }
  50. for ( ; *string ; ++string) {
  51. printchar (out, *string);
  52. ++pc;
  53. }
  54. for ( ; width > 0; --width) {
  55. printchar (out, padchar);
  56. ++pc;
  57. }
  58. return pc;
  59. }
  60. /* the following should be enough for 32 bit int */
  61. #define PRINT_BUF_LEN 12
  62. static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase)
  63. {
  64. char print_buf[PRINT_BUF_LEN];
  65. register char *s;
  66. register int t, neg = 0, pc = 0;
  67. register unsigned int u = i;
  68. if (i == 0) {
  69. print_buf[0] = '0';
  70. print_buf[1] = '\0';
  71. return prints (out, print_buf, width, pad);
  72. }
  73. if (sg && b == 10 && i < 0) {
  74. neg = 1;
  75. u = -i;
  76. }
  77. s = print_buf + PRINT_BUF_LEN-1;
  78. *s = '\0';
  79. while (u) {
  80. t = u % b;
  81. if( t >= 10 )
  82. t += letbase - '0' - 10;
  83. *--s = t + '0';
  84. u /= b;
  85. }
  86. if (neg) {
  87. if( width && (pad & PAD_ZERO) ) {
  88. printchar (out, '-');
  89. ++pc;
  90. --width;
  91. }
  92. else {
  93. *--s = '-';
  94. }
  95. }
  96. return pc + prints (out, s, width, pad);
  97. }
  98. static int print(char **out, int *varg)
  99. {
  100. register int width, pad;
  101. register int pc = 0;
  102. register char *format = (char *)(*varg++);
  103. char scr[2];
  104. for (; *format != 0; ++format) {
  105. if (*format == '%') {
  106. ++format;
  107. width = pad = 0;
  108. if (*format == '\0') break;
  109. if (*format == '%') goto out;
  110. if (*format == '-') {
  111. ++format;
  112. pad = PAD_RIGHT;
  113. }
  114. while (*format == '0') {
  115. ++format;
  116. pad |= PAD_ZERO;
  117. }
  118. for ( ; *format >= '0' && *format <= '9'; ++format) {
  119. width *= 10;
  120. width += *format - '0';
  121. }
  122. if( *format == 's' ) {
  123. register char *s = *((char **)varg++);
  124. pc += prints (out, s?s:"(null)", width, pad);
  125. continue;
  126. }
  127. if( *format == 'd' ) {
  128. pc += printi (out, *varg++, 10, 1, width, pad, 'a');
  129. continue;
  130. }
  131. if( *format == 'x' ) {
  132. pc += printi (out, *varg++, 16, 0, width, pad, 'a');
  133. continue;
  134. }
  135. if( *format == 'X' ) {
  136. pc += printi (out, *varg++, 16, 0, width, pad, 'A');
  137. continue;
  138. }
  139. if( *format == 'u' ) {
  140. pc += printi (out, *varg++, 10, 0, width, pad, 'a');
  141. continue;
  142. }
  143. if( *format == 'c' ) {
  144. /* char are converted to int then pushed on the stack */
  145. scr[0] = *varg++;
  146. scr[1] = '\0';
  147. pc += prints (out, scr, width, pad);
  148. continue;
  149. }
  150. }
  151. else {
  152. out:
  153. printchar (out, *format);
  154. ++pc;
  155. }
  156. }
  157. if (out) **out = '\0';
  158. return pc;
  159. }
  160. /* assuming sizeof(void *) == sizeof(int) */
  161. int printf(const char *format, ...)
  162. {
  163. register int *varg = (int *)(&format);
  164. return print(0, varg);
  165. }
  166. int sprintf(char *out, const char *format, ...)
  167. {
  168. register int *varg = (int *)(&format);
  169. return print(&out, varg);
  170. }
  171. #ifdef TEST_PRINTF
  172. int main(void)
  173. {
  174. char *ptr = "Hello world!";
  175. char *np = 0;
  176. int i = 5;
  177. unsigned int bs = sizeof(int)*8;
  178. int mi;
  179. char buf[80];
  180. mi = (1 << (bs-1)) + 1;
  181. printf("%s\n", ptr);
  182. printf("printf test\n");
  183. printf("%s is null pointer\n", np);
  184. printf("%d = 5\n", i);
  185. printf("%d = - max int\n", mi);
  186. printf("char %c = 'a'\n", 'a');
  187. printf("hex %x = ff\n", 0xff);
  188. printf("hex %02x = 00\n", 0);
  189. printf("signed %d = unsigned %u = hex %x\n", -3, -3, -3);
  190. printf("%d %s(s)%", 0, "message");
  191. printf("\n");
  192. printf("%d %s(s) with %%\n", 0, "message");
  193. sprintf(buf, "justif: \"%-10s\"\n", "left"); printf("%s", buf);
  194. sprintf(buf, "justif: \"%10s\"\n", "right"); printf("%s", buf);
  195. sprintf(buf, " 3: %04d zero padded\n", 3); printf("%s", buf);
  196. sprintf(buf, " 3: %-4d left justif.\n", 3); printf("%s", buf);
  197. sprintf(buf, " 3: %4d right justif.\n", 3); printf("%s", buf);
  198. sprintf(buf, "-3: %04d zero padded\n", -3); printf("%s", buf);
  199. sprintf(buf, "-3: %-4d left justif.\n", -3); printf("%s", buf);
  200. sprintf(buf, "-3: %4d right justif.\n", -3); printf("%s", buf);
  201. return 0;
  202. }
/*
* if you compile this file with
* gcc -Wall $(YOUR_C_OPTIONS) -DTEST_PRINTF -c printf.c
* you will get a normal warning:
* printf.c:214: warning: spurious trailing `%' in format
* this line is testing an invalid % at the end of the format string.
*
* this should display (on 32bit int machine) :
*
* Hello world!
* printf test
* (null) is null pointer
* 5 = 5
* -2147483647 = - max int
* char a = 'a'
* hex ff = ff
* hex 00 = 00
* signed -3 = unsigned 4294967293 = hex fffffffd
* 0 message(s)
* 0 message(s) with %
* justif: "left "
* justif: " right"
* 3: 0003 zero padded
* 3: 3 left justif.
* 3: 3 right justif.
* -3: -003 zero padded
* -3: -3 left justif.
* -3: -3 right justif.
*/
#endif
Last edited by Ancient Dragon; May 31st, 2007 at 5:49 am. Reason: URL snipped and added code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: explanation for source code of printf

 
0
  #2
May 31st, 2007
now that you have posted all that code, what, if anything, is your question?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 12
Reputation: narendharg is an unknown quantity at this point 
Solved Threads: 0
narendharg narendharg is offline Offline
Newbie Poster

Can any one help me to solve this problem

 
0
  #3
Jun 3rd, 2007
Normally we use printf to print a statement in C-Language,but how can we print a statement without using a printf in LINUX.

Could Some one tell me the addresses of the output window in LINUX,where we view the output of any C-Program.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: explanation for source code of printf

 
0
  #4
Jun 3rd, 2007
> but how can we print a statement without using a printf in LINUX.
Well given say
char *p = "this is a message";

You can have
puts(p);

or
while ( *p ) putchar(*p++);

or
write(1,p,strlen(p));

> Could Some one tell me the addresses of the output window in LINUX
There's no such thing. The whole of Linux is based on file descriptors, which could be anything from
- a real terminal
- a pseudo terminal
- a file
- another program.

If you're thinking of DOS, where you could cause things to appear on screen simply by writing some data to say b800:0000, then you're out of luck. There is no equivalent of that in Linux, or any other modern OS for that matter.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: explanation for source code of printf

 
0
  #5
Jun 3rd, 2007
In *nix you can use curses library which allows you to put characters on the screen in random locations. I also did it over 10 years ago using termio but I don't remember how I did it any more.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 12
Reputation: narendharg is an unknown quantity at this point 
Solved Threads: 0
narendharg narendharg is offline Offline
Newbie Poster

Re: explanation for source code of printf

 
0
  #6
Jun 4th, 2007
I would thank you for replying to my query ,Could you please give the source code for system call function write().
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: explanation for source code of printf

 
0
  #7
Jun 4th, 2007
I would imagine it's somewhere in the source code nearby where you found the source code for printf()

Where exactly is this question leading?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 12
Reputation: narendharg is an unknown quantity at this point 
Solved Threads: 0
narendharg narendharg is offline Offline
Newbie Poster

Re: explanation for source code of printf

 
0
  #8
Jun 5th, 2007
I want to know how the values are printed on the output window i.e the code for write() or printf().
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: explanation for source code of printf

 
0
  #9
Jun 5th, 2007
What difference does it make how exactly it is done? The C standards do not dictate how its done so each compiler might do it differently. The characters are ultimately sent to the os and let the os display then on whatever terminal(s) happens to be attached to the computer.

If you really think you can read and understand the source code for write then you can get gcc version from www.gnu.org where they will give you free all the source code for their compiler. But be prepared for heavy-duty reading which very vew if anyone will help you.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 2
Reputation: hneel is an unknown quantity at this point 
Solved Threads: 0
hneel hneel is offline Offline
Newbie Poster

Re: explanation for source code of printf

 
0
  #10
May 23rd, 2008
Thanks for the code. I was googl'ing for a solution and this was just what I needed.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC