This question already has answers here:
To the power of in C? [duplicate]
(7 answers)
Closed 7 years ago.
void sommeascf(n)
for(i=1; i<=n; i++){
result= result+ 1/i^n
}
}
the problem I'm facing is :
result = result + 1/i^n
How can I put the power function into this arithmetic operation ?
^ this is bitwise XOR operator in C . You can write that expression using pow function like this -
result= result+ 1/(pow(i,n));
Note - You need to include header <math.h>
Related
This question already has answers here:
Why does pow(n,2) return 24 when n=5, with my compiler and OS?
(4 answers)
Why does pow(5,2) become 24? [closed]
(3 answers)
Closed 2 years ago.
This code is part of an original code
Why the output of this code displays the number 24?
The output of this Code should be number 25!
Where's the problem with this code?
The compiler used is CodeBlocks
Thanks
int A=7;
A = pow((((A+1)/2)+1),2);
printf("%d", A);
This question already has answers here:
Why does C allow me to call an undeclared function? [duplicate]
(1 answer)
Function assigns incorrect float value in C
(1 answer)
Closed 3 years ago.
Hey guys I was wondering why my program was just returning junk and I found out that I didn't include math.h. But why did the code even compile and run if there isn't any definition for the function ?
#include <stdio.h>
int main()
{
int ka = 0;
ka = sqrt(4);
printf("ha ist %d", ka);
system("PAUSE");
return 0;
}
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 6 years ago.
I am trying the following code:
#include <stdio.h>
int main()
{
int c =0;
c -= --c - c++;
printf("%d \n",c);
return 0;
}
When I compiled and run it using a online c complier (https://www.tutorialspoint.com/compile_c_online.php) the result is -1. But I expected it to be 0.
So, I try it on my local Dev C++ (Windows) and the result is 0.
Should the result be 0 ?
If so, why 2 gcc compilers (ok they are in different plataform) gives me 2 different results ?
I ve been looking for some kind of automatic flag otimization which could produce a different result but I had no success.
THIS IS UNDEFINED BEHAVIOR (3 modifications without sequence points inbetween to the same variable)
This question already has answers here:
Strange behavior of Macro-expansion
(3 answers)
Why is the return value not what I expected in this C program with macro? [duplicate]
(2 answers)
Closed 7 years ago.
#define A 1+2
#define B 4+3
int main()
{
int val = A*B;
printf("%d",val);
}
Here A and B is defined, and val is (A*B). What is the correct output?
Let's trace the expansion and calculation manually.
A*B -> 1+2*4+3 -> 1+8+3 -> 12
As a result, the output will be 12
This is a common issue with Macros. When you define something even as x+y in a macro it is advisable to first rap them in () so as to "protect" the operation.
As such it would be better to define A as (1+2) etc. Otherwise you get the output 1+2*4+3=12 as people have stated above.
This question already has answers here:
C conditional operator ('?') with empty second parameter [duplicate]
(6 answers)
Closed 9 years ago.
#include<stdio.h>
int main()
{
printf("%d\n", 4 ?: 8);
}
According to the C standard this program is invalid because it is missing an expression between the ? and :.But The interesting thing is that there is when I compile the code it is printing 4.how come it will print 4 rather than showing any compile error
This is a gcc extension.
x ? : y
is equivalent to
x ? x : y
See here for detail.