473,372 Members | 1,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

how can i make functions which behaves like printf

how can i make functions which behaves like printf.
i mean the function should be capable of accepting variable number of
arguments and of varaible types.
thanx
kaush
Nov 14 '05 #1
5 1936
Kaush wrote:

how can i make functions which behaves like printf.
i mean the function should be capable of accepting variable number of
arguments and of varaible types.


/* BEGIN std_io.h */

#ifndef H_STD_IO
#define H_STD_IO

#include <stdarg.h>
#include <stdio.h>
/*
** Only 4 features from stdio.h, are used in std_io.c:
**
** EOF
** FILE
** stdout
** putc(c, stream)
*/

#define put_c(c, stream) (putc((c), (stream)))
#define put_char(c) (put_c((c), stdout))

int (put_c)(int, FILE *);
int fput_c(int, FILE *);
int (put_char)(int);
int put_s(const char *);
int fput_s(const char *, FILE *);
int min_printf(char *, ...);
/*
** Only 5 different conversion specifiers
** are supported for min_printf: % c d s u
** and no fancy stuff.
*/

#endif

/* END std_io.h */

/* BEGIN std_io.c */

#include "std_io.h"

static int put_u(unsigned);
static int put_u_plus_1(unsigned);
static int put_d(int);

int (put_c)(int c, FILE *stream)
{
return put_c(c, stream);
}

int fput_c(int c, FILE *stream)
{
return put_c(c, stream);
}

int (put_char)(int c)
{
return put_c(c, stdout);
}

int put_s(const char *s)
{
while (*s != '\0') {
if (put_char(*s) == EOF) {
return EOF;
}
++s;
}
return put_char('\n');
}

int fput_s(const char *s, FILE *stream)
{
const char *const p = s;

while (*s != '\0') {
if (put_c(*s, stream) == EOF) {
return EOF;
}
++s;
}
return s - p;
}

static int put_u(unsigned integer)
{
int count;

if (integer / 10 != 0) {
count = put_u(integer / 10);
if (count == EOF) {
return EOF;
}
} else {
count = 0;
}
return put_char(integer % 10 + '0') == EOF ? EOF : count + 1;
}

static int put_u_plus_1(unsigned integer)
{
unsigned digit;
int count;

digit = (integer % 10 + 1) % 10;
integer /= 10;
if (integer != 0) {
count = digit == 0 ? put_u_plus_1(integer) : put_u(integer);
if (count == EOF) {
return EOF;
}
} else {
if (digit == 0) {
if (put_char('1') == EOF) {
return EOF;
}
count = 1;
} else {
count = 0;
}
}
return put_char('0' + digit) == EOF ? EOF : count + 1;
}

static int put_d(int integer)
{
int count;

if (0 > integer) {
if (put_char('-') == EOF) {
return EOF;
}
++integer;
integer = -integer;
count = put_u_plus_1(integer);
return count == EOF ? EOF : count + 1;
} else {
return put_u(integer);
}
}

int min_printf(char *s, ...)
{
int count, increment;
va_list ap;

va_start(ap, s);
for (count = 0; *s != '\0'; ++s) {
if (*s == '%') {
switch (*++s) {
case 'c':
increment =
put_char(va_arg(ap, int)) == EOF ? EOF : 1;
break;
case 'd':
increment = put_d(va_arg(ap, int));
break;
case 's':
increment = fput_s(va_arg(ap, char *), stdout);
break;
case 'u':
increment = put_u(va_arg(ap, unsigned));
break;
default:
increment = put_char(*s) == EOF ? EOF : 1;
break;
}
} else {
increment = put_char(*s) == EOF ? EOF : 1;
}
if (increment == EOF) {
return EOF;
} else {
count += increment;
}
}
va_end(ap);
return count;
}

/* END std_io.c */
--
pete
Nov 14 '05 #2
"Kaush" <ka***@techie.com> wrote in message
news:9f**************************@posting.google.c om...
how can i make functions which behaves like printf.
i mean the function should be capable of accepting variable number of
arguments and of varaible types.


By reading the FAQ or a good C book.

http://www.eskimo.com/~scs/C-faq/q15.4.html

--
Peter

Nov 14 '05 #3
"pete" <pf*****@mindspring.com> wrote (and I shamelessly trimed):

#include <stdio.h>

#define put_c(c, stream) (putc((c), (stream)))

