3

I tried to program in C but was not sucessful. I have a simple source code and I need to match multiple letters in if (char). It's displaying the following error message (in linux terminal using gcc):

main.c: In function `main': main.c:16:23: warning: character constant too long for its type [enabled by default]

if (firstName[20] == 'Vojta'){

Source code:

#include <stdio.h>

int main(int argc, char const *argv[])
{
    /* code */
    char firstName[20];
    char lastName[40];
    char password[20];
    char confpasswd[20];
    int age;

    printf("Please write your first and last name:");
    scanf("%s%s", firstName, lastName);
    printf("%s %s:\n", firstName, lastName);

    if (firstName[20] == 'Vojta'){
        printf("\ncool\n");
    }

    return 0;
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 5
    `'Vojta'` is a multi-character string constant; it's of type `int` with an implementation-defined value. This is an obscure feature of the C language. I've seen it misused far more often than I've seen it used correctly. – Keith Thompson Apr 24 '15 at 15:47
  • And `firstName[20]` does not refer to the entire 20-element array; it refers to element 20 of the array, an object of type `char`. Since the array only has elements 0 through 19, `firstName[20]` doesn't even exist. – Keith Thompson Jul 24 '15 at 18:10

2 Answers2

10

Point 1

Use strcmp() to compare strings.

Point 2

Change

scanf("%s%s", firstName, lastName);

to

scanf("%19s %39s", firstName, lastName);

and do check the return value of scanf() to ensure sucess. However, it's better if you use two scanf() seperately to take two inputs, that will be less error prone.

Also, as a suggestion , you can read about fgets() and strtok() for a how-to read a whole line as input and tokenize the required part.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
2

Regarding this line:

if (firstName[20] == 'Vojta'){

firstName is an array and its value was set via the scanf(). Therefore, first name will contain a terminating NULL char ('\0'). Therefore, the contents of firstName, if the user entered 'Vojta' would be 'V', 'o', 'j', 't', 'a', '\0' followed by 14 garbage characters.

Because of the trailing '\0' (which was inserted by the scanf()) the contents of firstName is a valid string.

The best way to compare the contents of firstName with some string literal, is to compare it to a string literal, using the function strcmp().

Note: the current code is comparing the (offset) 20 position in firstName (which is outside the bounds of the array and therefore results in undefined behaviour).

Here is an example of the right way to make a string comparison:

if( 0 == strcmp( firstName, "Vojta" ) )

Do notice the double quote marks (") around the string literal.

Those double quote marks make it a string literal rather than just a bunch of characters that the compiler will evaluate to an 'int' value.

Note: individual characters can be represented by using single quote marks around each character as in: 'V', 'o', 'j', 't', 'a'.

The strcmp() function

  1. returns < 0 if the first parameter (contents of firstName) comes before (alphabetically) the second parameter.
  2. returns > 0 if the first parameter (contents of firstName) comes after (alphabetically) the second parameter.
  3. returns 0 if the first parameter matches the second parameter.
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
user3629249
  • 16,402
  • 1
  • 16
  • 17