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
hello=) with this following code i get the exception
error: expected ‘;’ before ‘printf’
#include <stdlib.h>
#include <stdio.h>
int main() {
int i;
scanf("%i", &i);
for(int i=0 ; i<10; i++){
if(i==1) printf("one");
else if(i==2) printf("two");
else if(i==3) printf("three");
else if(i==4)printf("four");
else if(i==5)printf("five");
else if(i==6) printf("six");
else if(i==7) printf("seven");
else if(i==8)printf("eight");
else(i>9) printf("even"+"/n"+"odd");
}
return 0;
}
Can i sum up this code into a sorter form ? And why do i get this exeption?
thank you all
Add some brackets "{}".
Code will looks better and will work.
if(expression) {
} elseif(expression) {
} elseif(expression) {
} else {
}
PS. I know it will better. If this text be comment. But I don't have points reputation yet :(
PS2. #Myst if code will have 9. Code don't printf anything.
else clause can't have a condition. So, you either mean just else or else if (i>9).
Besides, C doesn't have + operator that concatenates strings. You can just leave out the + and the C preprocessor will concatenate adjacent string literals (see C11, 5.1.1.2, 6).
else if(i>9) printf("even" "/n" "odd");
Related
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
Here is my C code. I am not getting any output. Please help. I also tried adding the initialization of inside the main function, then also I am not getting any output.
#include <stdio.h>
int x = 10;
int main()
{
if (x = 20)
{
x = -1;
}
else
{
printf("x not eqaul to 20\n");
}
if (x > 0)
{
printf("x not greather than 0\n");
}
else
{
/* notjing */
}
return 0;
}
So in the first if-statement you wrote if(x=20). This is not a conditunal argument, this is a mathematical operand.
So x will be set to 20; afterworths it will be set to -1. And no printf() will be called.
If think you wanted to use if(x==20).
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 2 years ago.
Improve this question
I am new to C can anyone help me how to fix this code.
I am getting a error:
'else' without a previous if
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20];
char str2[20];
int value;
printf("Enter the string: ");
scanf("%s",str1);
printf("Enter the string: ");
scanf("%s",str2);
value=strcmp(str1,str2);
if (value==0);
printf("The strings are same");
else
{
printf("The strings are not same");
}
return 0;
}
The problem here is that the semicolon after the if statement
if (value==0);
evaluates to: if value is equal to 0, do nothing.
The following line
printf("The strings are same");
not only would get executed every time, but also breaks the if-else apart so that the following else statement does not find any related if statement.
You could use braces here instead to prevent such problems in the future.
if (value == 0) {
printf("The strings are same");
} else {
printf("The strings are not same");
}
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 years ago.
Improve this question
#include <stdio.h>
int main()
{
int i,l,n;
int w[1000];
int h[1000];
scanf ("%d",&l);
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d %d",&w[i],&h[i]);
}
for(i=0;i<n;i++)
{
if((w[i]<l)!!(h[i]<l))
printf("UPLOAD ANOTHER\n");
else(w[i]>=l&&h[i]>=l)
{
if(w[i]==h[i])
printf("ACCEPTED\n");
else
printf ("CROP IT\n");
}
}
return 0;
}
As I am new to coding these lines of code showing me error :-
In function 'main':
18:20: error: expected ')' before '!' token
21:11: error: expected ';' before '{' token
In the first IF statement, you are using !! as OR, but you should be using ||.
Compilers can not know what you are trying to do, but they can tell you what they are expecting based on what they have read so far. Because you used !!, which is the NOT symbol (twice), the compiler only knows that your expression is syntactically incorrect. In this case, it is saying that given this unexpected unary NOT operator, the current expression should have been closed with a ')' symbol.
Compilers are not mind readers, so they cannot know that you meant || (logical OR). Some compilers used to try to guess (and even fix) such typos, and they only made things worse. PL/1 comes to mind, it would attempt to fix compiler errors and this drove people crazy.
The second error on line 21 is because you placed an expression after the "else" which makes no sense to the compiler which is expecting a statement to execute at this point. You can fix this by replacing "else" with "else if". This works because IF is a statement, and that statement expects an expression next. I think this is what you intended:
#include <stdio.h>
int main()
{
int i,l,n;
int w[1000];
int h[1000];
scanf ("%d",&l);
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d %d",&w[i],&h[i]);
}
for(i=0;i<n;i++)
{
if((w[i]<l)||(h[i]<l))
printf("UPLOAD ANOTHER\n");
else if (w[i]>=l&&h[i]>=l)
{
if(w[i]==h[i])
printf("ACCEPTED\n");
else
printf ("CROP IT\n");
}
}
return 0;
}
Once you get more experience with C, you will recognise this kind of error easily, a simple typo can confused the compiler. Good luck!
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 have written a program which is not giving proper result.
main()
{
int i=1,n,s=1;
printf("enter the value of n");
scanf("%d",&n);
while(i<=n)
{
s=s*i;
i++;
if (i==n+1)
{
break;
}
}
printf("factorial of n=",s);
}
it is giving the result as shown in the picture below.
Your problem is in this line:
printf("factorial of n=",s);
This outputs factorial of n=, but it does not simply concatenate the value of s, and there is no placeholder for s, so you actually have too many parameters.
You need a placeholder for the int output:
printf("factorial of n=%d",s);
Without it, your program exits with an error (status 15, when 0 would be normal).
Also, (as Vlad pointed out in his answer) the if (i==n+1) { ... } block is redundant, as your while loop will already exit when i > n.
Write
printf("factorial of n=%d\n",s);
^^
And this code snippet
if (i==n+1)
{
break;
}
is redundant and may be removed.
You could write the loop simpler. For example
while ( n > 1 ) s *= n--;
without a need to use one more variable i.
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
int menu () {
char choice [5];
int i;
int c;
printf("Welcome to your own personal tamaguchi!");
printf("\n 1.Name your tamaguchi.\n");
printf("\n 2.Check health and age.\n");
printf("\n 3.Feed tamaguchi.\n");
printf("\n 4. Exercise with tamaguchi.\n");
printf("Let tamaguchi sleep.n");
printf("\n 5. Close program.\n");
printf("Choose action: ");
scanf("%s", choice);
printf("\n");
for (i=0; choice[i]! = '\0'; i++){
if(!isdigit(choice[i]))
return -1;
}
c = atoi(choice);
return c;
}
They say the problem lies where ! is where choice[i]!='\0'.
I have included stdio, string, time and stdlib.
I don't know what I've done wrong, if you can see the mistake please tell me?
Thanks.
You need to change
for (i=0; choice[i]! = '\0'; i++){
to
for (i=0; choice[i] != '\0'; i++){
^
| //notice here
The operator here is not equal to !=. This is a single operator.
If you write like ! = [with a space in between], that becomes two separate operators, Logical NOT and assignment, thereby producing the error.