Solved

printf can not be a macro defined in terms of fprintf ?

Posted on 2014-03-10
5
404 Views
Last Modified: 2014-03-10
Please explain following sentence from 'C' language book:

"Note, though, that unlike getchar and fgetc, printf can not be a macro defined in terms of fprintf, because macros can not have a variable number of arguments."

Please provide example of how there will be variable number of arguments if printf is a macro defined in terms of fprintf ?
0
Comment
Question by:naseeam
  • 2
  • 2
5 Comments
 
LVL 25

Expert Comment

by:SStory
ID: 39918397
printf can have variable number of args.

printf("Good morning, %s, did you know that %d + %d = %d?",name,5,5,10);
printf("Good morning, %s",name);

Here printf has 4 args (5 including the string) in the first, and 1 or 2 in the second. It is saying you can't do this in the macro because it won't allow variable args..I think.
0
 

Author Comment

by:naseeam
ID: 39918478
Please provide example of printf being a macro defined in terms of fprintf.  Then, explain why it can't be done.
0
 
LVL 86

Accepted Solution

by:
jkr earned 325 total points
ID: 39919055
Well, you can create 'getchar()' as a macro from 'fgetc()' like

#define getchar fgetc(stdin)

Open in new window


The same or something similar is isn't possible because 'printf()' takes a variable number of 0 up to n arguments, which cannot be taken care of with a macro, since the latter always takes a defined number of arguments (with the notable exception of gcc, but that's a non-standard compiler extendion).
0
 
LVL 25

Assisted Solution

by:SStory
SStory earned 175 total points
ID: 39919199
#define dontwork fprintf(stdin,txt)

It won't work because fprintf can take tons of args based on the format string.  For every %d or %s etc in the string there must be a ,arg1,arg2...,argn

I don not know how much better anyone can explain this.  The above macro expects a single text string with no variables.  How many would you make?  You can not know.  Normally functions like that have an argv array of items or something and maybe the count argc...
0
 

Author Closing Comment

by:naseeam
ID: 39919273
Great explanation with great examples.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now