Solved

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

Posted on 2014-03-10
5
411 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 
LVL 1

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
 
LVL 1

Author Closing Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

730 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