scanf

scanf causing infinite loop in C

柔情痞子 提交于 2021-02-05 08:52:49
问题 I am relatively new to C, but I have been programming for a few years now. I am writing a program for a college class, and I am confused why the scanf function below is not called, resulting in an infinite loop. I have tried having my scanf outside the function, calling it twice, once from within, once from out, and a few other ways. I read online that the fflush might help, but it hasn't Any suggestions? // store starting variables int players; // print title printf("*-----------------------

scanf a big hexadecimal value

本小妞迷上赌 提交于 2021-02-05 07:53:14
问题 I have a issue trying to use scanf to get a big hexadecimal num (12 chars) from the user. it seems to only get the last 8 chars, eg - ABFFFFFFFF will become 0000FFFFFFFF. this is my code - unsigned long long address; scanf("%x",&address); printf("Address: %#014x", address); for this input: "ABFFFFFFFF" the output would be: Address: 0x0000ffffffff i have tried playing a bit with the scanf format, but to no avail. 回答1: You must use "%llx" for both scanf format and printf. See the manual page

scanf a big hexadecimal value

痴心易碎 提交于 2021-02-05 07:52:12
问题 I have a issue trying to use scanf to get a big hexadecimal num (12 chars) from the user. it seems to only get the last 8 chars, eg - ABFFFFFFFF will become 0000FFFFFFFF. this is my code - unsigned long long address; scanf("%x",&address); printf("Address: %#014x", address); for this input: "ABFFFFFFFF" the output would be: Address: 0x0000ffffffff i have tried playing a bit with the scanf format, but to no avail. 回答1: You must use "%llx" for both scanf format and printf. See the manual page

program wont read at second scanf()

巧了我就是萌 提交于 2021-02-05 06:21:45
问题 I dont understand where I went wrong. It does not read at second scanf() just skips on to the next line. #include <stdio.h> #define PI 3.14 int main() { int y='y', choice,radius; char read_y; float area, circum; do_again: printf("Enter the radius for the circle to calculate area and circumfrence \n"); scanf("%d",&radius); area = (float)radius*(float)radius*PI; circum = 2*(float)radius*PI; printf("The radius of the circle is %d for which the area is %8.4f and circumfrence is %8.4f \n", radius,

How to scanf a float followed immediately by the letter 'e' in c?

China☆狼群 提交于 2021-02-04 22:08:38
问题 I'm trying to use fscanf to read in data, and part of the input is a float followed by the letter 'e' , for example, 41.72elapsed . When writing the strng for fscanf , I attempted to use "%felapsed" , but this doesn't work, as %fe is its own format specifier. How would I read this in using fscanf ? edit: Here is the code: #include <stdio.h> #include <string.h> #include <stdlib.h> #define CHAR_MAX 1024 int main(int argc, char **argv) { FILE *file_in = fopen(argv[1], "r+"); char out_name[CHAR

How to scanf a float followed immediately by the letter 'e' in c?

爱⌒轻易说出口 提交于 2021-02-04 22:06:36
问题 I'm trying to use fscanf to read in data, and part of the input is a float followed by the letter 'e' , for example, 41.72elapsed . When writing the strng for fscanf , I attempted to use "%felapsed" , but this doesn't work, as %fe is its own format specifier. How would I read this in using fscanf ? edit: Here is the code: #include <stdio.h> #include <string.h> #include <stdlib.h> #define CHAR_MAX 1024 int main(int argc, char **argv) { FILE *file_in = fopen(argv[1], "r+"); char out_name[CHAR

scanf skipped after loop in C [duplicate]

╄→尐↘猪︶ㄣ 提交于 2021-02-04 21:21:08
问题 This question already has answers here : fgets doesn't work after scanf [duplicate] (9 answers) Closed 11 months ago . #define len 100 char sourceString[len]; char command; int main(void) { while (1) { printf("\nEnter source: "); fgets(sourceString, len, stdin); // this gets skipped on second loop. printf("\nEnter command: "); scanf(" %c", command) switch (command) { case 'A': printf("%s", sourceString); break; case 'B': printf("filler"); break; default: break; } } return 0; } Whether im

How do you use scanf to get an int in C?

ぃ、小莉子 提交于 2021-02-04 20:38:22
问题 I'm trying to learn the benefits and shortcomings of different ways to get input from the console. I'm confused with scanf . Why do I need to use use &favNumber instead of favNumber ? I understand that &favNumber is the address location of favNumber , but why is it done this way? I feel like there's a type mismatch here where favNumber is an int and I'm telling scanf that it's a pointer to an int. I thought I wrapped my head around pointers but this is confusing me a bit. Any help would be

How do you use scanf to get an int in C?

点点圈 提交于 2021-02-04 20:38:07
问题 I'm trying to learn the benefits and shortcomings of different ways to get input from the console. I'm confused with scanf . Why do I need to use use &favNumber instead of favNumber ? I understand that &favNumber is the address location of favNumber , but why is it done this way? I feel like there's a type mismatch here where favNumber is an int and I'm telling scanf that it's a pointer to an int. I thought I wrapped my head around pointers but this is confusing me a bit. Any help would be

NULL arg allowed to sscanf?

依然范特西╮ 提交于 2021-02-04 17:41:11
问题 Is a NULL pointer allowed as the string to store result in in a call to sscanf ? I don't find anything about it in any documentation but it seems to be working fine. Same thing with scanf . Example: int main(int arc, char* argv[]) { char* s = NULL; sscanf("Privjet mir!", "%s", s); printf("s: %s\n", s); return 0; } Output: s: (null) 回答1: No: Matches a sequence of non-white-space characters; the next pointer must be a pointer to character array that is long enough to hold the input sequence and