Hello i have a strange question.
space is a character
so if we have -
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 - #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);
-
}
6 5439
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.
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.
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.
So if i understand good when we want to ignore spaces with scanf we also put space in the expression of scanf?
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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)
|
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";
|
by: sreelal |
last post by:
Dear all,
plz see the following prograam.
main()
{
int a,b;
scanf("%d %d ",&a,&b);
|
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...
|
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...
|
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
|
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...
|
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 .
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
| |