Search code examples
cprintfscanfc-strings

i need help because of these constant nulls in C


this is my code and I have been getting constant nulls from it so I do need a help

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

int main () {       
    char jojo[100];
    
    printf("name: ");
    scanf("[^\n]*c", &jojo);
    printf("Happy Birthday to %s.\n");
    
    printf("\n");       
    return 0;       
}

Solution

  • For starters in the call of scanf

    scanf("[^\n]*c", &jojo);
    

    you shall not use a pointer to the character array. Also you need to use the symbol '%' before the conversion specifiers.

    Instead write

    scanf("%[^\n]%*c", jojo);
    

    In the call of printf you forgot to specify the argument that represents the array.

    printf("Happy Birthday to %s.\n");
    

    write

    printf("Happy Birthday to %s.\n", jojo);
    

    Here is a demonstrative program.

    #include <stdio.h>
    
    int main(void) 
    {
        char jojo[100];
        
        printf( "name: " );
        scanf("%99[^\n]%*c", jojo );
    
        printf( "Happy Birthday to %s.\n", jojo );
        
        return 0;
    }
    

    The program output might look like

    name: programmer Vaustin
    Happy Birthday to programmer Vaustin