Why does this if statement not run? [closed] - c

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 6 years ago.
Improve this question
I have this C code, and neither the if or else block is running when I go through it with the debugger
Here is the code:
if(P2IN & BIT4 == BIT4 ){
car_lock ^= BIT0;
is_pressed = 1;
}else{
is_pressed = 0;
}
At this point in the code, P2IN = 00010000
It seems like neither the if or else block is running, what am I missing?

The problem is in your if statement. The == operator takes higher precedence than the &, so what's really being evaluated is:
(P2IN & (BIT4 == BIT4))
You need to change your code to:
if ((P2IN & BIT4) == BIT4)
There's a useful webpage about operator precedence here.

Related

Why does this show me an error bad for loop on my pi 4 (begginer, first timer with normal 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 5 months ago.
Improve this question
#include <wiringPi.h>
#include <stdio.h>
#define ledPin 0
main()
{
wiringPiSetup()
int x;
for(x=0; x<4; x+1)
{
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
}
the error is on line 7 and i've been stuck on it for 2 days (i code in geany)
It sounds like you're getting a compile error on line 8:
wiringPiSetup() /* <-- You need to end the line with ";" */
Your loop should look like this:
for(x=0; x<4; x++) {...} /* "x+1" doesn't change the value of "x", so the loop will never terminate */
The problem is in your for loop.
for(x=0; x<4; x+1)
The third parameter in brackets does not do anything. You probably wanted to increment x and you will do that with x++ or x+=1.
x+1 will return the value, but that will not be stored anywhere.

When I run my code it shows some errors on terminal. Could any can tell me what is the error stands for? How can I debug it? [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 4 years ago.
Improve this question
Errors:
Test isPower2(0[0x0]) failed.
Gives 1[0x1]. Should be 0[0x0]
Code:
int isPower2(int x) {
int nonNega = (x>>31);
int result = !((x & (x-1)) ^ nonNega);
return result;
}
Your isPower2(0) returns true (1) but 0 is not a power of 2. So the expected result would be false (0).

C program: if statement within switch statement [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
I am trying to create a program that will switch between "modes". For example here are little snippet of the code:
int main()
{
int mode,input;
mode = 1;
for(;;)
{
scanf("%d", &input);
switch(input)
case 1:
if(mode = 1)
{
//statements go here;
mode = 2;
}
else
{
//statements go here;
mode = 1;
}
break;
}
}
So what I'm trying to do is get the program to switch between mode 1 and mode 2 by the input of the 1 button. However each time I press the number 1 key, it will only print the statements of mode 1 but won't switch to mode 2 and print out the statements for mode 2 if i press the number 1 button a second time. Is there something fundamentally wrong with my code?
*restriction: I must use the switch statements in the program.
To test the value of a variable you need double equals:
if(mode == 1)

Meltdown PoC Detailed Code Review [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I read all the week-end about Meltdown and Spectre
I also have already read the .pdfs for Spectre and Meltdown
which are Must Read for anyone seeking more knowledge about these exploits but unfortunately don't provide detailed explanations on the code.
I found various PoC on github, which were very interesting but I lack the knowledge to fully understand it. I would be thanksful about more explanation on specific parts:
From this link https://github.com/dendisuhubdy/meltdown/blob/master/src/poc.c , and other git repositories as well, there are many interesting parts in the conception of this exploit.
Time reads
/* Time reads. Order is lightly mixed up to prevent stride prediction */
for (i = 0; i < 256; i++) {
mix_i = ((i * 167) + 13) & 255;
addr = &array2[mix_i * 512];
time1 = __rdtscp(&junk); /* READ TIMER */
junk = *addr; /* MEMORY ACCESS TO TIME */
time2 = __rdtscp(&junk) - time1; /* READ TIMER & COMPUTE ELAPSED TIME */
if (time2 <= CACHE_HIT_THRESHOLD && mix_i != array1[tries % array1_size])
results[mix_i]++; /* cache hit - add +1 to score for this value */
}
why do we use prime numbers 167 and 13 ?
/* Locate highest & second-highest results results tallies in j/k */
Why do we care about getting the max value ?
Other parts explanations are very welcome as well !!

Basic C program does not display anything [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 7 years ago.
Improve this question
What does my program not output anything. I have tried other similar versions of solutions for project euler problem 1. I don't need the answer I would just like to know why their is no output. After I compile with gcc and execute the file it seems like is freezes with no output. I have to ctrl-z to kill the program.
#include <stdio.h>
/* Project Euler Problem 1 */
int main()
{
int sum = 0;
int i = 0;
while (i <= 1000);
{
if (i % 3 == 0 || i % 5 == 0);
{
sum += i;
}
i++;
}
printf("%d\n", sum);
return 0;
}
You are closing the while without doing any instruction by putting a semi-colon:
while (i <= 1000);
You should drop the semicolon.
The same for the if instruction:
if (i % 3 == 0 || i % 5 == 0);

Resources