use macros for arguments [closed] - c

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am trying to create a macro wrapper around a function so I could make the code more intuitive on reading. Something like instead of calling send_message_to_destination(m, d) to write send(m)to(d).
#include <stdio.h>
void send_data_to(int data, int dest)
{
printf("Send %d to %d\n", data, dest);
}
#define send(data)to(destination) send_data_to(data, destination)
int main() {
int data = 5;
int dest = 10;
send(data)to(dest);
}
It is possible to do so?
Do you think this would makes the code more readable or intuitive ?

I agree with the comments (not a good idea), however, you can use something like this. Remember the pre-processor just does a text replace with your macros.
#define to(destination) destination)
#define send(data) send_data_to(data,

No there is no such way to do so with MACROS

Related

Alternatives for str.substr using <cstring> library? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
Hi I was wondering if there is an alternative of str.substring() in string.h. If not, what is an efficient alternative?
Assuming that we all agree that using c++ it is safer and more professional to use the standard library tools.
That said, if we're talking about C and not C++, this should be one of the ways to extract a substring with the "string.h" library:
#include <stdio.h>
#include <string.h>
int main() {
char test[] = "abcdef";
char subtext[3];
memcpy(subtext, &test[1], 2); //Result: "bc"
subtext[2] = '\0';
printf("%s", subtext);
return 0;
}

Integer Overflows in C and Defenses Against Numeric Errors [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to research various integer overflow scenarios in C and I was wondering does the C language provide any defenses against numeric errors and are there any additional classes or libraries in the C language that can help with that? Also, can anyone give me an example of code that results in an integer overflow in C?
No, there are no defenses.
This overflows:
#include <limits.h>
#include <stdio.h>
const int a = INT_MAX - 2;
const int b = INT_MAX - 2;
printf("%d + %d = %d\n", a, b, a + b);
When I tested it it printed -6, but anything could happen I guess.

Obfuscate C code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I came across this obfuscated code recently:
#include <stdio.h>
#define _____(i,s,o,g,r,a,m)(i##r##s##o)
#define _ _____(m,i,n,u,a,l,s)
int _()
{
printf("Hello World!");
return 0;
}
What is happening here? How is it that _() is main()?
Edit:
I was looking for the 'technical term' that has been used here.
_ is replaced by ____(m,i,n,u,a,l,s)
____(m,i,n,u,a,l,s) which is filtered through the macro ____(i,s,o,g,r,a,m)(i##r##s##o)
i##r##s##o pastes the arguments i, r, s, o together to form text. i = m, r = a, s = i, o = n, thus you get main
This technique is called 'token pasting'. It is not something you'll use everyday, but there are times where it can be very useful. See GCC's documentation on token pasting.

Convert string of macro to macro [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How can a string like "EINVAL" be converted into EINVAL?
I have to convert a lot of macro string to their value, what can be done?
Here is the simplest method I can think of:
#include <errno.h>
if (!strcmp(str, "EINVAL"))
value = EINVAL;
No preprocessor trick can convert the string literal to the corresponding symbol.
But you can use the preprocessor to simplify a sequence of such tests:
value = 0;
#define conv(s) do { if (!strcmp(str, #s)) value = s; } while (0)
conv(EINVAL);
conv(ENOMEM);
conv(ERANGE);
conv(EINTR);
#undef conv
A usual, be careful with the preprocessor...

Why does this print 1? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
#include <stdio.h>
int main()
{
int var=0;
for(; var++; printf("%d",var));
printf("%d", var);
}
Please explain to me this C code. How is the output 1?
You might be confused because of the wrong code indentation. Your code is:
for(; var++; printf("%d",var))
;
printf("%d", var);
So you always get the output of the second printf. As var is initialized to 0 and var++ (the for-condition) is always executed, you end up with var==1.

Resources