format-string

How can I use a percent % in FormatString without it multiplying by 100?

牧云@^-^@ 提交于 2019-11-28 18:34:37
I would like to format an integer as a percent without it multiplying by 100 as shown here . Because my source is an int, dividing it first by 100 is not a valid option. Is this possible? [DisplayFormat(DataFormatString = "{0:#%}")] You can escape the % character: [DisplayFormat(DataFormatString = @"{0:#\%}")] Note that there are two ways to use \ as an escape character: if you prefix a string literal with the verbatim symbol ( @ ), then \ characters are included in the string as-is, which means that as part of a format string a single \ will function as an escape character. Without the @

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

How can a Format-String vulnerability be exploited?

删除回忆录丶 提交于 2019-11-26 14:56:21
I was reading about vulnerabilities in code and came across this Format-String Vulnerability . Wikipedia says: Format string bugs most commonly appear when a programmer wishes to print a string containing user supplied data. The programmer may mistakenly write printf(buffer) instead of printf("%s", buffer). The first version interprets buffer as a format string, and parses any formatting instructions it may contain. The second version simply prints a string to the screen, as the programmer intended. I got the problem with printf(buffer) version, but I still didn't get how this vulnerability

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

How to escape the % (percent) sign in C's printf?

狂风中的少年 提交于 2019-11-26 00:14:24
问题 How do you escape the % sign when using printf in C? printf(\"hello\\%\"); /* not like this */ 回答1: You can escape it by posting a double '%' like this: %% Using your example: printf("hello%%"); Escaping '%' sign is only for printf. If you do: char a[5]; strcpy(a, "%%"); printf("This is a's value: %s\n", a); It will print: This is a's value: %% 回答2: As others have said, %% will escape the %. Note, however, that you should never do this: char c[100]; char *c2; ... printf(c); /* OR */ printf(c2