1
# include <stdio.h>


void danidev(void){
 printf("Dani is a YouTuber and an indie game developer and an fps game developer having his game published in play store he is 22 years old and goes to a university");
}

void brackeys(void){
    printf("brackeys is a YouTuber and an indie game developer and also an fps game developer having most of his games published in itch.io and has a team which works on game development");
}

void blackthornprod(void){
    printf("  they are two brothers who create video games and teach others how to do the same over on Youtube and Udemy ! they are passionate in sharing their knowledge and game creation journey with other aspiring game developers.");
}

void jabrils(void){
 printf("jabrils is a ai programmer and also a machine learning pro coder and also a game developer he has made a lot of ai and has saved millions of people from their tough times");
}

void codingbullet(void){
 printf("coding bullet is a multi intelligent ai developer and also a master in machine learning also he owns a youtube channel with 2.06 million subscribers");
}

int main(){

 printf("HERE IS THE INFORMATION OF FAMOUS CODING YOUTUBERS(PLS TYPE THE FOLLWOING YOUTUBERS NAME): ");
 char b;
 scanf("%c",&b);
 if(b=='danidev'){
 danidev(); 
 }
 else if(b=='brackeys'){
 brackeys();
 }
 else if(b=='blackthornprod'){
 blackthornprod();
 }
 else if(b=='jabrils'){
 jabrils();
 }
 else if(b=='codingbullet'){
 codingbullet();
 }
 else{
 printf(" i dont know what you are taking about");
 }

return 0;
}

I have a problem when I enter a YouTubers name ( complete name) as an input I get a problem that the constant is too long and does not give the proper result and also it says the constant character is too long for its type

4
  • 1
    use double quotes "" to create string literals, not single-quotes. Create a char array to store the results of your scanf, and use %s to read a string rather than %c to read a single character. Commented May 19, 2020 at 16:07
  • thanks, Christian Gibbons for your help Commented May 19, 2020 at 16:11
  • 1
    I suggest that you work your way through a beginning C programming course. There are many free ones online. Commented May 19, 2020 at 16:18
  • Hi bob Jarvis, I newly started learning c and I am a 12-year-old kid(how is smart) I learn c by myself in educative.io @Bob Jarvis - Reinstate Monica Commented May 19, 2020 at 16:33

3 Answers 3

1

You need to allocate more space for the input. The type char is only going to hold one character. You need an array of chars. This can be declared as follows:

char b[30];

This will hold 30 characters.

For string literals, you should use double quotes not single quotes. e.g. "danidev" as opposed to 'danidev'.

When comparing strings (arrays of characters) you should use the strcmp() function.

Please read the documentation for it: strcmp

Since you are starting out, I would also recommend looking at some tutorials for strings in C. String Functions

Sign up to request clarification or add additional context in comments.

Comments

0

There a couple of errors, from string comparison, string definition and char/char array variables. It looks like JS sometimes, but take care of the big differences. Beside that, I have seen another answer has been added while I was writing this one, I want to add to pay attention at the length of array in scanf so you don't raise a buffer overflow error at runtime.

You can check it out here: https://onlinegdb.com/HyrgvK-sL

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

void danidev (void)
{
  printf ("Dani is a YouTuber and an indie game developer and an fps game developer having his game published in play store he is 22 years old and goes to a university");
}

int main ()
{

    printf("HERE IS THE INFORMATION OF FAMOUS CODING YOUTUBERS(PLS TYPE THE FOLLWOING YOUTUBERS NAME): ");
    char b[32];
    scanf("%31s", b);

    if (strncmp(b,"danidev", 32)== 0) 
    {
        danidev ();
    }
    else
    {
        printf (" i dont know what you are taking about");
    }
    return 0;
}

2 Comments

hi could you please explain about if (strncmp(b,"danidev", 32)== 0) what it does and how please
Sure, strncmp does a string compare. In C a string is an array of characters terminated by a null character. The extra n is for the maximum length of the strings, so you never go in a forbidden location. In this particular case, you can use also strcmp because the second string is smaller then 32 and it will terminate first, but I think it's a good habit to use strncmp.
0

You cannot directly compare strings using the equality operator (==). Instead, use strcmp() to compare two strings.

char str[20];
gets(str);

if(!strcmp(str, "danidev") danidev();  //Use double quotes for strings, instead of single quotes (used for character literal).
//Same as strcmp(str, "danidev") == 0
//rest of the code

Also, if you're a beginner, I suggest you to check some C's String library functions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.