I want to convert two functions into a single macro in which the sprintf() will have varying arguments and the second function will have the fixed length of arguments
for eg:
1. sprintf(buff,"The numbers are %u,%d, The text is %s", x,y,ch);
func((unsigned int *)buff,strlen(buff), z);
2. sprintf(buff,"hello");
func((unsigned int *)buff,strlen(buff), z);
8 1682
You want to substitute sprintf with a macro?
or you are looking for something else?
Raghu
Are you wondering how to write a macro that has a variable number of arguments? The answer varies:
C89: can't do it.
C99: Yes! It can be done.
Gnu C: Nonportable extension allows for this.
C++: <I don't know>
Does your flavor of C/C++ support inline functions? Can you use that feature rather than a macro?
You want to substitute sprintf with a macro?
or you are looking for something else?
Raghu
I have two functions which i shoud replace with a macro the first function is sprintf and the second function called func which uses the parameter buff from sprintf
where the sprintf has the varying number of arguments
I have two functions which i shoud replace with a macro the first function is sprintf and the second function called func which uses the parameter buff from sprintf
where the sprintf has the varying number of arguments
void LogThis(const char *fmt, ...)
You can use #define sprintf LogThis and i think you can do it.
Raghu
void LogThis(const char *fmt, ...)
You can use #define sprintf LogThis and i think you can do it.
Raghu
Can u put me a single macro for the following two function and what are all the declarations i can made before it
sprintf(buff,"hello");
func((unsigned int *)buff,strlen(buff), z);
Can u put me a single macro for the following two function and what are all the declarations i can made before it
sprintf(buff,"hello");
func((unsigned int *)buff,strlen(buff), z);
Hi Raghuram
ok i think i am not getting your answer
give me a macro to convert the following part
#ifdef LABEL
sprintf(buffer,"--------------------",................);
function((int *)buffer,strlen(buffer),variable);
#endif
----------------- indicates a string containing varying data types like %u, %d etc
................. indicates the variable for whose values the string should take
Banfa 9,065
Expert Mod 8TB
C89: can't do it.
Not entirely true, there is a hack that goes like this -
#define PRINT(x) printf x
-
-
-
PRINT(("This is a %s", "Hack"));
-
PRINT(("The answer is %d, in hex %x", 42, 42));
-
Note the double set of parenthesis, this causes all the parameters inside to be treated as the single parameter x. The parenthesis become part of the parameter x which can be verified by using the alternative definition of
#define PRINT(x) printf( #x "\n" )
I am not sure that is useful in this situation and I would tend to try an avoid using it anyway but the hack exists.
Not entirely true, there is a hack that goes like this -
#define PRINT(x) printf x
-
-
-
PRINT(("This is a %s", "Hack"));
-
PRINT(("The answer is %d, in hex %x", 42, 42));
-
Note the double set of parenthesis, this causes all the parameters inside to be treated as the single parameter x. The parenthesis become part of the parameter x which can be verified by using the alternative definition of
#define PRINT(x) printf( #x "\n" )
I am not sure that is useful in this situation and I would tend to try an avoid using it anyway but the hack exists.
The doubled parentheses work for calling one function, but not so well for a macro that wants to expand into two function calls with each function getting different parameters
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Thomas Matthews |
last post by:
Hi,
I have several translation units (modules) which contain static {local}
variables. These function have short global accessor functions.
I would like to change these functions into macros,...
|
by: Evangelista Sami |
last post by:
hello everybody
let's take this struct
typedef struct {
int i;
int j;
} my_struct;
is there a way to transform this function :
|
by: Ross A. Finlayson |
last post by:
Hi,
I hope you can help me understand the varargs facility.
Say I am programming in ISO C including stdarg.h and I declare a
function as so:
void log_printf(const char* logfilename, const...
|
by: Albert |
last post by:
So structures are useful to group variables, so you can to refer to a
collection as a single entity. Wouldn't it be useful to also have the
ability to collect variable and functions?
Ask K&R...
|
by: Gregory Pietsch |
last post by:
I'm writing a portable implementation of the C standard library for
http://www.clc-wiki.net and I was wondering if someone could check the
functions in math.h for sanity/portability/whatever. I'm...
|
by: krbyxtrm |
last post by:
Hi
is there a way to 'manage' function execution using macros?
#define MY_CALL_MACRO(MacroName)
{ g_MacroStack.push_back(MacroName);
<some code here...>}
such that when i use the...
|
by: Michael Brennan |
last post by:
Hi,
does the standard say if the isalpha, isdigit, etc. functions
in ctype.h are functions or macros?
My old pre-ansi book says they are macros,
but I've seen man pages that say they are...
|
by: sam_cit |
last post by:
Hi Everyone,
I have few questions on inline functions, when i declare a function as
inline, is it for sure that the compiler would replace the function
call with the actual body of the function?...
|
by: jacob navia |
last post by:
Gnu C features some interesting extensions, among others
compound statements that return a value. For instance:
({ int y = foo(); int z;
if (y>0) z = y; else z=-y;
z;
})
A block enclosed by...
|
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
|
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...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |