A simple mathematical equation does not work in C programming [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 8 years ago.
Improve this question
The following equation gives error message but it should not.
#include<stdio.h>
int main()
{
int a;
a=12 + 10/2 + 3*2 – 5;
printf("a=%d",a);
return 0;
}
What is wrong?

You wrote a dash – instead of a hyphen -. Spot the difference:
... 3*2 – 5;
... 3*2 - 5;

It seems that you have copied pasted the code from elsewhere. In expression
a=12 + 10/2 + 3*2 – 5;
^ is not minus sign
Change it to -

In your expression that's not a minus sign, judging by eye it's an en-dash, which is not accepted by the compiler. Don't write code in Word or "normal" word processors, use plain text editors.

Related

I tried running this code from Learn-C.org in Sublime Text and I get these errors [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 6 months ago.
Improve this question
I just signed up and at the moment I'm learning C language.
I messed around with formatting and trying different variables. That didn't work. If someone could point out the obvious here, I'd greatly appreciate it.
#include <stdio.h>
int main() {
int a = 3;
float b = 4.5;
double c = 5.25;
float sum;
sum = a + b + c;
printf("The sum of a, b, and c is %f.", sum);
return 0;
}
error: expected ',' or ';' before 'float'
float sum;
error: 'sum' undeclared (first use in this function)
sum = a + b + c;
note: each undeclared identifier is reported only once for each function it appears in
The code doesn't have any syntax errors and runs successfully as seen here
https://onlinegdb.com/w57JOqkgc
Make sure you save the changes you made before compiling / running.

Why this code is not working (if-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 1 year ago.
Improve this question
Why "if" is not accepting the condition (both "x" and "n" are constants after all).
#include <stdio.h>
int main(){
int x,n;
scanf("%d %d",&x,&n)
if(x==(4n-3))
printf("%d",x);
return 0;
}
Compile and you get this:
error: expected ';' before 'if'
4 | scanf("%d %d",&x,&n)
| ^
| ;
The little ^ points out the exact location of the bug and tells you to write a semi colon there. Go to that location that the compiler points out and do as it says.
Then:
error: invalid suffix "n" on integer constant
5 | if(x==(4n-3))
This means that 4n is nonsense, it is not valid C. You need to write 4*n-3.
You aren't writing a math statement here
It should be if(x==(4*n-3))

Is side-effect operator allowed in C expression with more than a variable? [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 use side effect operator in my expression which does not have just a variable. My program was compiled successfully but I got a runtime error "Segmentation fault"
Here is my code:
int main()
{
int x = 1;
printf(1 + (x++));
return 0;
}
C requires you to format the string, this way it knows what it should print. What you have in your example is nothing but memory addresses, which makes the C compiler confused.
int main()
{
int x = 1;
printf("%d\n", (1 + (x++)));
return 0;
}

Basic While loops doesn't print 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
I'm trying to create a while loop in C, it says that build is successful, however it doesn't print out anything. I don't really see whats wrong, it doesn't show anything in the console.
int main()
{
int w = 0;
while (w >=100){
printf("w = %i" , w);
w++;
}
return 0;
}
You define w=0 and in the next line you write "while w is greater or equal than 100", which cannot work.
Try
while (w <= 100)

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