So once my beginner ass got used to printf and variables in C, i started to use the scanf function, here's the code (shamelessly stolen from fresh2refresh.com)
#include <stdio.h>
int main()
{
char ch;
char str[100];
printf("Enter any character \n");
scanf("%c", &ch);
printf("Entered character is %c \n", ch);
printf("Enter any string ( upto 100 character ) \n");
scanf("%s", &str);
printf("Entered string is %s \n", str);
}
Except that it doesn't run, neither on the vscode output or cmd. it doesn't even print the "Enter any character" from line 5, it show that the code is running, but doesn't even accept inputs (and will only halt once i click the Stop Code Run button)
any tips?
CodePudding user response:
format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[100]’ [-Wformat=] scanf("%s", &str);
Strings are represented as Array of characters thus array name is an identifier(implicit conversion to pointer) is the address of first element thus no need to specified '&' in case of reading strings.
Its clear from warning that it can't convert from char (*)[100]
to char *
CodePudding user response:
The line
scanf("%s", &str);
Is incorrect - it should be
scanf("