-7
#include<stdio.h>

void main(){
    
     char marital[5];
     printf("enter your marital details-");
     scanf("%s",&marital);

     if(marital=="male"){        
        printf("you are insured");
     }
}
Papai from BEKOAIL
  • 1,469
  • 1
  • 11
  • 17

3 Answers3

0

You should use strcmp()to compare char arrays (strings) in C Programming Language. Also remember to add the string ending char '\0'.

#include <unistd.h>
#include <string.h>

#define INSURED "You are insured\n"
int main () {
        char marital[5];

        int n = read(0, marital, 5);
        marital[n - 1] = '\0';

        if ( strcmp(marital, "male") == 0 ) {
                write(1, INSURED, strlen(INSURED));
        }

        return 0;
 }

strcmp() returns an integer

  • 0 if strings are equal

  • > 0 if the first non-matching character in str1 is greater (in ASCII) than that of str2.

  • < 0 if the first non-matching character in str1 is lower (in ASCII) than that of str2.

Jiahui Chen
  • 75
  • 3
  • 10
  • 2
    As the compiler already adds the `\0` to any string literal, how would it be possible to forget it? – Gerhardh Sep 26 '21 at 11:39
  • @ImtiasBakar Use my updated code, it uses File Descriptors and Systems Calls and works fine. I've already tested it. I don't know why my answer got downvoted... – Jiahui Chen Sep 26 '21 at 12:01
  • Why are you using `read` when it doesn't really add anything useful to the question though? It's not portable and there are better ways of taking strings as input specifically. `write` also seems really unnecesary... – mediocrevegetable1 Sep 26 '21 at 12:25
  • It is portable across POSIX systems – Jiahui Chen Sep 26 '21 at 12:36
  • @JiahuiChen sorry for downvoted it was a mistake done by me thankyou for explaining problem and everything – Imtias Bakar Sep 26 '21 at 13:28
  • @stark does scanf puts \0 at the end? Please clarify – user786 Sep 26 '21 at 13:58
  • @stark scanf doesn't put a '\0' at the end. That function reads input from the standard input stream `stdin`. Actually, what you get is the input + '\n' because of the Enter key. That's why I'm changing the last '\n' for '\0' – Jiahui Chen Sep 26 '21 at 14:56
0
char gender[7];

scanf("%s",&gender);
char *message=(!strcmp(gender,"male"))?"gender is male":"gender is female";
printf("%s\n",message);
user786
  • 3,902
  • 4
  • 40
  • 72
0

There are a couple of issues in your code:

  • main() should not return void. Its proper return value in an int.
  • You don't need to pass the address of marital to scanf() since strings in C are just pointers to arrays of char.
  • scanf() is a dangerous function. You should not use it to read user input. Use fgets() function, it reads an entire line and is much safer.
  • if(marital=="male") you are basically comparing two pointers, not the actual strings. You have to use strcmp().

That said, here's how your code should be:

#include <stdio.h>
#include <string.h> // for strcmp()

int main()
{
    char marital[5];
    printf("Enter your marital details: ");
    fgets(marital, sizeof marital, stdin);
    marital[strcspn(marital, "\n")] = `\0`; // fgets reads \n too so remove it

    if(strcmp(marital, "male") == 0)
        printf("you are insured");
}
Zakk
  • 1,935
  • 1
  • 6
  • 17