1

I am writing a c program to print specific statements if current day is Friday using if-else statement. What is the error involved?

I've tried using integer values for the same code and it works, but when i equate n as Friday the output shows else part only.

char s[10]="Friday";
char n[6];
printf("Enter a day:\n");
scanf("%s",n); //n is string
if(n==s)
    printf("Have a nice weekend!");
else
    printf("Have a nice day!");

I expect the output for "Friday" to be "Have a nice weekend!", but the output is "Have a nice day!" for any input.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Nivesh P
  • 29
  • 6
  • 4
    Your comparison is comparing two *pointers* that will never the equal. Any decent book or tutorial should have taught you about [`strcmp`](https://en.cppreference.com/w/c/string/byte/strcmp). – Some programmer dude Sep 12 '19 at 16:06
  • 1
    BTW, the array `n` is not big enough to hold the string `"Friday"`. – user3386109 Sep 12 '19 at 16:09
  • Also, beware of buffer overflow. `char n[6]` is too short for any unabbreviated English names of days of the week. – Ian Abbott Sep 12 '19 at 16:09
  • I think this is a dup, but I'm on mobile. Will find it for you when I can – AndyG Sep 12 '19 at 16:10
  • Also note that n only has room for five characters plus the terminating null character. "Friday" with its 6 letters will already overflow it, not to mention "Wednesday"! Reserve more space with e.g. `char n[11]` and then use `scanf("%10s", n)` or so. – TeaRex Sep 12 '19 at 16:15

2 Answers2

2

You should use a function like strcmp to compare char arrays in C.

if(strcmp(s, n) == 0) {
    printf("Have a nice weekend!");
}

When you use the == operator you are comparing addresses (or sometimes pointers), not string literal values.

Also as pointed out above (pun intended) an array in C needs space for the null terminating character.

Woodstock
  • 22,184
  • 15
  • 80
  • 118
  • I am new to C language and not well versed in strcmp function. I used switch statement for the problem and it was working fine but had problem with if-else statement. Much thanks. – Nivesh P Sep 12 '19 at 16:35
  • @NiveshP good work, you are learning. strcmp is a basic C function widely used to compare strings. It's perfectly good to use as needed. – Woodstock Sep 13 '19 at 09:12
2

For starters you should enlarge the array becuase the string literal "Friday" can not fit into the array.

For example

char n[10];

Secondly instead of scanf use the standard function fgets because scanf such as it is written in your program is unsafe.

For example

fgets( n, sizeof( n ), stdin );

You can remove the trailing new line character the following way

n[ strcspn( n, "\n" ) ] = '\0';

To do that you have to include the header <string.h>.

In this statement

if(n==s)

there are compared addresses of first characters of the strings. You need to use standard string function strcmp to compare the strings themselves instead of the pointers. For example

if ( strcmp( n, s ) == 0 )

Here is a demonstrative program

#include <stdio.h>
#include <string.h>

int main(void) 
{
    const char *s = "Friday";
    char day[10];

    printf( "Enter a day: " );

    fgets( day, sizeof( day ), stdin );

    day[ strcspn( day, "\n" ) ] = '\0';

    if ( strcmp( day, s ) == 0 )
    {
        printf( "Have a nice weekend!" );
    }       
    else
    {
        printf( "Have a nice day!" );   
    }

    return 0;
}

Its output might look like

Enter a day: Friday
Have a nice weekend!
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Why should i use fgets instead of scanf? Why is scanf unsafe? Because fgets can read from open file while scanf reads only that of standard file? – Nivesh P Sep 12 '19 at 16:53
  • 1
    @NiveshP Because the user can enter more characters then the size of the array used in scanf. fgets allows to enter exactly the number of characters that is specified by the second argument. And you indeed entered more characters (including the terminating zero) when the array had size equal to 6. This invokes undefined behavior of the program. – Vlad from Moscow Sep 12 '19 at 16:54
  • So fgets gets the exact number of characters to be inputted. Much thanks mate. – Nivesh P Sep 12 '19 at 17:00