format-specifiers

Format specifier in scanf for bool datatype in C

徘徊边缘 提交于 2019-11-28 10:51:51
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. 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-specifier-in-scanf-for-bool-datatype-in-c

What is the purpose of using the [^ notation in scanf?

一笑奈何 提交于 2019-11-28 03:50:17
问题 I have run into some code and was wondering what the original developer was up to. Below is a simplified program using this pattern: #include <stdio.h> int main() { char title[80] = "mytitle"; char title2[80] = "mayataiatale"; char mystring[80]; /* hugh ? */ sscanf(title,"%[^a]",mystring); printf("%s\n",mystring); /* Output is "mytitle" */ /* hugh ? */ sscanf(title2,"%[^a]",mystring); /* Output is "m" */ printf("%s\n",mystring); return 0; } The man page for scanf has relevant information, but

What does the %*s format specifier mean?

亡梦爱人 提交于 2019-11-28 03:48:36
In some code that I have to maintain, I have seen a format specifier %*s . Can anybody tell me what this is and why it is used? An example of its usage is like: fprintf(outFile, "\n%*s", indent, ""); akappa It's used to specify, in a dynamic way, what the width of the field is : The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted. so "indent" specifies how much space to allocate for the string that follows it in the parameter list. So, printf("%*s", 5, ""); is the same as printf("%5s", ""); It's a nice way

Usage of void pointers across different platforms

微笑、不失礼 提交于 2019-11-28 02:47:12
问题 I have heard that pointers should first be cast to void to ensure consistency of values across different platforms and should use %p format specifier. Why is it and what exactly are the problems? int x=100; int *pi=&x; printf("value of pi is: %p",(void*)pi); 回答1: printf is a variadic function and must be passed arguments of the right types. The standard says %p takes void * . Implicit cast doesn't take place for variadic functions. Quoting from N1570 7.21.6.1 The fprintf function p : The

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

只愿长相守 提交于 2019-11-28 02:16:29
This question already has an answer here: scanf() leaves the new line char in the buffer 4 answers When I don't include whitespace 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 whitespace between %d and %c specification ? #include<stdio.h> main() { char c; int i; printf("Enter an Integer and a character : \n"); scanf("%d %c",&i,&c); printf("Integer = %d

What precisely does the %g printf specifier mean?

纵然是瞬间 提交于 2019-11-28 00:09:56
The %g specifier doesn't seem to behave in the way that most sources document it as behaving. According to most sources I've found, across multiple languages that use printf specifiers, the %g specifier is supposed to be equivalent to either %f or %e - whichever would produce shorter output for the provided value. For instance, at the time of writing this question, cplusplus.com says that the g specifier means: Use the shortest representation: %e or %f And the PHP manual says it means: g - shorter of %e and %f . And here's a Stack Overflow answer that claims that %g uses the shortest

Python - fixed exponent in scientific notation?

ぐ巨炮叔叔 提交于 2019-11-27 22:16:24
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 => 0.2e9 2.000e+09 => 2e9 2.000e+10 => 20e9 2.000e+11 => 200e9 2.000e+12 => 2000e9 ... I'd specify I want

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

主宰稳场 提交于 2019-11-27 17:39:13
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? Jabberwocky printf("Hello World!"); is IMHO not vulnerable but consider this: const char *str; ... printf(str); If str happens to point to a string containing %s format specifiers, your program will exhibit

sprintf for unsigned _int64

醉酒当歌 提交于 2019-11-27 09:13:39
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); } Output: The string is OD DB File Size = 100 bytes XML file size = 0 bytes DevSolar I don't know

printf not yielding input values

二次信任 提交于 2019-11-27 08:54:01
问题 I have the following block of code. The final line is meant to yield the values for X , Y and P values that are input by the user. However it only returns (0,0,0) instead of the values given by user. What am I missing? printf("What is the robot's initial X position? (cm)\n"); scanf("%f",&X); printf("What is the robot's initial Y position? (cm)\n"); scanf("%f",&Y); printf("What is the robot's initial angular position? (degrees)\n"); scanf("%f",&P); printf("The initial position is (%d, %d, %d)