473,789 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question regarding scanf

4 New Member
Hi all. I am beginner in C. My question is: What is " \n" inside scanf()?

Here is part of the implementation of an example:
...
char myChar;
int myID;
scanf("%c", &myChar);
scanf("%d\n"; &myID);
printf("Option: %c, ID: %d\n", myChar, ID);
...
The reason that I break scanf() into 2 parts is that I need to take either one argument or two arguments from ONE input line depending on what the first argument is. The following is the input line:
a
(or)
b 120

*a -- I only need one argument.
*b -- I need two arguments; the second argument is the ID.

The problem is my program still asking for the next input line before the results are printed. How I can fix it? Thanks.
Oct 28 '06 #1
1 1651
RADAR
21 New Member
Hi all. I am beginner in C. My question is: What is " \n" inside scanf()?

Here is part of the implementation of an example:
...
char myChar;
int myID;
scanf("%c", &myChar);
scanf("%d\n"; &myID);
printf("Option: %c, ID: %d\n", myChar, ID);
...
The reason that I break scanf() into 2 parts is that I need to take either one argument or two arguments from ONE input line depending on what the first argument is. The following is the input line:
a
(or)
b 120

*a -- I only need one argument.
*b -- I need two arguments; the second argument is the ID.

The problem is my program still asking for the next input line before the results are printed. How I can fix it? Thanks.
[code]
char myChar;
int myID;
scanf("%c", &myChar);
scanf("%d\n"; &myID);
printf("Option: %c, ID: %d\n", myChar, ID);
[\CODE]

I think you can design and implement your program without using any
'\n' in scanf because your values can not be executed if you do that.
[code]
char myChar;
int myID;
scanf("%c",&myC har);
scanf("%d",&myI D);
printf("Option: %c, \nID:%d\n",mych ar,myID);
[\CODE]

the important thing is '\n' in scanf is a logical mistake not a run-time error so your program in hole can work but your values can not be executed.Whenev er you use \n in bracks(" ") compiler will understand there is nothing wrong, the real problem will be seen in executing your values.
Try to use printf("Option: %c, \nID:%d\n",mych ar,myID); , I mean you wrote ID first so computer will throw away some number into ID.So you must write myID.
Respects...
Oct 28 '06 #2

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

Similar topics

9
3418
by: R.Neeser | last post by:
Hello, how do i get an keyboard input from the consol, WITH THE Space char? scnaf and all the other function divide the input string on such a char and give every part to a different variable. but i don't need that. char buf; scanf("%s",char); printf("%s",buf);
35
3192
by: Henry | last post by:
I was doing this program for an exercise in a book. The point was to create a program that would take a numerical grade from a user and convert it to a letter grade (yeah really easy). I tried to incorporate the while loop to keep the program running continuously so the user could keep entering grades to get the letter number, but I had little success and just kept creating infinite loops. My question is: How could I modify my program...
19
770
by: amanayin | last post by:
Why does this program run in the ddd debugger but when i try to run the program in a konsole i get a segmentation fault. I just can't work it out the OS i use is suse 8.2 pro program below: /* OVERLAY.C OVERLAY OF STRINGS */ #include<malloc.h> #include<string.h> #include<stdio.h> #define MAX_LEN 40 int main(void)
12
1822
by: drM | last post by:
I have looked at the faq and queried the archives, but cannot seem to be able to get this to work. It's the usual factorial recursive function, but that is not the problem. It hangs after the user enters a number. However, as I indicate, if one adds something else after the number, the function proceeds and finishes successfully. I would appreciate some helpful hints. thanks in advance. >>>>>>>>>
1
1569
by: chump1708 | last post by:
main() { int i; clrscr(); printf("%d", &i)+1; scanf("%d", i)-1; } whats the output n why??? Note that there is & which means it probably prints the address of
8
2327
by: AG | last post by:
Hello, This is my first post to this group, and on top of that I am a beginner. So please direct me to another group if this post seems out of place.... I have recently written a program which calculates loan amortization schedules, writes the data to a text file, and then upon user prompt, the program will display the created file and print the file. Until this morning, everything worked fine, and then I started messing around with...
1
1526
by: nozone | last post by:
Hi. The following is my code of an example: #include <stdio.h> /*line 1*/ main() /*line 2*/ {char *command = (char *)calloc(10, sizeof(char)); /*line 3*/ char *middle = (char *)calloc(10, sizeof(char)); /*line 4*/ char *name = (char *)calloc(20, sizeof(char)); /*line 5*/ char...
22
2777
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never cropped up before, since I rarely use "float"s for anything, and hardly ever use the function for floating-point numbers in the first place; I just was messing around testing it for all cases and noticed a problem.) Anyway, it is declared and I...
2
6555
by: subramanian100in | last post by:
Consider the following program named as x.c #include <stdlib.h> #include <stdio.h> int main(void) { unsigned int u; char str;
0
9663
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10404
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10193
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6761
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4089
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.