Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compile printing a string function give error message - C

Tags:

c

string

printf

i want to write a simple main function in C that receive two line of string input and prints them on the screen. this is the code i wrote:

int main()
{
    char a[100];
    char b[100];
    printf("Enter the first string:\n");
    fgets(a,100,stdin);
    printf("Enter the second string:\n");
    fgets(b,100,stdin);
    printf("\n\n THE FIRST STRING IS:  %S\n\n THE SECOND STRING IS:%S",a, b);
    return 0;
}

and when i try to compile, i get this error message:

gcc -g -Wall PN52.c -o myprog
PN52.c: In function ‘main’:
PN52.c:12:2: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 2 has type ‘char *’ [-Wformat]
PN52.c:12:2: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 3 has type ‘char *’ [-Wformat]

Thanks for helping

like image 550
Yuval Avatar asked Dec 20 '22 12:12

Yuval


1 Answers

You use %S format, whereas the format for strings (char*) is %s.

printf("%s - %s\n", a, b);
like image 144
md5 Avatar answered Dec 24 '22 00:12

md5