int (put_c)(int c, FILE *stream)
{
return put_c(c, stream);
}


That's really neat, I love the general idea. Is it portable?

Peter
Nov 14 '05 #4
Peter Pichler wrote:

"pete" <pf*****@mindspring.com> wrote (and I shamelessly trimed):

#include <stdio.h>

#define put_c(c, stream) (putc((c), (stream)))

int (put_c)(int c, FILE *stream)
{
return put_c(c, stream);
}
That's really neat, I love the general idea.


I'm glad that you like it. I feel that I understand the stdio
functions better now, especially in terms of their return values.
Is it portable?


It is.

--
pete
Nov 14 '05 #5
pete wrote:

Peter Pichler wrote:

"pete" <pf*****@mindspring.com> wrote (and I shamelessly trimed):

#include <stdio.h>

#define put_c(c, stream) (putc((c), (stream)))

int (put_c)(int c, FILE *stream)
{
return put_c(c, stream);
}
Is it portable?


You can link str_str.c with std_io.c

/* BEGIN str_str.c output */

put_s is a function.
fput_s is a function.
fput_c is a function.
put_char is a macro.
put_c is a macro.

On my system:

puts is a function.
fputs is a function.
fputc is a function.
putchar is a macro.
putc is a macro.

/* END str_str.c output */

/* BEGIN str_str.c */

#include <string.h>
#include "std_io.h"

#define str(s) # s
#define xstr(s) str(s)

int main(void)
{
char *mf[] = {"macro", "function"};

put_s("/* BEGIN str_str.c output */\n");
min_printf("put_s is a %s.\n",
mf[strstr(xstr(put_s(A)), str(put_s(A))) != NULL]);
min_printf("fput_s is a %s.\n",
mf[strstr(xstr(fput_s(A)), str(fput_s(A))) != NULL]);
min_printf("fput_c is a %s.\n",
mf[strstr(xstr(fput_c(A)), str(fput_c(A))) != NULL]);
min_printf("put_char is a %s.\n",
mf[strstr(xstr(put_char(A)), str(put_char(A))) != NULL]);
min_printf("put_c is a %s.\n",
mf[strstr(xstr(put_c(A, B)), str(put_c(A, B))) != NULL]);
put_s("\nOn my system:\n");
min_printf("puts is a %s.\n",
mf[strstr(xstr(puts(A)), str(puts(A))) != NULL]);
min_printf("fputs is a %s.\n",
mf[strstr(xstr(fputs(A)), str(fputs(A))) != NULL]);
min_printf("fputc is a %s.\n",
mf[strstr(xstr(fputc(A)), str(fputc(A))) != NULL]);
min_printf("putchar is a %s.\n",
mf[strstr(xstr(putchar(A)), str(putchar(A))) != NULL]);
min_printf("putc is a %s.\n",
mf[strstr(xstr(putc(A, B)), str(putc(A, B))) != NULL]);
put_s("\n/* END str_str.c output */");
return 0;
}

/* END str_str.c */
--
pete
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: zbyszek | last post by:
I am working with a large C++ program which, for reasons of backward compatibility, uses C's printf and fprintf rather than iostreams. For a certain type of build I want to provide new functions...
7
by: Aguilar, James | last post by:
I've heard that virtual functions are relatively ineffecient, especially virtual functions that are small but get called very frequently. Could someone describe for me the process by which the...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
4
by: kimimaro | last post by:
Thank you for your help I think I know what the problem is. Firstly the add_record cannot read the record.txt or something if the record.txt (in which I used it to store the details of each record)...
5
by: Christopher Benson-Manica | last post by:
Do variadic functions (such as printf()) always scan the format string for format specifiers, even when there are no additional arguments? If it is instead a QoI issue, what would be true for a...
31
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
4
by: msolem | last post by:
I have some code where there are a set of functions that return pointers to each other. I'm having a bit of a hard time figuring out the correct type to use to do that. The code below works but...
21
by: Lane Straatman | last post by:
#include <stdio.h> #include <stdlib.h> #include <complex.h> int main(void) { _Complex z1, z2, z3; z1 = .4 + .7i; z2 = pow(z1, 2); z3 = z1 * z1; printf("%f, %f \n", z1);
0
hqprog
by: hqprog | last post by:
Having search extensively I've learned the two functions timegm and gmtime_r though in the GNU standard C library extend the ISO standard. I need to use these two functions in myprog.c (on pc - ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.