-5

I was practising some programming and came up with this code. But I am getting this error message : multi-character character constant [-Werror,-Wmultichar] from the 8th line

#include <cs50.h>
#include <stdio.h>

int main (void)
{
     int n = get_int("what is your age: ");

     if( n <= '25' || n >= '0')
           {
               printf("you are young\n");
           }
     else
           {
               printf("you are old\n");
           }
 }

-CONCLUSION-

In the 8th line , the single quote on 25 and 0 was not needed because 25 and 0 are int and not char. And || should be replaced with && because n should be in between 0 and 25.

rasberry
  • 13
  • 5
  • 4
    `if(n <= 25 && n >= 0)`. Wrong operator (should be && instead of ||) and wrong literal types (should be int instead of char) – kaylum Oct 27 '21 at 07:04
  • Every integer is going to be less than or equal to 25 OR greater than or equal to zero. So you need to change the OR operator (`||`) with AND operator (`&&`). – Harun Yilmaz Oct 27 '21 at 07:07
  • oh thanks ! but im still getting errors : multi - character constant [- werror , wmultic har] what is this? – rasberry Oct 27 '21 at 07:08
  • 1
    The single quotes around the numbers are an issue too. They mean to treat the value as a character literal. `'25'` is wrong because it is more than one digit, and `'0'` is interpreted as 48, the ASCII value of 0. https://en.cppreference.com/w/cpp/language/ascii – Retired Ninja Oct 27 '21 at 07:10
  • 2
    You need to study the the first chapters of any C book. Also avoid CS50, it has a very poor reputation since it teaches harmful practices. – Lundin Oct 27 '21 at 07:10
  • 1
    @rasberry Looks like you haven't changed `'25'` to `25`. That is, remove the quotes. Same for the zero. – kaylum Oct 27 '21 at 07:12

1 Answers1

2

' ' is a literal char value. Because '25' is not a char, it throws an error. Looking at your code, maybe you don't need a literal char value, it looks like you need a literal int value. So, get rid of the single quotes brackets.

Also, n <= 25 or n >= 0 is true for every n. If you want the condition to check if n is in a range of 0 and 25, change || (logical OR) to && (logical AND)

So, in conclusion, this is the right code:

#include <cs50.h>
#include <stdio.h>

int main ()
{
     int n = get_int("what is you age: ");

     if( n <= 25 && n >= 0)
     {
         printf("you are young\n");
     }
     else
     {
         printf("you are old\n");
     }
 }
  • 2
    Nitpick: `' '` is actually an integer character constant and its type is `int`. When it contains more than one character - like `'25'` - the integer value is implementation defined. So it should not result in an error but e.g. gcc will generate a warning (which will result in an error if -Werror is used as compiler flag). See https://stackoverflow.com/questions/40609590/are-multi-character-character-constants-valid-in-c-maybe-in-ms-vc – Support Ukraine Oct 27 '21 at 07:27
  • `'25'` would more likely be 12853 if allowed, but shouldn't ever be 25. – Retired Ninja Oct 27 '21 at 09:07