operator is not working

+2 votes
asked Oct 13 by Matt Santos (140 points)
I wonder why my sample code is not working

printf("\n");
printf("Enter your name: ");
scanf("%d",&name);
printf("Enter your gender: ");
scanf("%d%d", &m,&f);

if(m==1)
{
printf("Hello Mr. %d",name);
}
else if (f==1)
{
printf("Hello Ms. %d",name);
return 0;

sorry i'm a fresh student coder

3 Answers

0 votes
answered Oct 14 by Peter Minarik (9,500 points)

Compile your code

You have to look at the compilation errors and fix them.

Also, when you share you code, share the whole content, not just a fragment. Includes, variables, functions and other declarations, etc are also important.

Problems

Strings

When you want to read or print strings, you have to use the %s format. Also, store strings in char buffers (see name below). Only problem is that in a buffer you cannot store longer strings than the size of the buffer.

Decimals

Decimals (e.g. int) should be used with the %d format.

A possible solution

#include <stdio.h>

int main()
{
    char name[100];
    int gender;
    
    printf("Enter your name: ");
    scanf("%s", name);
    printf("\nEnter your gender (1 for male, 0 for female): ");
    scanf("%d", &gender);
    printf("\n");

    if (gender == 1)
    {
        printf("Hello Mr. %s\n", name);
    }
    else if (gender == 0)
    {
        printf("Hello Ms. %s\n", name);
    }
    else
    {
        printf("Unknown gender code: %d\n", gender);
    }

    return 0;
}
0 votes
answered Oct 16 by YOGESH (140 points)
#include<stdio.h>

#include<conio.h>

int main()

{

 string s;

int g;

printf("Enter your name");

scanf("%s",&s);

printf("Enter your Gender(1 for male \n0f or Female\n  3 for other)");

scanf("%d",&g);

if(g==1)

printf("Hello Mr");

elseif(g==0)

printf("Hello Ms");

else

printf("Other gender")

return 0;
0 votes
answered 6 days ago by Shivam Raina (200 points)
// answer in character type

#include<stdio.h>
#include<string.h>
int main()
{
    char name[20];
    char x;
    printf("enter your name: \n");
    scanf("%s",name);
    printf("enter your gender: \n");
    scanf("%s",&x);
    
    if(x=='m')
    {
        printf("hello Mr.%s",name);
    }
    else if(x=='f')
    {
        printf("hello Mrs.%s",name);
    }
    else
    {
        printf("hello %s",name);
    }
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...