format-specifiers

Format specifier in scanf for bool datatype in C

为君一笑 提交于 2019-11-27 05:54:57
问题 I am using bool datatype in C std99 whose definitions are defined in <stdbool.h> . Now I want the user to give me input. What format specifier I must use in scanf to input the boolean value of 1 byte from the user and then manipulate it afterwards in my program. 回答1: There is none. Use a temp object as the size of _Bool is implementation dependent. #include <stdbool.h> #include <stdio.h> bool b; int temp; scanf("%d", &temp); b = temp; 来源: https://stackoverflow.com/questions/12920694/format

Correct printf format specifier for size_t: %zu or %Iu?

吃可爱长大的小学妹 提交于 2019-11-27 04:35:12
I want to print out the value of a size_t variable using printf in C++ using Microsoft Visual Studio 2010 (I want to use printf instead of << in this specific piece of code, so please no answers telling me I should use << instead). According to the post Platform independent size_t Format specifiers in c? the correct platform-independent way is to use %zu , but this does not seem to work in Visual Studio. The Visual Studio documentation at http://msdn.microsoft.com/en-us/library/vstudio/tcxf1dw6.aspx tells me that I must use %Iu (using uppercase i , not lowercase l ). Is Microsoft not following

Read no more than size of string with scanf()

烂漫一生 提交于 2019-11-27 04:30:01
Edit: for my class I have to use scanf . So recommending other ways of input is not the solution I am looking for (if there is one that involves scanf). If I am reading in user input for a small project (for example, a game). Lets say I ask would you like to play? This would accept a yes or no answer. So i write up some simple code like this: #include <stdio.h> int main(void) { char string[3]; //The max number of letters for "yes". printf("Would you like to play?"); scanf("%s", string); } So this code should simply ask them to input yes or no . I am setting the length of my char array to size

What does `scanf(“%*[^\\n]%*c”)` mean?

江枫思渺然 提交于 2019-11-27 03:42:22
I want to make a loop in C that, when the program asks for an integer and the user types a non-digit character, the program asks again for an integer. I just found the below code. but I don't understand what this means scanf("%*[^\n]%*c") . What does ^\n mean? What does the * before ^\n and c mean? /* This program calculate the mean score of an user 4 individual scores, and outputs the mean and a final grade Input: score1, score2,score2, score3 Output: Mean, FinalGrade */ #include <stdio.h> //#include <stdlib.h> int main(void){ int userScore = 0; //Stores the scores that the user inputs float

Why scanf(“%d”, […]) does not consume '\\n'? while scanf(“%c”) does?

有些话、适合烂在心里 提交于 2019-11-27 01:59:35
Here , I saw this statement in the accepted answer: Most of the conversion specifiers skip leading whitespace including newlines but %c does not. For me it is not clear the rationale under this different behaviors, I would have expected a uniform one (e.g. always skipping or never). I came into this kind of problem with a piece of C code like this: #include "stdio.h" int main(void){ char ch; int actualNum; printf("Insert a number: "); scanf("%d", &actualNum); // getchar(); printf("Insert a character: "); scanf("%c", &ch); return 0; } Swapping the two scanf s solves the problem, as well as the

How to printf “unsigned long” in C?

 ̄綄美尐妖づ 提交于 2019-11-26 23:27:47
I can never understand how to print unsigned long datatype in C. Suppose unsigned_foo is an unsigned long , then I try: printf("%lu\n", unsigned_foo) printf("%du\n", unsigned_foo) printf("%ud\n", unsigned_foo) printf("%ll\n", unsigned_foo) printf("%ld\n", unsigned_foo) printf("%dl\n", unsigned_foo) And all of them print some kind of -123123123 number instead of unsigned long that I have. %lu is the correct format for unsigned long . Sounds like there are other issues at play here, such as memory corruption or an uninitialized variable. Perhaps show us a larger picture? NealCaffery %lu for

Python - fixed exponent in scientific notation?

南笙酒味 提交于 2019-11-26 23:02:47
问题 Consider the following Python snippet: for ix in [0.02, 0.2, 2, 20, 200, 2000]: iss=str(ix) + "e9" isf=float(iss) print(iss + "\t=> " + ("%04.03e" % isf ) + " (" + str(isf) + ")") It generates the following output: 0.02e9 => 2.000e+07 (20000000.0) 0.2e9 => 2.000e+08 (200000000.0) 2e9 => 2.000e+09 (2000000000.0) 20e9 => 2.000e+10 (20000000000.0) 200e9 => 2.000e+11 (2e+11) 2000e9 => 2.000e+12 (2e+12) My question is - is it possible to "go back" somehow? That is: 2.000e+07 => 0.02e9 2.000e+08 =>

Why is printf with a single argument (without conversion specifiers) deprecated?

牧云@^-^@ 提交于 2019-11-26 19:06:48
问题 In a book that I'm reading, it's written that printf with a single argument (without conversion specifiers) is deprecated. It recommends to substitute printf("Hello World!"); with puts("Hello World!"); or printf("%s", "Hello World!"); Can someone tell me why printf("Hello World!"); is wrong? It is written in the book that it contains vulnerabilities. What are these vulnerabilities? 回答1: printf("Hello World!"); is IMHO not vulnerable but consider this: const char *str; ... printf(str); If str

Platform independent size_t Format specifiers in c?

江枫思渺然 提交于 2019-11-26 18:30:59
I want to print out a variable of type size_t in C but it appears that size_t is aliased to different variable types on different architectures. For example, on one machine (64-bit) the following code does not throw any warnings: size_t size = 1; printf("the size is %ld", size); but on my other machine (32-bit) the above code produces the following warning message: warning: format '%ld' expects type 'long int *', but argument 3 has type 'size_t *' I suspect this is due to the difference in pointer size, so that on my 64-bit machine size_t is aliased to a long int ( "%ld" ), whereas on my 32

What is the purpose of the h and hh modifiers for printf?

牧云@^-^@ 提交于 2019-11-26 17:24:48
Aside from %hn and %hhn (where the h or hh specifies the size of the pointed-to object), what is the point of the h and hh modifiers for printf format specifiers? Due to default promotions which are required by the standard to be applied for variadic functions, it is impossible to pass arguments of type char or short (or any signed/unsigned variants thereof) to printf . According to 7.19.6.1(7), the h modifier: Specifies that a following d, i, o, u, x, or X conversion specifier applies to a short int or unsigned short int argument (the argument will have been promoted according to the integer