format-specifiers

How should I print types like off_t and size_t?

我们两清 提交于 2019-11-26 14:56:45
I'm trying to print types like off_t and size_t . What is the correct placeholder for printf() that is portable ? Or is there a completely different way to print those variables? You can use z for size_t and t for ptrdiff_t like in printf("%zu %td", size, ptrdiff); But my manpage says some older library used a different character than z and discourages use of it. Nevertheless, it's standardized (by the C99 standard). For those intmax_t and int8_t of stdint.h and so on, there are macros you can use, like another answer said: printf("value: %" PRId32, some_int32_t); printf("value: %" PRIu16,

sprintf for unsigned _int64

可紊 提交于 2019-11-26 14:27:43
问题 I am having following code. output of second %d in sprintf is always shown as zero. I think i am specifying wrong specifiers. Can any one help me in getting write string with right values. And this has to achieved in posix standard. Thanks for inputs void main() { unsigned _int64 dbFileSize = 99; unsigned _int64 fileSize = 100; char buf[128]; memset(buf, 0x00, 128); sprintf(buf, "\nOD DB File Size = %d bytes \t XML file size = %d bytes", fileSize, dbFileSize); printf("The string is %s ", buf)

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

烈酒焚心 提交于 2019-11-26 12:45:19
问题 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

How do you format an unsigned long long int using printf?

試著忘記壹切 提交于 2019-11-26 12:40:25
#include <stdio.h> int main() { unsigned long long int num = 285212672; //FYI: fits in 29 bits int normalInt = 5; printf("My number is %d bytes wide and its value is %ul. A normal number is %d.\n", sizeof(num), num, normalInt); return 0; } Output: My number is 8 bytes wide and its value is 285212672l. A normal number is 0. I assume this unexpected result is from printing the unsigned long long int . How do you printf() an unsigned long long int ? John Downey Use the ll (el-el) long-long modifier with the u (unsigned) conversion. (Works in windows, GNU). printf("%llu", 285212672); You may want

Whitespace before %c specification in the format specifier of scanf function in C [duplicate]

巧了我就是萌 提交于 2019-11-26 11:39:31
问题 This question already has answers here : scanf() leaves the new line char in the buffer (4 answers) Closed last year . When I don\'t include white space between %d and %c specification in the format string of scanf() function in the following program, and give input during run-time as \"4 h\", then the output is \"Integer = 4 and Character= . How exactly variable \"c\" takes the input in this case and what difference does it make if i include a white space between %d and %c specification ?

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

China☆狼群 提交于 2019-11-26 10:36:31
问题 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

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

此生再无相见时 提交于 2019-11-26 09:52:58
问题 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(\

Read no more than size of string with scanf()

风流意气都作罢 提交于 2019-11-26 09:41:32
问题 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);

How to printf “unsigned long” in C?

柔情痞子 提交于 2019-11-26 08:40:04
问题 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. 回答1: %lu is the correct format for unsigned long . Sounds like there are other issues at

Platform independent size_t Format specifiers in c?

前提是你 提交于 2019-11-26 06:27:08
问题 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