-1

I am new in C programming. Can you please advise, what's wrong with my code? It looks like the if statement is not working, and instead it's jumping and printing the else statement.

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

int main()
{
    char profession;

    printf("what is your profession? \n");
    scanf(" %s", profession);

    if(profession==“QA”)
    {
        printf(“Go and Test\n“);
    }
    else
    {
        printf("Do whatever you want");
    }

    return 0;
}
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Sus
  • 1
  • 2

2 Answers2

0

First of all, you can't compare strings like that in C. use strcmp or strncmp instead.

Secondly, in your code, profession is a char, and you want to put a string (several chars) in it. It won't work. You can create a char * (pointer on a char) (without forgetting to malloc() it) or a char [] (a char array).

Sylvain GIROD
  • 836
  • 1
  • 11
  • 23
0

Firstly, strings in C are array of characters, so you have to declare profession as a pointer, pointing to an array of characters. So that statement will look like this char* profession, secondly, you have to use a method called strcmp(char* a, char* b) that accepts two char pointers. This will return 0 if they are equal. I will include the answer, but I assume there are better ways of writing this code.

int main() {

    char* profession;
    char* compare = "QA";
    printf("What is your profession?\n");

    scanf(" %s", profession);

    if(strcmp(profession, compare) == 0) {
            printf("Go and Test\n");
    } else {
            printf("Do whatever you want");
    }

    return 0;
}
arne.z
  • 3,242
  • 3
  • 24
  • 46