473,387 Members | 3,801 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,387 software developers and data experts.

why getchar is not executed??

yky
in this programme,if i input a number 100,the program will be over ,and i
donot have the chance to input a char to z;
# include<stdio.h>
main()
{
int x;
char z;
do
{printf("please input a number between 1 and 100\n");
scanf("%d",&x);
if(x==58)
{printf("exellent\n");
exit();}
else if(x-58>-5&&x-58<0)
printf("it is a little smaller\n");
else if(x-58<5&&x-58>0)
printf("it is a little bigger\n");
else if(x-58<-5||x-58>5)
printf("it is much away\n");
}while(x<100&&x>1);
puts("press any key");
z=getchar();
putchar(z);
}

Sep 21 '06 #1
1 1510
"yky" <ya************@yahoo.com.cnwrites:
in this programme,if i input a number 100,the program will be over ,and i
donot have the chance to input a char to z;
# include<stdio.h>
main()
{
int x;
char z;
do
{printf("please input a number between 1 and 100\n");
scanf("%d",&x);
if(x==58)
{printf("exellent\n");
exit();}
else if(x-58>-5&&x-58<0)
printf("it is a little smaller\n");
else if(x-58<5&&x-58>0)
printf("it is a little bigger\n");
else if(x-58<-5||x-58>5)
printf("it is much away\n");
}while(x<100&&x>1);
puts("press any key");
z=getchar();
putchar(z);
}
The getchar() was executed. If you typed "42" followed by <returnat
the prompt, then scanf() call read "42" and getchar() read the
newline.

The comp.lang.c FAQ is at <http://www.c-faq.com/>. I call your
attention to questions 12.18a, 12.18b, and 12.20. For that matter,
you should probably read all of section 12, then the entire FAQ as you
find the time.

Some other coments:

"main()" is more or less acceptable, but "int main(void)" is
preferred.

Your code would be a lot easier to read if you added some white space.
For example, rather than
}while(x<100&&x>1);
you could write:
} while (x < 100 && x 1);

Proper indentation would also be helpful.

getchar() returns an int, but you assign its result to a char. That
probably doesn't matter much here, since you're about to terminate the
program, but in general you need to store the result in an int so you
can check for EOF.

You call exit() with no arguments. exit() takes a single argument of
type int; this is *not* optional. The portable argument values are 0,
EXIT_FAILURE, and EXIT_SUCCESS; the latter two are macros defined in
<stdlib.h>. <stdlib.halso declares the exit() function; your
failure to add the required "#include <stdlib.h>" at the top of your
program is probably why your compiler didn't complain about the
missing argument.

You should add a "return 0;" at the end of your program.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 21 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: clusardi2k | last post by:
/* The below code on SGI will wait for you to enter 2 things, but on Linux it will only wait the first time. I can make the code work by replacing the scanf with: char data ; fgets...
12
by: Emmanuel Delahaye | last post by:
Hi there, It is commonly accepted that a call to the getchar() function suspends the execution of the current program. I have not found any description of this behaviour in the standard (I may...
1
by: White Spirit | last post by:
I'm trying to use getchar() to read alphanumeric data as follows:- char input; /* Take a string of input and remove all spaces therein */ int j = 0; while ((input = getchar()) != '\n') { if...
13
by: broeisi | last post by:
Hello, Can someone help me out with this one? The following program should read characters till it met the EOF (-1) on my pc. I'm running linux and using the gcc compiler version 3.4.5. But...
5
by: Jonathan | last post by:
Hi-- I have the following code: #include <stdio.h> char a,b; int main()
25
by: ehabaziz2001 | last post by:
Why I can not begin my subscript of character arrrays with 0. In this program I can not do : do { na=getchar(); i++; na=getchar(); } while (na!='\n');
14
by: arnuld | last post by:
i have slightly modified the programme from section 1.5.1 which takes the input frm keyboard and then prints that to the terminal. it just does not run and i am unable to understand the error...
3
by: mahiapkum | last post by:
Hello, I have a code which uses getchar(). #include<stdio.h> int main() { char ch; ch = getchar(); if(ch == 'Y') { printf("Beautiful...
22
by: arnuld | last post by:
Mostly when I want to take input from stdin I use getchar() but I get this from man page itself: "If the integer value returned by getchar() is stored into a variable of type char and then...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.