This question already has answers here:
C macros and use of arguments in parentheses
(2 answers)
Closed 3 years ago.
I have a question about C behavior I don't understand...
#define KB 1024
#define FOUR_KB 4*KB
int main() {
uint32_t a;
uint32_t b = 24576;
a = ceil((float)b/FOUR_KB);
//want to get number of 4K transfers
//(and possibly last transfer that is less than than 4K)
}
At this point I would expect a to be 6, but result I get is 96.
Any explanation for this?
Multiplication and division have the same priority but compiler compute from left to right when push to stack :
24576÷(4×1024) = 6 // what you expect
24576÷4×1024 = 6291456 // what compiler compute, this mean : 6144 * 1024
You should use of parenthesis and #define like this :
#define FOUR_KB (4*KB)
Related
This question already has answers here:
How to compare signed and unsigned (and avoiding issues)
(1 answer)
Why is this happening with the sizeof operator when comparing with a negative number? [duplicate]
(2 answers)
Closed 11 months ago.
#include<stdio.h>
int main(){
int i;
// printf("%d",sizeof(i)) ;
printf("%d",(sizeof(i) > (-1))) ;
return 0;}
why does the code print 0 when sizeof(i) gives 4 in 64 bit OS?
why does (sizeof(i) > (-1))) gives false(0) ?
Use a better compiler and enable warnings. Under any sane compiler you should have gotten a warning about comparing an unsigned and a signed value.
This should be closer to what you want:
printf("%d", (int)sizeof(i) > -1);
Or at least this:
printf("%d", sizeof(i) >= 0);
However your code is a no-op anyway, because it's impossible to have a negative size of a type.
This question already has an answer here:
Macros not producing required result
(1 answer)
Closed 1 year ago.
#include <stdio.h>
#define SQRT(X)X*X
int main(){
int x=16/SQRT(4);
printf("%d",x);
return 0;
}
this output is 16 why? firstly i defined macro and then try do calculation
Currently int x=16/SQRT(4); will expand to int x=16/4*4; which is clearly 16. Use brackets to ensure the macro expansion is as intended:
#define SQRT(X) ((X)*(X))
Aside: SQRT is an odd name for the macro as that usually means "square root". SQR or SQUARE would be a more appropriate name.
This question already has answers here:
The need for parentheses in macros in C [duplicate]
(8 answers)
Closed 2 years ago.
#include<stdio.h>
#define sqr(x) x*x
int main()
{
int a = 16/sqr(4);
printf("%d", a);
}
if I am not wrong the output should be 1
as ,a = 16/sqr(4) gives 16/16 => 1
but the output is 16
this is happening only when I take division operator(/) ,I am getting correct output when using other operators
may I know the reason? .. and thank you
16 / 4 * 4 is (16 / 4) * 4 = 16
Moral: always take care with macros. In your case you should enclose in parenthesis:
#define sqr(x) ((x)*(x))
There are still problems with this macro as it evaluates the argument twice.
For instance:
int read_int();
int a = 16 / sqr(read_int());
Will unexpectedly evaluate read_int() twice as it expands to:
int a = 16 / ((read_int() * (read_int());
The advice would be to use functions for this. Make it inline to give the compiler better chances to optimize it.
a = 16/sqr(4);
Expanded the above expression gives:
a = 16 / 4 * 4
Which is 16 based on normal mathematical order of operations.
That's why macros should always be enclosed in parentheses:
#define sqr(x) ((x) * (x))
Are you trying to make a function definiton? If yes, then i don't think it should be on a #define. Instead, make a function sqr(x). Here's how:
#include <stdio.h>
int sqr(int x)
{
return x*x;
}
int main()
{
int a = 16/sqr(4);
printf("%d", a);
}
And you'll get the result 1.
Edit: but anyway, you've already get the solution with macros. This is just another way to do what you want.
This question already has an answer here:
What does [ N ... M ] mean in C aggregate initializers?
(1 answer)
Closed 5 years ago.
What does this line mean:
[0 ... 255] = &&default_label
in the definition:
static const void *jumptable[256] = {
[0 ... 255] = &&default_label,
/* Now overwrite non-defaults ... */
/* 32 bit ALU operations */
[BPF_ALU | BPF_ADD | BPF_X] = &&ALU_ADD_X,
…
};
http://lxr.devzen.net/source/xref/linux-4.8.15/kernel/bpf/core.c#473
The [0 ... 255] notation is a GCC extension to the designated initializer syntax (that is desperately needed in standard C). It sets elements 0 through 255 of the array (of void * values) to the address of the label default_label (another GCC syntax extension, but this is one that is not desperately needed in standard C).
This question already has answers here:
Macro vs Function in C
(11 answers)
Closed 6 years ago.
Please explain the output — I am getting the output as 20,
but it should be 64 if I am not wrong.
#include<stdio.h>
#define SQUARE(X) (X*X)
main()
{
int a, b=6;
a = SQUARE(b+2);
printf("\n%d", a);
}
The correct result is 20.
Macros are simple text substitutions.
To see that the result is 20, just replace the X with b+2. Then you have:
b+2*b+2
as b is 6 it is
6+2*6+2
which is 20
When using macros it is important to use parenthesis so the macro should look
# define SQUARE(X) ((X)*(X))
Then the result will be 64 as the evaluation is
(b+2)*(b+2)
(6+2)*(6+2)
8*8
64