?
Solved

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

Posted on 2014-03-10
5
Medium Priority
?
427 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 1300 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 700 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

764 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