Unexpected output of scansets in C

as expected this prog. should accept a number until it encounters a 4 but it gives some garbage value. why?

int main(void) { int a; printf("Enter a number: "); scanf("%[^4]d", &a); printf("You entered: %d\n", a); return 0; }

-------------Problems Reply------------

As far as I know scansets are meant to be used with strings (which makes the d not act as an integer placeholder specification). One way to write it is to read the input into a string and then parse it:

int main(void)
int a;
char b[255];
printf("Enter a number: ");
scanf("%254[^4]", &b);

a = atoi(b);
printf("You entered: %d\n", a);

return 0;

I'm keeping the modified code to a minimum, you'd definitely need some extra checks for input sanity.

To clarify: The 254 prefix limits the amount of data that scanf will capture, so as to not exceed the size of the buffer (strings are terminated with an extra null character, so the read length must be smaller than the actual size)1.

The scanset working with only characters.

Here is my sample code. (but, I don't know what you really want.)

#include <stdio.h>
int main(void) {
char buffer[128];
printf("Enter a number: ");
scanf("%[^4]s", buffer);

printf("You entered: %s\n", buffer);

return 0;

The result is,

Enter a number: 12345678
You entered: 123

Additionally, if you want integer value, use atoi().

Category:c# Views:2 Time:2018-07-08
Tags: set

Related post

  • Can someone explain this Python re.sub() unexpected output? 2009-07-28

    I am using Python 2.6 and am getting [what I think is] unexpected output from re.sub() >>> re.sub('[aeiou]', '-', 'the cat sat on the mat') 'th- c-t s-t -n th- m-t' >>> re.sub('[aeiou]', '-', 'the cat sat on the mat', re.IGNORECASE)

  • The plugin generated x characters of unexpected output! 2010-11-02

    I'm getting this message each time I activate my plugin: The plugin generated 80 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or re

  • The plugin generated xxx characters of unexpected output during activation 2011-10-11

    all I am creating wordpress plugin just for improving my WP knowledge. This plugin display Total Twitter counter & Feed Subscriber.You can manage it via widget. and i am getting this error. The plugin generated 123 characters of unexpected output

  • why is my wordpress plugin reciving "unexpected output" 2013-06-01

    when i install this wordpress plugin it gives me: The plugin generated 189 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing

  • Unexpected output from Bubblesort program with MSVC vs TCC 2011-01-01

    One of my friends sent this code to me, saying it doesn't work as expected: #include<stdio.h> void main() { int a [10] ={23, 100, 20, 30, 25, 45, 40, 55, 43, 42}; int sizeOfInput = sizeof(a)/sizeof(int); int b, outer, inner, c; printf("Size is

  • php date() functions producing unexpected output 2011-03-11

    I'm trying to do some basic date and timestamp operations in PHP and have been getting unexpected results. I'm trying to get a timestamp with just the year, month, and day of the month (i.e. the timestamp of a date at 12:00 A.M.) of the existing time

  • unexpected output in function calls 2011-09-02

    i have a program in which i have implemented main function as below and finally i getting a unexpected value of i. int main() { int fun (int); int i=3; fun(i=fun(fun(i))); printf("%d",i); return 0; } and my function implementation is int fun(int i) {

  • Unexpected output of std::wcout 2009-04-06

    While testing some functions to convert strings between wchar_t and utf8 I met the following weird result with Visual C++ express 2008 std::wcout << L"élève" << std::endl; prints out "ÚlÞve:" which is obviously not what is expected. This

  • Enumerating array which contains a dictionary produces unexpected output 2009-07-02

    My question is why does it output the last 4 lines in the log(see below)...those objects are part of the dictionary printed earlier in the log & should not be located at the end of the array? I am missing something fundamental here... thx NSDicti

  • Why does this C++0x program generates unexpected output? 2010-02-02

    This program: test_header.hpp #include <boost/signal.hpp> #include <utility> class Sensor; class Recorder : public ::boost::signals::trackable { public: explicit Recorder(int id) : id_(id) {} // Cannot be copied Recorder(const Recorder

  • jQuery - I'm getting unexpected outputs from a basic math formula 2010-05-21

    Hi I would like to start by saying I'd greatly appreciate anyones help on this. I have built a small caculator to calculate the amount a consumer can save annually on energy by installing a ground heat pump or solar panels. As far as I can tell the m

  • Unexpected output when comparing database column to variable 2010-07-20

    I have code which retrieves information about players from a MySQL database. I want to apply a special case to the HTML output if their ranking changes. I want it to look like this: But i cant get it to be like i want, instead it prints the rank on e

  • unexpected output for map inside of do 2010-08-29

    Why doesn't this produce the output I expect? (defn test-fn [] (do (println "start") (map #(println (+ % 1)) '(1 2 3)) (println "done"))) It outputs start done Whereas I would expect start 2 3 4 done --------------Solutions------------- map is lazy,

  • Unexpected output on file include 2010-08-30

    I've been working on a custom CMS in PHP and haven't run into any problems until recently. When testing, I've noticed that the string  has started appearing at the top of only the index page. Testing with some die statements throughout the code, i

  • Why does this C program give unexpected output? 2010-09-06

    Possible Duplicate: C programming: is this undefined behavior? #include<stdio.h> main() { int i=5; printf("%d%d%d",i,i++,++i); } my expected output is 556. But when i executed it the result is 767. how? --------------Solutions------------- You

  • Unexpected Outputs in Left Shift Operation-? 2010-10-23

    I read that Left shift e1< x=5; printf("%d",x<<3); Output is 40 but according to me it should be 30. and for x<<4 it is 80 .(but expected 40). Although for x<<1 and x<<2 outputs are 10 and 20 as expected.Please explain this

  • Unexpected output (BMP) file 2010-11-01

    I'll make an edge detection with Sobel operator if I managed to create a double array that keeps the pixel values of 8bit Gray-scale BMP file. After execution of program, it creates an weirdy bitmap file: What could be the missing point? Thanks for h

  • Unexpected output when using a ternary operator and final variable 2011-01-17

    Consider this code snippet: public static void main(String[] args) { int z1 = 0; final int z2 = 0; System.out.println(false ? z1 : 'X'); System.out.println(false ? z2 : 'X'); } When running this code, I would expect to see two X in your console. Howe

  • Nokogiri XML Builder creating unexpected output when scraping HTML 2011-01-22

    I am fairly new to Ruby and the world of programming so please, bear with me. My goal is to scrape a table and then save the data to an XML file. The simple script that I've written successfully accomplishes both things. The problem I am having is th

  • Assembly 386: Unexpected output when using Interrupt 21h with function 02h 2011-02-19

    I'm beginning today with Assembly (i386) and I'm trying to output the caracters of a 2-digits number. The way I found after some resarch is to divide my number by 10d to get the remainder and the quotient and then to use interruption 21h with functio

  • Haskell: Unexpected output for expression [0, 0.1 .. 1] 2011-02-19

    When evaluating the expression: *main> [0, 0.1 .. 1] I was actually expecting: [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1] But I was quite shocked to see the output be [0.0,0.1,0.2,0.30000000000000004,0.4000000000000001,0.5000000000000001,

  • Crash and unexpected output when using Java sip servlets 2011-02-25

    I am trying a simple program in Java which would display Received Invite and Received Cancel when x-lite SIP client calls the SIP servlet and cancels the call respectively. My Java code is below. I am using Tomcat Mobicents SIP servlet - package org.

  • Getting unexpected output when running a function(s). Outputting the code of a prototype 2011-03-02

    When the loadGame() function is called, it should build deck of cards, deal the cards, then show the cards. It does all this plus more. When it the function to show the cards to the user is run, it outputs the number representing a card(s) along with

  • Unexpected output with HashMap 2011-03-16

    CODE: public class Puzzle23{ void Puzzle23(){ map1.put(String1, "1"); map1.put(String2, "2"); } private final NewMap map1 = new NewMap(); private static final String String1 = new String("J2eeSig"); private static final String String2 = new String("J

  • Unexpected output in this program 2011-03-30

    #include<stdio.h> int main() { char arr[20]; printf("\nEnter your name: "); gets(arr); printf(arr); } Input: %d%s%u Output: 0<null>-056716812[ Why is the output like this? --------------Solutions------------- printf writes the output unde

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.176 (s). 11 q(s)