While studying the scanf
function with a little more depth, I had a question about the arguments I put before% when reading a string, that is scanf("argumentos...%s",minhastring)
, in the following codes I add a space and the ' J 'before the% to verify the behavior of scanf, that is the cleaning of the buffer is not what is under study, because this is not a correct way to do such a treatment, but rather the utility of putting characters before the%:
First version of the code:
//abrindo um arquivo para escrever o resultado do que for digitado
FILE *arq = fopen("testes.txt","w");
char str1[20], str2[20];
//lendo duas strings, o %19 limita o numero de caracteres a ser armazenado, evitando o
//overflow e o [^\n] faz com que o conteudo seja lido até encontrar uma quebra de linha,
// tornando possivel a leitura de strings com espaço
printf("Digite a primeira string: ");
scanf("%19[^\n]s", str1);
printf("Digite a segunda string: ");
scanf("%19[^\n]s", str2);
//armazenando a string formatada no arquivo: testes.txt
fprintf(arq, "Foi digitado \"%s\" e \"%s\" ", str1, str2);
//fechando o arquivo
fclose(arq);
Here are some tests and their outputs in a file:
1st
Type the first string: Antonio
Type the second string:
"Antonio" and "NÊ8~þÿÿÿb8v¼ [= v @"
I then changed the code and put a space before the "%" of the second scanf and also added the character 'J'.
Before the change: scanf("%19[^\n]s", str2);
Now scanf(" J%19[^\n]s", str2)
I could not type the second string, after typing the first one the program was terminated, in the next example the space is probably 'catching' the '\ n', and the 'J' character is forcing the second string to start with the letter 'J':
2nd Version of the code:
FILE *arq = fopen("testes.txt","w");
char str1[20], str2[20];
printf("Digite a primeira string: ");
scanf("%19[^\n]s", str1);
printf("Digite a segunda string: ");
scanf(" J%19[^\n]s", str2);
fprintf(arq, "Foi digitado \"%s\" e \"%s\" ", str1, str2);
fclose(arq);
Here are some tests and their outputs in a file:
1st
Type the first string: Maria Eduarda
Enter the second string: Joao Carlos
"Maria Eduarda" and "Oao Carlos" were spelled
2nd
Type the first string: Maria Eduarda
Type the second string: Maria Joaquina
"Maria Eduarda" and "> Þþÿÿÿb8v¼ [= v @"
Interesting Findings: The scanf
space is solving the problem of what was in stdin
, the '\ n' that "left" of the first data entry was ignored. By putting the letter 'J', or any other, before the '%' sign, I can only correctly read a string if it starts with the letter entered as a parameter, and the entered letter is ignored by scanf
, which does not stores it in the variable, as in the first test:
Type the first string: Maria Eduarda
Enter the second string: Joao Carlos
"Maria Eduarda" and "Oao Carlos" were spelled
-> This doubt arose when doing some tests and "playing around" a bit in the code blocks. I find C a very interesting language. And I would like to get this question: What happens when I put arguments between the quotation marks and the '%' in the scanf? And when they are generally used, if they are used?