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.
Related
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 4 years ago.
Improve this question
so I've written some codes to add two numbers in C, but I can't figure out how to add three instead without spilling the results into another variable or clobbering the caller's version of the variables?
int add(a,b,c)
int a, b, c;
{ int tempr;
for(;b--;++a); // danger
/*tempr = c+a;*/
tempr = a+c;
return (tempr);
}
Here's how, if you must use a function for some reason.
int add(int a,int b,int c) {
return a+b+c;
}
When you define a function in C, you have to define the type of the parameters as well.
Please note the following:
It is an error to define a variable in a function that has the same name as a function parameter
Although not an error, there's no need for parentheses when calling return (tempr);.
return tempr; is perfectly fine
if you use a for loop, ending it with ; immediately after the for statement will result in the following statement not being a part of the loop. This might not be what you had in mind.
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
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.
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
I am having difficulty getting the real output from a C function. For example:
int max3(int a, int b, int c){
if ((a>b)&&(a>c))
return a;
if ((b>c)&&(b>a))
return b;
return c;
}
Can you give me an idea of how to specify the real output (e.g. tools, algorithms, etc.)?
In this above example, the real output is 6 (in case (a,b,c) = (1,2,6)).
Thank in advance very much.
I'd have probably written it, as it is simple, using the ternary operator:
int max3(int a, int b, int c){
if (a>b)
return (a>c)?a:c;
else
return (b>c)?b:c;
}
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 9 years ago.
Improve this question
I really don't understands this code:
#include <stdio.h>
int main (int argument c, char *argument v[])
{
return 0;
}
What does this code mean? How does it converts to other formation of coding?
This is (almost) the simplest C/C++ program. (It works for both languages.) It does nothing other than return 0, which signifies successful execution.
It should read
int main(int argc, char **argv)