473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

does scanf recognizes space as character?

82
Hello i have a strange question.

space is a character
so if we have
Expand|Select|Wrap|Line Numbers
  1. char ch;
  2. 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
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4.       int i1, i2, ok;
  5.       char operator, equals;
  6.       double res;
  7.  
  8.     printf("give an expression like <number> <operator> <number> =\n");
  9.     do {
  10.         scanf("%d %c %d %c", &i1, &operator, &i2, &equals);    
  11.         ok = 1;                               
  12.  
  13.         switch(operator) {    
  14.                   case '+':    res = i1 + i2;
  15.                     break;
  16.             case '-':    res = i1 - i2;
  17.                     break;
  18.             case '*':    res = i1 * i2;
  19.                     break;
  20.             case '/':     
  21.                     if (i2 != 0)
  22.                         res = (double)i1 / i2;
  23.                     else {
  24.                         printf("the divider should not be 0\n");
  25.                         ok = 0;
  26.                     }
  27.                     break;
  28.             default:    
  29.                     printf("the operator is not valid\n");
  30.                     ok = 0;
  31.         }
  32.  
  33.         if (equals != '=') {    
  34.             printf("the expression should have =\n");
  35.             ok = 0;
  36.         }
  37.     } while (!ok);            
  38.  
  39.     printf("%d %c %d = %f\n", i1, operator, i2, res);    
  40. }
Dec 13 '07 #1
6 5439
oler1s
671 Expert 512MB
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.
Dec 13 '07 #2
kalar
82
I cannot stress how important it is to read the documentation.
i don't want this

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.
Dec 13 '07 #3
oler1s
671 Expert 512MB
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.
Dec 14 '07 #4
kalar
82
So if i understand good when we want to ignore spaces with scanf we also put space in the expression of scanf?
Dec 14 '07 #5
primeSo
35
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
Dec 14 '07 #6
kalar
82
Thank you very much!!!
Dec 14 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Jonathan Ng | last post by:
Hi, I was wondering if there was a way to include the white spaces in a string. Currently, I am using: scanf("%s", &input); However, this doesn't include the 'space' character or any other...
7
by: Steve Kobes | last post by:
I ran the following program on gcc: #include <stdio.h> int main(void) { unsigned int i; char c; if (scanf("%x", &i) == 1)
2
by: Stu | last post by:
I have the following "C" program, which works fine. #include <stdio.h> #include <stdlib.h> #include <strings.h> int main() { char *buffer = "1234 - 5678";
5
by: sreelal | last post by:
Dear all, plz see the following prograam. main() { int a,b; scanf("%d %d ",&a,&b);
33
by: Lalatendu Das | last post by:
Dear friends, I am getting a problem in the code while interacting with a nested Do-while loop It is skipping a scanf () function which it should not. I have written the whole code below. Please...
30
by: James Daughtry | last post by:
char array; scanf("%19s", &array); I know this is wrong because it's a type mismatch, where scanf expects a pointer to char and gets a pointer to an array of 20 char. I know that question 6.12...
14
by: iwinux | last post by:
Hi. Before I use scanf(), I must malloc the memory for it, like this: //Start char * buffer; buffer = malloc(20); scanf("%s", &buffer); //End
4
by: sulekhasweety | last post by:
scanf(" \"%\"",s); means The scanf will expect a string enclosed within double quotes for correctly reading the input. Within the double quotes it will accept any input other than ". It will...
3
by: Tinku | last post by:
#include<stdio.h> main() { char line; scanf("%", line); printf("%s", line); } it will read and print the line but what is "%" in general we gives %s, %c .
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.