Scanf optional input

Posted: Kash65 Date of post: 24.06.2017

I'm working on a project and I can't seem to figure out a way how to read a string with an int that's not always there. Then I'd like to put "string" in instruction and leave argument equal to 0.

But the program expects a number after "string" and won't continue until it gets it. How can I read input from stdin when I can have 2 different types of input? I tried several solutions, but none of them worked. By posting your answer, you agree to the privacy policy and terms of service. Stack Overflow Questions Developer Jobs Documentation beta Tags Users. Sign up or log in to customize your list.

Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us.

scanf optional input

Log In Sign Up. Join the Stack Overflow Community.

Stack Overflow is a community of 7. Join them; it only takes a minute: C - scanf string with optional int Ask Question. You do that by not trying to parse standard input directly.

scanf, _scanf_l, wscanf, _wscanf_l

Instead you Read a line of input with fgets. Attempt to parse that line into an integer possibly with sscanf: If you succeed you have your integer.

Otherwise you have the original string. Hi, could you elaborate a bit more how to attempt to parse with 'sscanf'? But bear in mind that scanf and sscanf function family all return a value: If not enough items, scanf will stall but sscanf will not.

WeatherVane Thank you, I absolutely did not realize that the functions returned a value.

scanf - C++ Reference

I tried to quickly throw something together and it seems to be working so far. Sign up or log in StackExchange.

Sign up using Facebook. Sign up using Email and Password.

scanf format string - Wikipedia

Post as a guest Name. Stack Overflow works best with JavaScript enabled. Thanks c string input scanf stdin share improve this question.

C Programming Tutorial-6: Receive Input from Users Using scanf()

MathOverflow Mathematics Cross Validated stats Theoretical Computer Science Physics Chemistry Biology Computer Science Philosophy more 3. Meta Stack Exchange Stack Apps Area 51 Stack Overflow Talent.

inserted by FC2 system