473,387 Members | 1,799 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.

Confusion on \n and enter key

see this progrmmmmm
Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.   char ch;
  4.   printf("enter the string");
  5.   do 
  6.   {
  7.     ch=getchar();
  8.   } while(ch!='\n');
  9.   getch();
  10. }
here that do while loops end when we press the "enter key"
but wht is my question is ascii value of "enter key" is 13..
while asccii of '\n ' is 10
how this possible
this program still runs

plz any one say the reson behind this
Feb 16 '08 #1
1 13362
ashitpro
542 Expert 512MB
see this progrmmmmm
void main()
{
char ch;
printf("enter the string");
do
{
ch=getchar();
}while(ch!='\n');
getch();

}

here that do while loops end when we press the "enter key"
but wht is my question is ascii value of "enter key" is 13..
while asccii of '\n ' is 10
how this possible
this program still runs

plz any one say the reson behind this

Well,
here you have to understand how getchar() works.
when you issues a getchar().
getchar() waits for a "character+enter key" , however if you enter "string+enter key" it will consider a first character of a string you entered.
Now result will be put in 'stdin' as "character you entered+\n"
In simple language we can say that it converts "enter key" to '\n'.
So when you hit only enter key. your 'ch' variable will end up with having '\n' in it.
If you don't want this behaviour, use
ch = getch() or ch = getche(), instead of ch=getchar();
In former case your will see the '13' value in 'ch' variable.
and of course in later case 'ch' will have '10' i.e '\n'.
that's why your loop gets terminated
Feb 16 '08 #2

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

Similar topics

38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
2
by: Bill C. | last post by:
Hi, I'm trying to implement a ComboBox drop-down column for a DataGrid. When a cell is selected in the ComboBox column I overlay a ComboBox over the cell and call: this.comboBox.Show();...
8
by: Z D | last post by:
Hello, I'm having a strange problem that is probably due to my lack of understanding of how threading & COM Interop works in a WinForms.NET application. Here's the situation: I have a 3rd...
9
by: William LaMartin | last post by:
Does anyone know when we can actually purchase Visual Studio 2005 Professional or the upgrade and have it shipped immediately? First Microsoft said that it would be released Nov. 17. When Nov...
4
by: Ranginald | last post by:
Sorry for the simple question but thanks in advance: My goal is to create reusale code for a web app in C#. I have written the code already as a windows app but here is where I am confused: ...
2
by: shumaker | last post by:
I have a combobox that is very much like the one found in the RSS project here: http://msdn.microsoft.com/vstudio/express/visualCSharp/learning/ My projectNameComboBox basically is filled with a...
5
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
14
tolkienarda
by: tolkienarda | last post by:
hi all i read the scanner class documentation from sun's website and i thought i would have some fun trying to write a program that stores a line from the user to var input and then parses it on...
8
by: carlos123 | last post by:
hello, i am making a hall pass program for my computer programming class. i am having issues with my jcheckboxes. first off. this file reads from a .txt that just has greg john joe 54343 45777...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.