Basic C program does not display anything [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 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);

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.

Why am I getting an error in this C program? [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 5 months ago.
Improve this question
I am new to C programming but I stumbled on this code
int print(int nb)
{
if (nb < 0)
{
return (0);
}
printf("%d", nb + print(nb - 1));
nb --;
return (nb);
}
int main(void)
{
print(4);
return (0);
}
I ran the code and it gave me an output of 00246
why is that the output that, looking at it logically, the answer is not suppose to start with a 0
print(4) -> print(3) -> print(2) -> print(1) -> print(0) -> print(-1)
print(-1) stops the recursion returning 0, thus a call to printf() is emitted with 0 + 0, which is 0.
print(0) ends with -1 as value, and a call to printf() with 1 + -1 is emitted, which is 0.
etc.

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)

How do i input this table in a text file into C? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
my text file goes something like this:
0 000 fff1
0 121 afaada4
0 000 mm1m1
I've been trying to do something like this:
if (fi) {
while(fscanf(fi,"%d" "%d" "%s",&sigs1,&sigs2,&sigs3)!=EOF);
printf("%d %d %s",sigs1,sigs2,sigs3);
fclose(fi);
}
#include <stdio.h>
int main() {
int sigs1, sigs2;
char * sigs3;
FILE *fi;
fi = fopen("text.txt", "r");
if (fi) {
while(fscanf(fi,"%d " "%d" "%s ",&sigs1,&sigs2,sigs3)!=EOF)
printf("%d %03d %s\n",sigs1,sigs2,sigs3);
fclose(fi);
}
return 0;
}
Test
0 000 fff1
0 121 afaada4
0 000 mm1m1

I don't understand why it is giving this output? [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
#include <stdio.h>
void main ()
{
int i=0;
for (i=0; i<21; i++)
{
switch(i)
{
case 0:
i+=5;
case 1:
i+=2;
case 5:
i+=5;
default:
i+=4;
break;
}
printf("%d ",i);
}
getchar();
}
Now the Output of this program is 16 21 I don't understand why this program is giving this output when the limit of the loop is less than 18 it gives only 16 but when value is greater than 18 output is 16 21 any help
You need to put break statements at the end of each case. Otherwise each case will 'fall through' to the next one.

Resources