4
When studying the function scanf
with a little more depth I arose a doubt about the arguments I put before the % when reading a string, ie scanf("argumentos...%s",minhastring)
, in the following codes I add a space and the character 'J' before the % to check the behavior of the scanf, ie 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 %:
1st 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:
first
Type the first string: Antonio
Type the second string:
It was typed "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 the program was terminated, in the next example the space is probably "catching" the ' n', and the character 'J' 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 respective outputs in a file:
first
Type the first string: Maria Eduarda
Type the second string: Joao Carlos
It was typed "Maria Eduarda" and "oao Carlos"
2nd
Type the first string: Maria Eduarda
Type the second string: Maria Joaquina
It was typed "Maria Eduarda" and " Þþÿÿÿb8v¼[=v@"
Interesting Findings: The Space of scanf
is solving the problem of what was left in stdin
, the "n' that is left" of the first data entry was ignored. By placing the letter 'J', or any other, before the '%' sign I can only correctly read a string if it starts with the given letter as parameter, and the informed letter is ignored by scanf
, which does not store it in the variable, as in the first test:
Type the first string: Maria Eduarda
Type the second string: Joao Carlos
It was typed "Maria Eduarda" and "oao Carlos"
->This doubt arose when doing some tests and "playing" a little in code Blocks. I find C a very interesting language. And I would like to remove this doubt: What happens when I put arguments between quotation marks and '%' in the scanf? And when they’re usually used, if they’re used?
I could even answer, but I don’t know if that’s what you want. First: working is not being right. Second:
scanf()
it is problematic, to do some exercises, ok, for general use, better to use something else: http://answall.com/a/111703/101, http://answall.com/q/42981/101– Maniero
@Bigown It was my first post here, I made a mistake by not specifying my doubt better. The question of buffer and stdin was not the focus of my question, but parameters that appear before the % of the scanf. I do not intend to use it in this way, only to know if I can and if so, if it has been implemented to be used in this way or if it will only show undefined behavior. For example when typing "love", if my scanf looks like this: scanf("a%s", str). Why is the letter 'a' ignored? When typing a word that does not start with 'a' can’t store the value in the variable? Thank you.
– Luis Henrique
I think you’re accessing inappropriate places of memory that explains these strange characters
– user45474