Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<stdio.h>
    main()
    {
    	char address[20];
        printf("Please enter your full residential address :\n");
    	scanf("%[^$]",&address);
        printf("Your address is : %s\n\n",address);
    	getchar();
    }


CASE-I : When i am finished giving the input, then no matter how many times i press enter button on keyboard, the screen never displays the output, instead the cursor just keeps on going to the next line,i.e., it keeps taking the newline character(\n) as an input.
CASE-II : When i insert(input) a $ character after my input(for which i need output), then as i press 'ENTER' output appears on the screen.

What i understand ?
The result of CASE-II is pretty clear to me as in this case when the scanf encounters the '$' character it terminates reading the input characters, and on pressing ENTER it generates the output.

What i don't understand ?
CASE-II is not clear to me.I can't understand what is happening in this case.

What I have tried:

I can't find any reason behind this in my standard textbooks.
Posted
Updated 14-Apr-16 5:12am
v3

1 solution

I believe this

Quote:
[^$]


is telling scanf to stop when you type the $ character.. if you did this

C++
[^\n]


it would stop when you use the return key (ie, \n is NOT in the valid character class, so input stops)

All this being said, scanf is usually used to get formatted input, using it to read unformatted input into a buffer as you are doing, and not a large buffer at that, can cause also sorts of nasty things to happen

edit - you could try and make it a bit safe by using

scanf("%19[^\n]")


given your buffer size of 20
 
Share this answer
 
v2
Comments
Shubham_Dubey 14-Apr-16 8:27am    
In data-type declaration i have defined that "address" can hold ONLY 20 characters.
Also, the program produces the same results even if i specify the field-width of %s in the format specification of printf statement.
So, your explanation is incorrect.
Jochen Arndt 14-Apr-16 9:26am    
Garth's answer is correct. The behaviour is as intended.

The scanf() call will return when a match has occurred and the enter key is pressed afterwards. All characters at and behind the match are ignored.

So with [^$] you have to enter the $ character and CR afterwards:
"test$" + CR -> "test"
"test$23" + CR -> "test"
"test" + CR -> scanf() still waits for '$' but CR is echoed back.
"test" + CR + "test1$" -> "test" + CR + "test1"
Shubham_Dubey 14-Apr-16 9:31am    
Ok..When i use scanf("%19[^$]") as you suggested, my program works fine.
Can you explain in detail, why ??
P.S. :-I am a novice in C programming.I started learning a month ago, so don't know much.
Also, about "buffer" i know that buffer is a temporary storage where computer keeps our input before it goes to the output.
I am not able to understand the use of buffer size here..
Please elaborate.
Thanks
Jochen Arndt 14-Apr-16 9:14am    
Countered the downvote because the answer is correct.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900