GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 370 of 453    Print  
why scanf("%d",&n) and gets(string) don't work together?

  
Total Answers and Comments: 1 Last Update: September 18, 2007     Asked by: pbchaudhari 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
September 18, 2007 02:49:03   #1  
baseersd Member Since: June 2007   Contribution: 34    

RE: why scanf("%d",&n) and gets(string) don't work tog...
This is a general problem of the keyboard buffer. When scanf(" d" &n); is used and when an input is entered the newline character("n") along with the input is stored in buffer. This buffer gets clear as the user presses "Enter" or if the buffer is full.

So when we use the scanf(" d" &n); the "n" is left in the buffer itself. So the buffer will fill the string with " ". Hence it will not print anything in place of string.

The Following code will help in understanding this point.

#include<stdio.h>
int main()
{
int n;
char string[20];

printf("String :: s" string);

printf("Enter the number");
scanf(" d" &n);

printf("nEnter the string");
gets(string);

printf("Number :: d" n);
printf("String :: s" string);
if(strcmp(string " ") 0)
printf("Null string");
system("pause");
return 0;
}

Here initially i am trying to print the contents of the string.... It gives garbage.Later
in the output Null string will be displayed. Which means the string is containing " ".

Note :
When we change the order ie. gets(string); and then scanf( d" &n); we will not have this problem.

Solution :
The solution for such problems is to use fflush(stdin); This clears the keyboard buffer. Hence we can get the desired output. This fflush() should be used after entering the input. ie after scanf() and before gets().

#include<stdio.h>
int main()
{
int n;
char string[20];

printf("String :: s" string);
printf("Enter the number");
scanf(" d" &n);
fflush(stdin);
printf("nEnter the string");
gets(string);

printf("Number :: d" n);
printf("String :: s" string);
if(strcmp(string " ") 0)
printf("Null string");
system("pause");
return 0;
}

Hope i answered correctly. If you have any doubt feel free to ask it.




 
Is this answer useful? Yes | No

 Related Questions

The answer depends on what you mean by quickest. For most sorting problems, it just doesn’t matter how quick the sort is because it is done infrequently or other operations take significantly more 
Latest Answer : The algorithms which follows divide and qunquer technique provides fastest implementation. ...

A type-insensitive macro is a macro that performs the same basic operation on different data types. This task can be accomplished by using the concatenation operator to create a call to a type-sensitive 

What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used?
The strcpy() function is designed to work exclusively with strings. It copies each byte of the source string to the destination string and stops when the terminating null character () has been moved. 

The standard C library provides several functions for converting numbers of all formats (integers, longs, floats, and so on) to strings and vice versa  The following functions can be used to convert 
Latest Answer : sprintf() is used to convert integer to string. ...

The standard C library provides several functions for converting strings to numbers of all formats (integers, longs, floats, and so on) and vice versa.  The following functions can be used to convert 
Latest Answer : /*Let take an example*/main(){char rasmi="111";int ranjan;ranjan=atoi(rasmi);printf("n This is Rasmi Ranjan Nayak's Reply %d n", ranjan);} ...

/* Use printf() to print the first 11 characters of source_str. */ printf(“First 11 characters: ‘%11.11s’n”, source_str);  
Latest Answer : You can also print it using pointers....Try this following code segment ... This does the trick. char str[]="The Sky is Blue and so are you";char *s1= &str[5];printf("%sn",s1); ...

A void pointer is a C convention for “a raw address.” The compiler has no idea what type of object a void Pointer “really points to.” If you write int *ip; ip points to an int. 
Latest Answer : Void Pointer or Generic Pointer is the one to which any datatype can be assigned.eg.main(){    int *p;     void *vp;     vp=p;} ...
Tags : Pointer

The hardest part about using a pointer-to-function is declaring it.  Consider an example. You want to create a pointer, pf, that points to the strcmp() function. The strcmp() function is declared 
Tags : Pointer

Sometimes you can get away with using a small memory model in most of a given program. There might be just a few things that don’t fit in your small data and code segments. When that happens, you 
Tags : Pointer

No. Pointer addition and subtraction are based on advancing the pointer by a number of elements. By definition, if you have a void pointer, you don’t know what it’s pointing to, so you don’t 
Tags : Pointer


 Sponsored Links

 
Related Articles

How EDI work with XML

How EDI work with XML EDI and XML systems have been seen as the opportunity to create a holistic approach to data information exchange that can deliver and process simple durable and effective business transactions by electronic means To achieve this methods must be employed that are not only of val
 

Concepts of Object-Oriented Programming

Object Oriented JavaScript In this chapter you ll learn about OOP Object Oriented Programming and how it relates to JavaScript As an ASP NET developer you probably have some experience working with objects and you may even be familiar with concepts such as inheritance However unless you re already a
 

C++ String Representation and Handling

C String Representation and Handling In this C tutorial you will learn about string representation and handling how is string represented end string notation initializing char array character representation string literals example programe to understand the character array concept and the accessing
 

JavaScript String Object

JavaScript String Object In this JavaScript tutorial you will learn about String Object purpose of string object in JavaScript purpose of string object indexof method lastIndexOf method and substring method along with syntax and example mosgoogle center Purpose of String Object in JavaScript The mai
 

Convert a String into an Integer

How to convert a string into an integer in C program? This is done by using the function named as atoi(). This function atoi() is present in the header file named as &lt;stdlib.h&gt;. When programmers use this function they must include the header file by the statement #include &lt;stdl
 

String handling functions

What is the string handling functions present in header file &lt;string.h&gt;? There are number of string handling functions present in string.h. In other words if a programmer uses any of the function present in string.h then they must include the header file as #include &lt;string.h&am
 

Convert a String into Long Value

What string function is used to convert a string into long value? This is done by using the function named as atol() . This function atol() is present in the header file named as &lt;stdlib.h&gt;.&nbsp;When programmers use this function they must include the header file by the statement
 

Base Address of the String

What happens when we try to change the value of base address of the string? The base address of the sting takes a special place in the context of strings. This is because suing this base address only the string gets identified. In other words the base address therefore takes the position of constant
 

SQL Programming

SQL Programming Overview Anybody who has done something for a long time has probably wanted to change how things work at some point or another. A worker at a mill might have found a more efficient way of cutting logs, or a mathematics teacher might have had a hand in changing a school&rsquo;s al
 

The Interview Snafu

How to turn someone else&rsquo;s mistake to your advantage Your dream job is about to become reality. A recruiter gave you the heads up about the perfect position at Humungous Conglomerate, Inc. You went through five interviews as well as a battery of psychological tests mandated by their HR de
 

About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape