C-preprocessor #define SQR(x) ( x * x ) [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I think I'm going crazy. I'm learning C-preprocessor and I can't figure this problem out, I defined SQR(x) (X*X) purposely without brackets
#include <stdio.h>
#define SQR(x) (x * x)
int main()
{
int counter; /* counter for loop */
for(counter = 0; counter < 5; ++counter)
{
printf("x %d, x squared %d\n",counter+1, SQR(counter+1))
}
return (0);
}
In one moment in loop counter = 3. Then it should be 3 + 1 * 3 + 1 and that is equal to 7, but the output says 5.
Here is output of the code above:
x 1, x squared 1
x 2, x squared 3
x 3, x squared 5
x 4, x squared 7
x 5, x squared 9
Process returned 0 (0x0) execution time : 0.020 s
Press any key to continue.
I'm clearly missing something but I can't figure it out.

When counter=3, it prints as 4 because you print counter+1. Use:
printf("x %d, x squared %d\n",counter, SQR(counter+1));
to see what the actual value of counter is.

Aside from the most obvious error pointed out in Ignaus' answer, you have a second, more insidious and more dangerous error here. Do the substitution that the preprocessor will do on SQR:
SQR(counter+1)
becomes:
counter+1 * counter+1
which will be executed as if it were written like this:
counter + (1 * counter) + 1
That is obviously incorrect and not what you intended. With macros, you should always enclose the arguments in parentheses. So your macro should look like this:
#define SQR(x) ((x) * (x))
Now do the substitution again and you get:
((counter+1) * (counter+1))
which is correct. Make it a habit to always include macro arguments in parentheses to avoid these sorts of errors.

Related

Why does my summation program behaves weird? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Yesterday, I had to do a math introduction test, where I had to calculate the cardinality of a summation. I was lazy to write it out and I thought I'd just write a small C program to solve it. I felt confident that'll work, since I did much more complex programs, but I just can't get it to work.
#include <stdio.h>
int main(){
int i = 1;
int n = 2 * i + 1;
while(n <= 36){
printf("%d\n", n);
i++;
}
return 0;
}
In theory, there should have been the sequence "3, 5, 7, 9, ...", but all I get is "3, 3, 3, 3, ...". It's only not working if I'm using the variable n, if I replace it with i within while everything works as I would expect.
What am I missing?
This:
int n = 2 * i + 1;
Is not a formula for n which gets calculated every time n is used. It sets n to the value of 2 * i + 1 == 2 * 1 + 1 == 2 + 1 == 3 at the time the statement is encountered and that's it. So n never changes inside of the loop and you end up with an infinite loop.
Move the assignment to inside the loop:
while(n <= 36){
printf("%d\n", n);
i++;
n = 2 * i + 1;
}
There is no change of value n in the while loop and so it never fail the condition n <= 36. Maybe you want to do this way
while(n <= 36){
printf("%d\n", n);
i++;
n = 2 * i + 1;
}
It always gives 3 as a result because you do not update n at each iteration. n is calculated once at the beginning. You should calculate n again each time i is incremented.

integer divided by other integer constant defined by macro returns incorrect in C? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
in the following code the output is 30 ! , it should be 0. am I missing something?
#include <stdio.h>
#define L 30
#define N2 L * L
int main() {
unsigned int id = 30;
printf("k = %d\n", id/N2); //this prints 30 but 30/900 should be zero!
printf("k = %d\n", id/900); //this prints 0 OK!
return 0;
}
Order of operations:
30 / 30 * 30 is 1 * 30. 30 / 900 is 0.
You need to parenthesize in your macro to get the right behaviour:
#define N2 (L * L)
#define N2 (L * L)
or
printf("k = %d\n", id/(N2));
would do the trick.

Understanding Simple For Loop Code in C [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 6 years ago.
Improve this question
I'm beginner in C programming (just started) and i need help from you to understand the output of this very simple code:
int main()
{
int x=1;
for (;x<=10;x++);
printf("%d\n",x);
return 0;
}
output is:
11
the same output if x value is <=11
and if x value is 12 or more, it prints the exact value of x (ex: if int x=12; the output is 12).
how did the computer understand this code?
So,
int main()
{
int x=1; // line 1
for (;x<=10;x++); // line 2
printf("%d\n",x); // line 3
return 0; // line 4
}
Line 1 initializes x to 1.
Line 2 keeps increasing x by 1 until it reaches 11. The first semicolon indicates "don't do anything before starting the loop", x<=10 indicates keep going until x > 10 (so when x = 11) and x++ means increase x by 1 each time. If x >= 11, this line gets basically skipped because x is already greater than 10.
Line 3 prints out x to the command line (in this case, x = 11 if x started out less than 11 or just x if x started at >= 11 due to the previous line)
Line 4 means the program was successful, exit the program.
for is this:
for(*init-expr*; *test-expr*; *update-expr*)
*body-statement*
Or rather, commonly, it can be decribed like this:
*init-expr*;
while(*test-expr*){
*body-statement*
*update-expr*;
}
and, your for statement is followed by a semicolon, where body-statement is.So, it is a "null statement", just loop and update x, when finishes the loop, just print the x after loop, so, the output is 11.

Function that's calculating a log - Unused variable error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm writing a function that tries to get the square of a root.
I guess it's easier with an example:
I want to give a number to that function, say 1024 and the function should tell me 12. So always looking for the x here: 1024 = 2^x.
If I gave 255 to the function it should return 7.
Now I guess my maths is pretty okay, but I get an Error saying I didn't use a Variable in Line 6. Could you have a look?
int log_base2(int num)
{
int x = 2;
int count = 0;
for(; x <= num; x * 2 )
{
count++;
}
return count;
}
Error is in line 6 ( for(....))
for(; x <= num; x * 2 )
Here x * 2 calculates its value, and then throws the result out. What you want is probably:
for(; x <= num; x *= 2 )
The error message is perhaps because the compiler optimizes the variable x away as it's useless.
You are not modifying x anywhere. If you want x to become 2 * x in the next iteration you have to change this
for(; x <= num; x * 2 )
to
for(; x <= num; x = 2 * x )

Too many arguments to function 'rand'? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have only used the function twice and it displays the aforementioned error. Can someone explain as to why the compiler does that?
void printrandom()
{
int x = (rand(5)+1);
int y = (rand(5)+1);
printf("%d and %d - a total of %d", x, y, (x+y));
}
It is actually rand(void), which is why you are getting that error.
Try int x = (rand() % 5) + 1;
EDIT as Daniel points out, using % will actually affect the probability. See his link for how to address this issue.
Declaration for rand() function is
int rand(void);
This means that it takes no arguments. Remove 5 from rand. If you want to generate random numbers from 1 to 5, the you can do this as
int x = rand()%5 + 1;

Resources