-1

Here is my code, the error code is

Expected expression before 'if'

#include <stdio.h>
#include <stdlib.h>

int main()
{

    int friends = 2;

    printf("I have %d friend%s", friends, (if(friends != 1){"s"})
           );

    /*In english you say "friends" when you have 0 or more than one. However if you have 1 you say "friend" without the letter s. I have created a conditional code but there is an error in my print code*/

    return 0;
}
kocica
  • 6,412
  • 2
  • 14
  • 35
sangmin park
  • 547
  • 3
  • 7
  • 16

4 Answers4

2

if is a statement, not an expression. Since a statement cannot be used inside of an expression, the if statement cannot be used either inside of an expression.

However, you can use the ternary operator instead, which is an expression, in order to create a conditional expression:

printf("I have %d friend%s", friends, friends != 1 ? "s" : "");
JFMR
  • 23,265
  • 4
  • 52
  • 76
  • Thanks for your help. I'm a beginner and trying to learn the programming language C on my own. What exactly considers something an expression? Is 'if' conditional statements an expression too? Is that why I can't put it inside printf statement? – sangmin park Aug 12 '17 at 17:06
  • 1
    @sangminpark You have to distinguish between the terms *statement* and *expression*. `if` is an statement and `?:` is and expression. See https://stackoverflow.com/questions/19132/expression-versus-statement – JFMR Aug 12 '17 at 17:11
2

Specific to gcc, statement expressions are allowed. Please refer following:

https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html

Other problem with your code is that if statement does not have a rvalue. So the last statement of the statement expression should be a rvalue. Following code will work fine:

#include <stdio.h>
#include <stdlib.h>

int main()
{

    int friends = 2;

    printf("I have %d friend%s", friends, (char *)({char *ret = ""; if(friends != 1) ret="s";ret;})
           );

    /*In english you say "friends" when you have 0 or more than one. However if you have 1 you say "friend" without the letter s. I have created a conditional code but there is an error in my print code*/

    return 0;
}
1

You cannot use if inside expression, but you can use ternary operator for that purposes.

#include <stdio.h>
#include <stdlib.h>

int main()
{

    int friends = 2;

    printf("I have %d friend%s", friends, friends != 1 ? "s" : "");

    /*In english you say "friends" when you have 0 or more than one. However if you have 1 you say "friend" without the letter s. I have created a conditional code but there is an error in my print code*/

    return 0;
}
kocica
  • 6,412
  • 2
  • 14
  • 35
  • Thanks for your help. I'm a beginner and trying to learn the programming language C on my own. What exactly considers something an expression? Is 'if' conditional statements an expression too? Is that why I can't put it inside printf statement? – sangmin park Aug 12 '17 at 17:03
  • I would recommend you to read some good C language book. Operators are used with operands to build expressions. For example the following is an expression containing two operands and one oprator 4 + 5. Becouse you cannot use statement in expression. Ternary operator is not statement so there is not problem with using it in expression. Hope you understand now @sangminpark. – kocica Aug 12 '17 at 17:25
1

Being that the previous answers suggest the use of the ternary operator, perhaps the following equivalent output with different syntax might help also.

#include <studio.h>
int main(){
     int friends = 2;
     if(friends > 1){
          printf("I have %d friend%s",friends,"s");
     }else{
          printf("I have %d friend",friends);
    }
    return 0;
}
Miket25
  • 1,895
  • 3
  • 15
  • 29