Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
What is the mathematical equivalent equation of following Macro
#define SQ(a) (a*a )
int answer SQ(2 + 3 );
Output is 11 for this case
and for
int answer SQ(2 + 4);
is 14 I can't figure out the equation from outputs.
The macro you defined lacks brackets to keep the arithmetic working as you want. Remember preprocessor macros are doing text replacement solely. So what you'll get from calling it as you shown expands to
int answer (2 + 4 * 2 + 4);
and according operator precedence the result is 14.
Write your macro as
#define SQ(a) ((a)*(a))
to get the result you expected.
SQ(2 + 4) expands to 2+4*2+4 = 14 because you have not used brackets in your macro. It is a generic macro pitfall for newcomers as macros are not quite safe in this respect as they are just processed by the preprocessor as raw string.
You should write something like this:
#define SQ(a) ((a)*(a))
and that will expand to: (2+4)*(2+4) = 36.
The same logic holds true If you replace 4 with 3, you will get to the 11, and with the corrected macro 25.
That being said, you really should not initialize an integer like that. The general way is to use explicit assignment.
Related
This question already has answers here:
Confused by squaring macro SQR in c [duplicate]
(3 answers)
The need for parentheses in macros in C [duplicate]
(8 answers)
Closed 2 years ago.
I have defined a macro to find the square of a number.
#include<iostream>
#define square(x) x*x
using namespace std;
int main()
{
int a = square(2+3);
cout<<a<<endl;
}
The output in the above case is 11. I have replaced the expression in the brackets with (3+2) then also the output is 11. Similarly for (4+2) and (2+4) the output is 14.
I have replaced the '+' with '*' then it is giving the normal output like it calculated the expression in the brackets first and calculating the square of that.
Can any one tell me how to generalize the output like what is the order of execution?
With #define square(x) x*x, this square(2+3) turns into
2+3*2+3. Which of course causes unexpected results because of preference of * over +. The problem does not occur with *, because the order of preference in that case does not matter.
You probably want #define square(x) (x)*(x), in order to get (2+3)*(2+3).
Or, less vulnerable to indirect problems of similar nature,
#define square(x) ((x)*(x)), in order to get ((2+3)*(2+3)).
Even better, because macros really offer a lot of traps like this one to get caught in, you should simply use a cleanly defined function. With modern compilers most of the reasons to use macros are obsolete.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I had try to compile this code but it shows this error "term does not evaluate to a function taking 0 arguments"
I'm completely new to programming so please help me out here.
For reference, the problem function seems to be:
void traps_rand()
{
while (player!=treasure)
srand((unsigned)time(0));
xt1=(rand %6()+1);
xt2=(rand %6()+1);
xt3=(rand %6()+1);
yt1=(rand %8()+1);
yt2=(rand %8()+1);
yt3=(rand %8()+1);
...
...
...
I'm pretty sure this is what you want if you are trying to generate a random number between 1-6:
xt1 = rand() % 6 + 1;
The statement above executes the function rand (as noted by the parentheses), then does modulo 6 on the result before adding 1.
Your original statement:
xt1=(rand %6()+1);
is attempting to invoke the function "6" and use that as the modulus with the address of rand. Then add 1. It hits as error because there is no function named 6. You can't name functions starting with numbers anyway.
Change rand %6()+1 to rand() %6 + 1. It looks like you are using a variable named rand and calling a function named 6(), but what you really want is to call rand() and mod it by 6 (+1).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm studying for a SAS certification exam, and I came across an unexplained behavior. Note the data step below:
data D;
A+1;
A+1;
A+1;
run;
Question 1: Why this step does not result in error?
Question 2: Why a variable A is created, and its value is 3 and not missing?
Question 3: Why when I change + for - , it results in error?
I have searched about it and i couldn't find nothing, even in SAS documentation
A+1 is sum statement initially A or anything in that form is automatically set to 0 and in your second line of code it becomes 0 +1 = 1 then this value is in A is retained that is A becomes 1 and then when you add 1 in your 3 line of code becomes 2 and then 3. There is nothing of sort is there for -, so it errors when you do A-1, becomes A is not defined, where as in A +1 A is automatically set to 0. Below is the documentation for Sum statement
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000289454.htm.
Please see in below comment of #longfish explains to do the samething for -1, you need to do A+-1
That is a SUM statement. The syntax is
variable + expression ;
That is why replacing the + with - did not work. It no longer followed the pattern above. If you want to subtract then negate the expression.
variable + - (expression) ;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm comparing two strings with strcmp in the following manner:
long t=1011;
char tc[10], tcr[10];
ltoa(t,tc,10);
cout<<tc<<endl; //prints 1011
strcpy(tcr, strrev(tc));
cout<<tcr<<endl; //prints 1101
cout<<strcmp(tc,tcr);
This gives me a result of 0, which indicates that the strings are equal. However, when I try:
cout<<strcmp("1011", "1101"); // prints -1 thats okay
I get the expected value of -1. What's am I doing wrong? I am using devc++ compiler version 4.9.9.2
It depends on how function strrev is defined, If it reverses the argument in place then the result is expected because tc was reversed.
For example function strrev can be declared the following way
char * strrev( char *s );
and the return value and the value of the argument will be equal.
Take into account that strrev is not a standard function.
If you change your code like so:
long t=1011;
char tc[10], tcr[10];
ltoa(t,tc,10);
strcpy(tcr, strrev(tc));
cout<<tc<<endl;
cout<<tcr<<endl;
cout<<strcmp(tc,tcr);
then you'll see that tc and tcr are the same. strrev reverses the input string in place, and 1101 is printed twice.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am trying to build my code but getting error in below lines in header file
sample.h
1 #ifndef FORMAT_STRING_H
2 #define FORMAT_STRING_H
3
4 #define FORMAT_STR( ovr, x, y, ... ) \
5 { \
6 char buf[100]; memset(buf, 0, 100);\
7 using namespace std; \
8 snprintf(buf, 99, __VA_ARGS__); \
9 ovr->drawStr( x, y, buf );\
10 }
11 #endif //FORMAT_STRING_H
and getting error at line 4 error: expected an identifier .
I am not able to understand what is the problem exactly in mentioned line .
I am using Ti DSP C6000 Code Generation Tools 7.3.0B3 compiler .
Compiling C++ code .
It seems your compiler does not support a function-like macro with variable number of arguments.
It is very simple to check this. Write for example
#define FORMAT_STR( ovr, x, y, ... )\
{\
}
and do not call it in the code. If the compiler will issue the same error then indeed it does not support such macros.