473,411 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Functions as a single Macro

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);
Oct 16 '08 #1
8 1682
gpraghuram
1,275 Expert 1GB
You want to substitute sprintf with a macro?
or you are looking for something else?


Raghu
Oct 16 '08 #2
donbock
2,426 Expert 2GB
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?
Oct 16 '08 #3
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
Oct 16 '08 #4
gpraghuram
1,275 Expert 1GB
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
Oct 16 '08 #5
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);
Oct 16 '08 #6
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
Oct 17 '08 #7
Banfa
9,065 Expert Mod 8TB
C89: can't do it.
Not entirely true, there is a hack that goes like this

Expand|Select|Wrap|Line Numbers
  1. #define PRINT(x) printf x
  2.  
  3.  
  4. PRINT(("This is a %s", "Hack"));
  5. PRINT(("The answer is %d, in hex %x", 42, 42));
  6.  
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.
Oct 17 '08 #8
donbock
2,426 Expert 2GB
Not entirely true, there is a hack that goes like this

Expand|Select|Wrap|Line Numbers
  1. #define PRINT(x) printf x
  2.  
  3.  
  4. PRINT(("This is a %s", "Hack"));
  5. PRINT(("The answer is %d, in hex %x", 42, 42));
  6.  
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
Oct 17 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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,...
8
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 :
19
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...
47
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...
110
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...
5
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...
3
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...
12
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?...
60
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...
0
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
0
BarryA
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...
1
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...
0
marktang
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,...
0
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...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.