hi guys,
here in this code, it's working if I execute scanf functions one by one (independently, as I take others in comment line). However, I tried different variations but it's not working and regardless of how I search, I couldn't have figured out why it doesn't work on purpose. can you help, please?
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20
int main(){
char a[SIZE]={0};
char b[SIZE]={0};
char c[SIZE]={0};
printf("Enter the string suzy as you see: ");
scanf("%s\"%[^\"]\'%[^\']",a,b,c);
/* printf("Enter the string suzy as you see: ");
scanf("%s",a);
printf("Enter the string \"suzy\" as you see: ");
scanf("\"%[^\"]",b);
printf("Enter the string \'suzy\' as you see: ");
scanf("\'%[^\']",c);
*/
printf("\n\na The string suzy is %s",a);
printf("\n\nb The string \"suzy\" is %s",b);
printf("\n\nc The string \'suzy\' is %s\n\n\n\n\n",c);
return 0;
}
Enter the string suzy as you see: suzy "suzy" 'suzy'
a The string suzy is suzy
b The string "suzy" is
c The string 'suzy' is
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20
int main(){
char a[SIZE]={0};
char b[SIZE]={0};
char c[SIZE]={0};
// printf("Enter the string suzy as you see: ");
// scanf("%s\"%[^\"]\'%[^\']",a,b,c);
printf("Enter the string suzy as you see: ");
scanf("%s",a);
printf("Enter the string \"suzy\" as you see: ");
scanf("\"%[^\"]",b);
printf("Enter the string \'suzy\' as you see: ");
scanf("\'%[^\']",c);
printf("\n\na The string suzy is %s",a);
printf("\n\nb The string \"suzy\" is %s",b);
printf("\n\nc The string \'suzy\' is %s\n\n\n\n\n",c);
return 0;
}
Enter the string suzy as you see: suzy
Enter the string "suzy" as you see: Enter the string 'suzy' as you see:
a The string suzy is suzy
b The string "suzy" is
c The string 'suzy' is
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20
int main()
{
char a[SIZE]={0};
char b[SIZE]={0};
char c[SIZE]={0};
printf("Enter the string suzy as you see: ");
scanf(" %s",a);
printf("Enter the string \"suzy\" as you see: ");
scanf(" \"%[^\"]\"",b);
printf("Enter the string \'suzy\' as you see: ");
scanf(" \'%[^\']\'",c);
printf("\n\na The string suzy is %s",a);
printf("\n\nb The string \"suzy\" is %s",b);
printf("\n\nc The string \'suzy\' is %s\n\n\n\n\n",c);
return 0;
}
I made two changes to the format strings:
1) Added space in front which will make scanf ignore any leading whitespace before trying to read the rest.
2) Added missing quotes (\" and \') at the end.
in origin of your code I made this change and it worked but still I am not sure why that happens.
first scanf scanf("%s",a);
second scanf scanf(" \"%[^\"]",b);
third scanf scanf("\" \'%[^\']",c);
The extra \" is necessary to consume the ending double quote character from the user input.
1 2
"suzy"
^
If you don't do that it will be left in the input buffer and cause the next scanf to fail because it expects a single quote ' but finds a double quote "