7,503 questions
-3
votes
0
answers
150
views
Why excatly are std::cin/std::cout slower than scanf/printf? [duplicate]
I understand that std::cin/std::cout are generally slower than scanf/printf, and that's primarily due to synchronization with C’s standard streams. However, all explanations I saw just say that and ...
1
vote
7
answers
307
views
How to handle two data formats when reading a file in C
I have a data file containing a number of 4 bit values in hexadecimal with a leading zero separated by commas..
00,00,00,00,00,00,00,00,00,00,00,00,00,00,
00,04,00,00,00,00,00,00,00,00,00,00,00,00,
0a,...
4
votes
4
answers
225
views
The scanf function in C language uses a width specifier for char
There is a code where I enter ABCDEFGH and press enter, and the final result is HGF. When I use debug mode to observe variables. When executing the first sentence input, x='A'. After the next step, x='...
5
votes
3
answers
290
views
Finding the bigger number using bitwise operation in C
I am trying to write a computer programme that will take two numbers from the user as inputs and will print the bigger number using bitwise operation. I want it to end the programme and return 1 if ...
5
votes
2
answers
446
views
How do I properly use scanf and what are its limits?
I am currently trying to make a simple little program (BMI index calculator), since I have just started learning C. Currently using Eclipse.
Here is my code:
#include <stdio.h>
int weight, ...
2
votes
1
answer
84
views
String format specifier works but prevents reading of others
I'm making a program that reads multiple structs in and out of different files. For one of them, I have a format for reading from the file, and one for writing to it.
#include <stdio.h>
int ...
2
votes
1
answer
146
views
Why is my string not printing to PowerShell in C on second function call?
I'm a beginner programmer learning C as a hobby. I just learned about accepting user input using scanf() and thought it would be a fun learning experience to make a small "game" that runs in ...
1
vote
2
answers
111
views
Writing a Binomial Coefficient calculator in C : Problem with checking scanf failure
I want to write a computer programme that will do the following things :
Ask for two integer inputs n and k from the user using scanf.
If one of the scanfs don't return 1, it will print "Failed&...
1
vote
1
answer
74
views
Memory management issues for a linear regression program in C
I am planning to make a Linear Regression model using C. It takes a set of m points as input from stdin using scanf. The points are defined as a struct:
typedef struct{
double x;
double y;
} ...
3
votes
2
answers
121
views
Why does scanf() read only after specified text?
I was doing a CTF reversing challenge when I came across this C code in Ghidra:
int main(void)
{
int iVar1;
char input[32];
fwrite("Password: ",1,10,stdout);
__isoc99_scanf("...
1
vote
1
answer
144
views
Why does the program suddenly terminate after printf? [duplicate]
I am trying out a program to generate a linked list, and to do it, I am asking for a Y/N character input from the user to generate a new node. However, the program just abruptly terminates after the ...
1
vote
2
answers
120
views
getting segmentation fault when attempting to read text file contraining 1 byte of hex value per line in c
was trying to write some c code to read a text file which contains some hex value in the follow format
AA
BB
CC
DD
Containing 1 Byte of hex values per line in the file.
the following is the code I ...
1
vote
1
answer
187
views
While loop interates a number of times equal to the amount of bytes in input
I am a novice to programming, and I'm doing it for fun. I encountered a strange bug I do not understand while trying to parse simple user input.
My problem is that when an invalid character is read (...
5
votes
4
answers
185
views
What is an input item in scanf?
I'm learning C and trying to understand how scanf works. I can't understand some terms: the term "input item", "initial subsequence", "matching sequence".
I am reading ...
2
votes
3
answers
131
views
Usage details of fscanf and similar functions?
I'm using fscanf to parse a string, and I've been using it thusly:
fscanf(file_in, "%i: %s%c%s", ¤t_id) == 4
Ignore the weirdness of the formatting(I know I could probably get away ...
0
votes
1
answer
149
views
My scanf function isn't working in vscode
#include <stdio.h>
int main(void)
{
int total=0;
int i, num;
printf("Sum of 0 to num, enter num: ");
scanf("%d", &num);
for(i=0; i<=num; i++)
...
3
votes
3
answers
131
views
fscanf not reading second line from file
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100 // Maximum size of the arrays
int main() {
FILE *file;
int array1[MAX_SIZE], array2[MAX_SIZE];
int i = 0, j = 0;
...
0
votes
2
answers
122
views
can you give me a test case where these two c programs will produce different outputs?
program 1:
#include <stdio.h>
#define MAXSTR 100
int main()
{
char ch;
char str[MAXSTR];
char line[MAXSTR];
scanf("%c", &ch); // Just read character
...
0
votes
1
answer
74
views
parsing a file using fscanf [closed]
I am trying to parse this input file temperatures.txt, but currently I am having trouble with formatting the input correctly. I have tried many renditions so now I am looking for help.
The issue I am ...
3
votes
1
answer
94
views
Behavior of scanset
There is a difference in the scansets %1[x+-/] and %1[x/+-]. The first will also accept , and . but the second rejects those characters.
#include <stdio.h>
#include <stdlib.h>
char ...
0
votes
2
answers
107
views
Why does my scanf() get ignored completely
I want to make a function that reads a value from the user with spaces, for example ("Noble Gas").I found a way that works if i run it alone but not in my function.
The code lets me input ...
0
votes
1
answer
122
views
Is it important to add a white space before all format specifiers in c programming? [duplicate]
Is it important to add a white space before all format specifiers with scanf in c programming?
scanf(" %d",&variable_name);
scanf(" %c",&variable_name);
And will it be ...
0
votes
3
answers
133
views
Why is the '&' operator needed for an int variable but not for an array in C? [duplicate]
#include <stdio.h>
#include <stdlib.h>
int main(){
char name[100];
int age;
printf("Enter you name:\n");
scanf("%s", name);
printf("Enter you ...
1
vote
1
answer
112
views
taking char. by char. input to make a string in c
so i did try it with using a for loop but every single time my output skips the value of index 0 and i just dont understand why....
here is my code :
// take char by char input and print it as string
...
0
votes
2
answers
272
views
Getting warning of "format specifies type 'int' but the argument has type 'int *' [-Wformat]" when Im pretty sure my code is correct?
So I am new to coding and am teaching myself C. Today I was trying to write a simple program using scanf() but I'm not getting the output I expect. Whatever number I type it seems like scanf() either ...
1
vote
2
answers
130
views
In this code, why is scanf() not setting the input to the variable properly?
I am trying to post this to the C board. If I am in the wrong place, or this is not an appropriate question please let me know so can delete or move this. Thank you very much.
I am learning C as my ...
1
vote
0
answers
47
views
How to Handle Empty Fields When Parsing a CSV File in C [duplicate]
I am working on a program in C to parse data from a CSV file. The structure of my CSV file includes fields that may be empty, and I need to handle these cases properly. Here's an example of a CSV row:
...
2
votes
1
answer
218
views
Negated scanset technique in C
I am trying to use a negated scanset technique with scanf (I am not allowed to use anything else so no getchar()).
But I have a small problem with it.
When I use scanf(“%*[ \n]%*c”).
If I enter an ...
0
votes
1
answer
91
views
behaviour of scanf vs fgets [duplicate]
Please see the following piece of code in C
printf ("\nEnter the lines (terminate each line with ENTER...\n\n");
for (i = 0; i < lines; i++)
scanf (" %[^\n]", s[i]);
I have ...
-1
votes
4
answers
172
views
The issue of %5c causing output exceptions in C language
#include <stdio.h>
int main() {
int y = 0, w = 0;
scanf("%d %5c", &y, &w);
printf("%d,%c\n", y, w);
return 0;
}
When i input 2000 12345, the output ...
1
vote
2
answers
119
views
How to fscanf `1/2` as `0.5` from a .dad file in C
I have a .dad file with the content
1/2
I wish to read from the file and save the value 0.5 into a double using fscanf, but the method reads it as 1.000.
To be completely clear, here is the code and ...
1
vote
0
answers
131
views
How to properly execute in parallel several fscanf?
I'm trying to do parallel execution of several fscanf to read file faster
I have 10 nums in a file
10-nums.txt
478
205
256
175
196
52
594
333
72
777
and have a simple code
main.c
#include <stdio.h&...
2
votes
3
answers
106
views
How do I make my program not break when symbol is non-numeric?
#include <stdio.h>
#include <ctype.h>
void task(const char *file_name)
{
FILE *file = fopen("1.txt", "r");
if (file == NULL){
printf("Error ...
0
votes
3
answers
123
views
Is it possible for a single scanf command to take either one or two inputs?
printf("Commands:\n");
printf(" add <word>: adds a new word to dictionary\n");
printf(" lookup <word>: searches for a word\n");
...
-1
votes
1
answer
95
views
why is this program skipping the the scanf in this program [closed]
anytime i run this function in my program
void treeInput(){
char temp;
int x, tempRow, tempColm;
while(x=0){
scanf(" %c ", &temp);
if(temp == 'Q'){
...
3
votes
4
answers
188
views
Problems with scanf input stream
I am trying to do a postfix evaluation using stack. The output is supposed to be in a format 2,3,4,+,-,# with # signifying the end of expression. The problem is negative numbers are allowed i.e 2,3,-...
0
votes
1
answer
113
views
Why can't you put scanf() on the same line as the variable? [closed]
For example:
int number = scanf("%d", &number);
0
votes
1
answer
105
views
Am i using the scanf function properly
I am writing code using Scanf and i want it to take in three different inputs at once perform some computations using those inputs and give me an output but it is not working as i expected .
Firstly ...
1
vote
3
answers
202
views
Use scanf() to read \n in a loop?
I must use scanf() to read each 2-digit hexadecimal value. Each 2-digit
hexadecimal number will be separated by a single space and a ‘\n’ character (rather
than a space after the number) will indicate ...
2
votes
1
answer
125
views
Parsing with a space (sscanf or strtok)
For some context, my game runs in the terminal and is like a command line type game. At the moment to process commands is does a strcmp() for every single command which isn't really pretty nor ...
0
votes
0
answers
32
views
Why can't I get an output on the printf statement in C language right after the scan statement, the run command skips it completely [duplicate]
I am new to C language coding, I was trying to learn the various inputs that I can take from the user and display the same but I keep getting a blank in the place of the printf statement that takes ...
0
votes
2
answers
119
views
How to prevent buffer overflow when we recursively ask for input if input is not valid
Have to take a input string of size 5. Also have to check for validity in some unrelated sense. If the input is not valid user is prompted to input again.
Using the scanf("%4s", input);
But ...
0
votes
1
answer
105
views
I cannot get the scanf function to work while using VScode
I made a simple program for a class using c and printf would work just fine; however, when I added the Scanf function, nothing would work not even the printf lines. My teacher used the same code on VS ...
0
votes
1
answer
101
views
It doesn't register the value that I give it
It just gives me 2.5 as an answer whenever I input the num and I don't know why, so any help would be greatly appreciated!
#include <stdio.h>
double equ(double num);
int main()
{
double ...
0
votes
1
answer
72
views
Stopping a while loop from user's input [duplicate]
I want to calculate average of the numbers entered by the user, where the user can enter as many numbers as he/she wants , i.e., the loop will stop only when the user wants.
To stop the loop from user'...
1
vote
2
answers
138
views
In s/fscanf(), how to ensure that the entire format string was matched against entire input?
The *scanf() family of functions return the number of "input items" (that is, conversion specifications) successfully matched and assigned:
RETURN VALUE
On success, these functions return ...
0
votes
1
answer
124
views
are printf and scanf used safely here in this code?
#include <stdio.h>
int main() {
char a[12];
int b = scanf("%11s", a);
if (b != 1) { return 1; };
if (printf("%s", a) > 11) {
return 1;
...
2
votes
1
answer
83
views
using scanf to read strings in nasm
im working on a script which will read a string from the user and print it back, i wanted the script to read the input using scanf and print it back with printf.
So far i tried following those forums:
...
0
votes
3
answers
107
views
Trying to read txt file line by line in C Language
Basically my input file is in the format:
I 15 3 15 10 10 20
S -5 3 15 82
I -20 80 -4 10
S 4 -20 8
The number of ints in a row can vary, but there is always one char at the beginning of each ...
3
votes
2
answers
217
views
Why doesn't scanf("%d %d") complain about decimal input?
Why when I run this code:
#include <stdio.h>
int main () {
int a, b;
if (scanf("%d %d", &a, &b) == 2)
printf("%d %d", a, b);
else
printf(...