Solved

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

Posted on 2014-03-10
5
403 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

760 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

19 Experts available now in Live!

Get 1:1 Help Now