does scanf recognizes space as character?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kalar
    New Member
    • Aug 2007
    • 82

    does scanf recognizes space as character?

    Hello i have a strange question.

    space is a character
    so if we have
    Code:
    char ch;
    scanf("%c", &ch); /*and i press space and enter*/
    so now we have in ch = ' '. Am i wrong?
    if NO then why this calculator program works if i put as example
    4+5= and
    4 + 5 =

    i mean the spaces when we type 4 + we have a space so the operator has the space as a character and not +. I feel that is something so easy but now i can't unerstand it.Thanks
    Code:
    #include <stdio.h>
    
    int main(void) {
      	int i1, i2, ok;
      	char operator, equals;
      	double res;
    
    	printf("give an expression like <number> <operator> <number> =\n");
    	do {
    		scanf("%d %c %d %c", &i1, &operator, &i2, &equals);	
    		ok = 1;							   
    
    		switch(operator) {	
          			case '+':	res = i1 + i2;
    					break;
    			case '-':	res = i1 - i2;
    					break;
    			case '*':	res = i1 * i2;
    					break;
    			case '/': 	
    					if (i2 != 0)
    						res = (double)i1 / i2;
    					else {
    						printf("the divider should not be 0\n");
    						ok = 0;
    					}
    					break;
    			default:	
    					printf("the operator is not valid\n");
    					ok = 0;
    		}
    
    		if (equals != '=') {	
    			printf("the expression should have =\n");
    			ok = 0;
    		}
    	} while (!ok);			
    
    	printf("%d %c %d = %f\n", i1, operator, i2, res);	
    }
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    I cannot stress how important it is to read the documentation. You can find documentation of scanf with Google. A useful Google term is “man scanf” or in general “man <some C function>” as that will bring up an online man page. If you are on a *NIX system, you can just do this from the console.

    Now let’s consider the line: scanf(“%d %c %d %c”,...)

    From the documentation, you should gather the following. %d takes in a number. %c takes in a character. However, %c does not skip whitespace. However, if %c should skip whitespace, then it should be preceded with whitespace. This information is written in plain English in the man pages.

    Now look at the format string in question. There is %d. Then there is a %c preceded with whitespace. Then %d. Then %c preceded with whitespace.

    So a number, then a character, then a number, then a character. Any whitespace before the character is ignored.

    Comment

    • kalar
      New Member
      • Aug 2007
      • 82

      #3
      Originally posted by oler1s
      I cannot stress how important it is to read the documentation.
      i don't want this

      Originally posted by oler1s
      Now look at the format string in question. There is %d. Then there is a %c preceded with whitespace. Then %d. Then %c preceded with whitespace.

      So a number, then a character, then a number, then a character. Any whitespace before the character is ignored.
      yes but if i press 4 + 9 = we have number-character(space is a character)-character (+)-character(space )-number(9)-character(space )-character(=)
      So why the program works?
      I don't know if you understand me, maybe i don't exlain it so well.

      Comment

      • oler1s
        Recognized Expert Contributor
        • Aug 2007
        • 671

        #4
        I understand what you're saying. I tried to explain what was happening in my previous explanation, but I don't think you understood it. I'll explain again:

        %d %c %d %c
        4 + 9 =

        On the top is the format string, on the bottom is the input. Let's walk through the input, and see how it matches up.

        4: matches with %d.
        <space>: gets skipped. Why? Because there is a space before %c. I explained in my previous post (paraphrasing the documentation), the significance of a space before %c.
        +: matches with %c.
        <space>: gets skipped. Why? Because there is a space following %c. A space ion the format string means any whitespace, including none, gets skipped.
        9: matches with the second %d.
        <space>: gets skipped. Again, there is a space before %c, which changes its behavior. It skips whitespace, and then takes a non whitespace character. So the space here gets skipped...
        =: and the '=' character matches with %c.

        Yes, %c will store a space, because space is a character. But from the documentation: "The usual skip of leading white space is suppressed. To skip white space first, use an explicit space in the format."

        If you think it doesn't make sense, show me how you think it should match up with the format string.

        Comment

        • kalar
          New Member
          • Aug 2007
          • 82

          #5
          So if i understand good when we want to ignore spaces with scanf we also put space in the expression of scanf?

          Comment

          • primeSo
            New Member
            • Aug 2007
            • 35

            #6
            Originally posted by kalar
            So if i understand good when we want to ignore spaces with scanf we also put space in the expression of scanf?
            yes, use space prior to your format string. ie. scanf(" %d", &num); will ignore any preceeding white space until it hit a non-white space input

            Comment

            • kalar
              New Member
              • Aug 2007
              • 82

              #7
              Thank you very much!!!

              Comment

              Working...