Questions tagged [scanf]
Questions related to the scanf() family of functions in the C runtime library, which read and convert formatted data. (Includes scanf(), sscanf(), fscanf(), and their variadic equivalents.)
7,041
questions
0
votes
0
answers
8
views
sscanf like this [a-zA-Z] does not work in keil(mdk)
sscanf do not support Abbreviations in KEIL? Why?
Here is the situation:
KEIL version and Configuration like this:
KEIL version and Config image
I want to use sscanf to identify each part of the ...
0
votes
2
answers
59
views
C: Size of array changes scanf
I'm trying to understand, why does the size of array change the output (input is 10 5). If I set char k[1] then it only prints 5, but if I set char k[2] then if prints 10 5.
This is my code:
#include &...
0
votes
0
answers
41
views
Scanf printing weird characters after the input
This code takes in input N-file names and generates N-threads, then the main thread writes something and puts it in a buffer for each thread, and each threads writes their buffer content on their own ...
1
vote
3
answers
65
views
scanf() not writing to 2d char array
I am working through a textbook teaching myself C programming using the Code::Blocks v20.03 IDE.
I am really bamboozled by a small program that is to read in three small strings into a 2d char array ...
0
votes
0
answers
37
views
scanf doesn't stop with qt creator, with visual studio code works, i have the problem just with qt creator
scanf, getchar, fgets doesn't stop when enter is pressed with qt creator, with visual studio code works
where the problem ?
i don't want use visual or eclipse or another for work in c.
#include <...
1
vote
2
answers
51
views
Is there some problem with the following c code
When i compile and run this program my input string is not same as output.
#include<stdio.h>
int main()
{
int n; char ch[100];
scanf("%d : %5s", &n, ch);
printf("%...
0
votes
2
answers
45
views
Differing output with printf using %d and %i
I found questions about %i and %d on here, but all of them seemed to claim that they were the same in printf.
Compiler: Apple clang version 12.0.5 (clang-1205.0.22.9)
Note: 15 is 017 in octal and 0xf ...
3
votes
3
answers
68
views
Printing text too often?
i am new to c and have used python before. This whole buffer overflow stuff is really breaking my mind.
#include <stdio.h>
int main(){
char str1[3];
while(true){
scanf("%...
-1
votes
2
answers
34
views
reading string from a file via stdin in c11
So I have a .txt file that I want to read via stdin in c11 program using scanf().
The file is essentially many lines made of one single string.
example:
hello
how
are
you
How can I know when the file ...
0
votes
2
answers
62
views
why is the output of my program 0.000000 when it should be the value i entered when prompted. below is the code
I am trying to execute this code. but the number variable isn't storing the value I input,what could be posssible explanations of this behaviour.
#include <stdio.h>
int main(){
float number;
...
-6
votes
0
answers
54
views
Checking data type of user input in C [closed]
I would like to solve straightforward user input validation. I am dealing with a problem (in my number guessing game) that when user inputs anything else than int my program crashes.
So, I need to ...
0
votes
0
answers
41
views
c reading user input not working as intended, line gets skipped when entering data
After inputting the hourly wage, the compiler spits out the first and second printf, what methods could I use to fix this? Feel free to give any other feedback if some of this code is sloppy, I'm ...
0
votes
1
answer
33
views
I writing code to make marksheet of 10th class. there is fourth scanf is not taking input
My fourth scanf is not taking input?
I'm trying to make mark sheet in c language. Then my first three scanf run properly but my fourth scanf is not taking input my program is directly stop executing.
#...
-2
votes
0
answers
35
views
How to use if else statement with char [] in C [duplicate]
so I finally decided to start learing C and ran into this problem. I am fairly confident that I scanf correctly but when i added the if else statement I started running into issues. I was trying to ...
-1
votes
2
answers
42
views
scanf double returns garbage value if entered characters
scanf("%lf", &Deposit_Amount);
This statement returns garbage value instead of 0.00 when I enter a character.
Here's my code
double Deposit_Amount;
StartDepositInput:;
...
0
votes
0
answers
25
views
Not able to take the user input when using if-else and switch case [ C language] [duplicate]
I want someone to run the code, you will find that the code at line number 14 to 17, I don't what this error is. I am giving my code along with this error image.
/*
A simple calculator made with ...
0
votes
0
answers
32
views
Violation of access with pointers and scanf
The following code is supposed to insert the name of a good, whether it's getting stored or sent away (with just a single letter, like i for in or o for out) and in conclusion how much of that item is ...
-2
votes
1
answer
41
views
code is running , but I get nothing, scanf problem
#include <stdio.h>
#include <stdlib.h>
int main(){
int age;
printf("Please, Enter your age: \n");
scanf("%d", &age);
printf("your were born in %...
0
votes
3
answers
51
views
String Unexpected Behavior in C
I was writing a simple program to read a string and then print it.
#include<stdio.h>
// void printString(char *str){
// while(*str!='\0'){
// printf("%c",*str);
// ...
0
votes
1
answer
36
views
Why does my VScode not work when I input scanf?
So I'm new to coding overall and I'm using VScode, but in one of the user input tutorial I'm watching require the use of scanf(), but for some reason my code refuses to work, can someone help me!
#...
0
votes
0
answers
52
views
Would there be complications to use both fgets() and fscanf() to read the same file?
I'm writing the information of an array of struct in to a file and then convert the content of the file to a new struct. My code will write the array of struct port_table to a file in the following ...
0
votes
1
answer
34
views
How can i terminate a while loop with scanf?
im making a program to sum five values, with the option of sum less than five values, the program works right until i want to exit the while loop with this option("Do you want to charge another ...
0
votes
2
answers
51
views
How do I exit my program by pressing ENTER using scanf_s?
So i've done some googling and found that using the code:
#include <stdio.h>
void main()
{
char ch;
ch = fgetc(stdin);
if (ch == '\n')
{
printf("\n");
}
...
1
vote
1
answer
53
views
How to read a word in upper and lower case from file? Go
I have a task to read a file and, then scan for "bad word", and finally output "True" or "False" if "bad word" contains in file or not.
I wrote this function:
...
0
votes
1
answer
36
views
Why is the answer correct when using Scanf unsafe, but not when handling errors? (Go)
Just recently I started to learn Go. I did a simple task for JetBrains Academy.
There is a very simple code:
var number int
fmt.Scanf("%d", &number)
if number > 0 {
...
0
votes
0
answers
59
views
I need help regarding fscanf in c
I want a way to implement the function with delimiter, but it should be condition like scan until you find a space or end of line.
Let's say I have a file with the following contents at the start of ...
1
vote
2
answers
61
views
Scanf in while loop for getting matrix input
Can anyone explain how the below mentioned scanf part will execute in while loop for getting matrix input without boundary size in C?
#include <stdio.h>
#include <stdlib.h>
#include <...
1
vote
1
answer
57
views
How to use fgets() and sscanf
Suppose there is a case:
input required:
Name of Player
Height of Player
Age of Player
User may input in such a way that he may press enter(\n) after each input.
printf("Please enter Name, ...
0
votes
2
answers
75
views
How to get string terminated with new line in file handling using fscanf
I have these in my file:
JOS BUTTLER
JASON ROY
DAWID MALAN
JONNY BAISTROW
BEN STOKES
in different lines. And I want them to extract in my program to print them on the exact way they are in file. My ...
1
vote
1
answer
56
views
Read string with scanf() without knowing the length [duplicate]
I want to read a string (str) from the user and a number (num) via scanf() but I don't know how to initialize str correctly.
int main(void)
{
char *str = NULL;
int num;
scanf("%s %d&...
0
votes
1
answer
67
views
Can't read special characters with scanf [duplicate]
I'm building a system and i need a char inside a struct to receive a name and 2 values, but when I see the result, the name is always broken from a special character.
#define NOMINMAX
#include <...
1
vote
1
answer
45
views
i get some problems with scanf() in C [duplicate]
#include <stdio.h>
#include <stdlib.h>
void add(int *arr, int n);
int main() {
int arr[] = {};
add(arr, 4);
return 0;
}
void add(int *arr, int n){
for (int i = 0; i < n; i++)...
2
votes
2
answers
80
views
How NOT to read float type in scanf when reading type int
I need to write two integers (and nothing else) to variables. It does the validation makes sure that none of args is a string and that they are not empty and excepts division by zero, or when i put ...
-1
votes
1
answer
54
views
I want to fill a 2d array using pointer and scanf
#include <stdio.h>
#include <stdlib.h>
int main()
{
char arr[20][20];
char (*p)[20];
p = arr;
for (int x = 0; x < 3; x++)
{
for(int y = 0; y &...
1
vote
3
answers
419
views
Printf a double
I have a problem in printing a double in C.
My code:
#include <stdio.h>
main() {
double n;
scanf("%lf", &n);
printf("%f", n);
}
input: 446486416781684178
...
0
votes
1
answer
51
views
printing characters using array in c language?
I tried to scan and print the characters of array using below code but input characters are not matching with output characters
#include <stdio.h>
int main() {
char s[10];
int i, n;
...
3
votes
3
answers
75
views
Use scanf to split a string on a non-whitespace separator
I aim to scan a string containing a colon as a division and save both parts of it in a tuple.
For example:
input: "a:b"
output: ("a", "b")
My approach so far keeps ...
2
votes
3
answers
127
views
Scanf() not able to detect wrong input
int i, f;
f = scanf("%d", &i);
When I enter input as 3333333333333333333333 (greater than the capacity of int). Shouldn't the value of f be 0?
0
votes
1
answer
47
views
How does scanf store value?
I'm new to C. Given the following code,
int main(void)
{
int a;
scanf("%d",&a);
scanf("\n"); // consume trailing newline
}
As I understand it, the first declaration ...
0
votes
2
answers
53
views
How to read a string with spaces in c?
I read online that fgets can do it but I'm only familiar with scanf, and couldn't understand how to use fgets properly. I'm a beginner so the main part of what I don't understand about fgets is the ...
-1
votes
1
answer
67
views
Using Scanf function in C for some period of time [duplicate]
The requirement of my program is, it should move to next line without waiting for the user to enter while using Scanf(). The user have to enter the input within that period of time.
What I can do so ...
0
votes
2
answers
28
views
How to get word from string containing numbers through sscanf?
I have string "word123" need to extract only "word" using sscanf.
char str[10];
sscanf("word1233","%s", str);
-1
votes
1
answer
72
views
why does the loop execute just one time? [closed]
I would like to know why the loop below just execute one time?
Here is my code.
for(i=0;i<MAX;i++)
{
printf("name:");
gets(student[i].name);
printf("math score:");
...
0
votes
1
answer
80
views
The fgets statement doesnt get executed
Here is my code for a simple programme in C for storing details of students in a struct. The problem that I am having is that the fgets statement for taking in the name string doesn't get executed and ...
0
votes
2
answers
63
views
How do I read a whole line to a string?
int run_add_line_after(Document *doc, char *command) {
int paragraph_num, line_num;
char com[MAX_STR_SIZE + 1], extra[MAX_STR_SIZE + 2], line[MAX_STR_SIZE + 1];
if (sscanf(command, " ...
-2
votes
3
answers
41
views
first scanf gets other chrachters for the next scanfs
I'm making a simple calculator in C and I want the program to show the message "Enter an operator!" when the user enters something that isn't an operator, enter, space or tab. but if the ...
1
vote
3
answers
49
views
Multiple fscanf statements in a loop. How do I get when EOF is reached?
I'm having problems while reading a file -again.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct camion {
int data[3];
int numeropacchi;
...
-3
votes
1
answer
32
views
Initializing and scanning a variable in C [duplicate]
can anyone please help me out with this in C language
for ex:
int a = scanf("%d", &a);
doesn't assign the input value to a, but instead gives zero only always.
but this works as ...
1
vote
2
answers
53
views
Why doesn't scanf with character set format read input in a for loop?
While trying to get newline character as input using scanf and display it as output as this discussion suggests, I tried the following code,
#include <stdio.h>
void main()
{
int n;
...
0
votes
1
answer
69
views
File scan with specific format and retrieve the data on following numbers in C
So I'm new to C programming and have some trouble figuring out how to file scan and input each different data into each variable. I already think file scan with fscanf, fgets or fread, but i don't ...