scanf

don't care in scanf

Imagine the following: you read in a string with scanf() but you only need a few of the datapoints in the string. Is there an easy way to throw away the extraneous information, without losing the ability to check if the appropriate data is there so you can still reject malformed strings easily? example: const char* store = "Get: 15 b...

Getting multiple values with scanf()

I am using scanf() to get a set of ints from the user. But I would like the user to supply all 4 ints at once instead of 4 different promps. I know I can get one value by doing: scanf( "%i", &minx); But I would like the user to be able to do something like: Enter Four Ints: 123 234 345 456 Is it possible to do this? ...

Help While Loop in C

I'm new to C programming, I come from a Java background. I was wondering why in the following code, in the while loop I have to type my input ten times and then all ten inputs are displayed. I'm trying to type something once and have it displayed right after. Then continue typing my other inputs. #include <stdio.h> #include <stdlib.h> ...

Simple C Program

Ok so I am trying to learn C and I want my user to input a value so I am using scanf. I started off not having the flushes, because nothing was comming up until I typed in two values. Now that I have them though I get the same problem there still is no output until after I type in two numbers. Here is my code: #include <stdio.h> usin...

Can I use scanf to capture a directive with a width specified by a variable?

I have the following code: scanf(" %Xs %Ys", buf1, buf2); Where X and Y should be integers. The problem is that the values for X and Y are compile-time constants, and even if I wanted to hard-code the values into the format string, I can't, because I don't know the values. In printf, you can send a width variable along with the argume...

How to Take whitespace in Input in C

I wanted to take character array from console and it also include white spaces, the only method i know in C is scanf, but it miss stop taking input once it hit with white space. What i should do? Here is what i am doing. char address[100]; scanf("%s", address); ...

Read In Hex Values (C)

Hello all. I am currently attempting to read in Hex values from a text file. There can be multiple lines of Hex's and each line can be as long as needed: f53d6d0568c7c7ce 1307a7a1c84058 b41af04b24f3eb83ce Currently, I put together a simple loop to read in the Hex values into an unsigned char line[500] with fscanf as such: ...

How to prevent scanf causing a buffer overflow in C?

I use this code: while ( scanf("%s", buf) == 1 ){ What would be the best way to prevent possible buffer overflow so that it can be passed strings of random lengths? I know I can limit the input string by calling for example: while ( scanf("%20s", buf) == 1 ){ But I'd prefer to be able to process whatever the user inputs. Or can't...

What is the difference between these three fscanf calls in OCaml?

I wrote a short bit of code to simply skip num_lines lines in an input file (printing the lines out for debugging purposes. Here's two things I tried that didn't work: for i = 0 to num_lines do print_endline (fscanf infile "%s" (fun p -> p)); done;; for i = 0 to num_lines do print_endline (fscanf infile "%S\n" (fun p -> p)); done;...

Scanf skips every other while loop in C

I'm trying to develop a simple text-based hangman game, and the main game loop starts with a prompt to enter a guess at each letter, then goes on to check if the letter is in the word and takes a life off if it isn't. However, when I run the game the prompt comes up twice each time, and the program doesn't wait for the user's input. It a...

Validate max integer in scanf

I want to read a int from stdin but I want to validate if the user exceeds the int max value. How can I do it? int n; scanf("%d", &n); scanf reads the decimal input and stores in the int, causing overflow. How can I check and avoid this? ...

scanf() causing infinite loop

I've a small C-program which just reads numbers from stdin, one at each loop cycle. If the user inputs some NaN, an error should be printed to the console and the input prompt should return again. On input of "0", the loop should end and the number of given positive/negative values should be printed to the console. Here's the program: #...

Overflow over scanf("%8s", string)?

Hi, I know it's possible to overflow ordinary code: char string[9]; scanf("%s", string). But is it possible to overflow scanf("%8s", string)? 8 is just an example. I know "%8s" works like a delimit, but I also notice when I input string longer than 8 chars, the program will terminate due to: * stack smashing detected *: ./a.out ter...

does scanf() take '\n' as input leftover from previous scanf()?

in the following c code: char name[20]; int a; int b; for(i=0;i<10;i++) { printf("\nEnter name, a & b: "); scanf("%s %d %d",name,&a,&b); } does scanf read in the '\n', entered at the end of scanf() in 1st iteration, for the 2nd iteration inputs? ...

fscanf problem with reading in String

Hi, I'm reading in a .txt file. I'm using fscanf to get the data as it is formatted. The line I'm having problems with is this: result = fscanf(fp, "%s", ap->name); This is fine until I have a name with a whitespace eg: St Ives So I use this to read in the white space: result = fscanf(fp, "%[^\n]s", ap->name); However, when I try ...

Problem with scanf in Eclipse / MiniGW

I'm trying to run the following code in eclipse but the console remains blank until i stop the program at which point the output "Enter next value (<=0 to quit)2130567168 minutes is 35509452 hours, 48 minutes." is repeated over and over. It seems that scanf is putting some default value in for some reason... can't figure out why. I'm no...

Why doesn't scanf need an ampersand for strings and also works fine in printf (in C)?

I am learning about strings in C now. How come to use scanf to get a string you can do scanf("%s",str1); and for printf you can do printf("The string is %s\n", str1); I understand that for scanf it is because the string is just a character array which is a pointer, but for printf, how is it that you can just put the variable name...

C - trying to read a single char from stdin (and failing) w/ scanf / getchar

Hi - as a part of a homework assignment, I'm trying to read a single char from stdin, and act according to it: char choice; while (1) { printf("please enter [y]es, [n]o or [m]aybe: "); scanf("%c", choice); fflush(stdin); // validate input if (choice == 'y' || choice == 'n' || choice == 'm') { break; } el...

C - Check if a value is a number?

How can I check if an integer initialized from function scanf is a number? ...in the simpler way possible thanks :) ...

Unexpected behavior when printing 4-byte integer byte by byte.

I have this sample code for converting 32 bit integers to ip addresses. #include <stdio.h> int main() { unsigned int c ; unsigned char* cptr = (unsigned char*) while(1) { scanf("%d", printf("Integer value: %u\n",c); printf("%u.%u.%u.%u \n",*cptr, *(cptr+1), *(cptr+2), *(cptr+3) ); } } This code gives incorrect output for...