<< < 1 2 3 4 5 6 7 8 9 10 11 > >>   Sort: Rank

How can I print a character in a printf format string?
How can I print a '%' character in a printf format string? I tried \%, but it didn't work. Simply double the percent sign: %% . The reason it's tricky to print % signs with printf is that % is essentially printf's escape character. Whenever printf sees a %, it expects it to be followed by a characte...
2015-11-09, 1298👍, 0💬

Why doesnt long int work?
Why doesn't long int n = 123456; printf("%d\n", n); work? Whenever you print long ints you must use the l (lower case letter ``ell'') modifier in the printf format (e.g. %ld). printf can't know the types of the arguments which you've passed to it, so you must let it know by using the correct format ...
2015-11-09, 1169👍, 0💬

I thought that ANSI function prototypes were supposed to guard against argument type mismatches.
I thought that ANSI function prototypes were supposed to guard against argument type mismatches. When a function accepts a variable number of arguments, its prototype does not (and cannot) provide any information about the number and types of those variable arguments. Therefore, the usual protection...
2015-11-06, 1209👍, 0💬

How can printf use f for type double, if scanf requires lf?
Someone told me it was wrong to use %lf with printf. How can printf use %f for type double, if scanf requires %lf? It's true that printf's %f specifier works with both float and double arguments Due to the ``default argument promotions'' (which apply in variable-length argument lists values of type ...
2015-11-06, 1237👍, 0💬

What printf format should I use for a typedef like size_t when I don't know whether it is long or some other type?
What printf format should I use for a typedef like size_t when I don't know whether it is long or some other type? Use a cast to convert the value to a known, conservatively-sized type, then use the printf format matching that type. For example, to print the size of a type, you might use printf("%lu...
2015-11-04, 1159👍, 0💬

How can I print numbers with commas separating the thousands? What about currency formatted numbers?
How can I print numbers with commas separating the thousands? What about currency formatted numbers? The functions in &lt;locale.h> begin to provide some support for these operations, but there is no standard C function for doing either task. (In Standard C, the only thing printf does in respons...
2015-11-02, 1736👍, 0💬

Why doesnt the call scanf work?
Why doesn't the call scanf("%d", i) work? The arguments you pass to scanf must always be pointers: for each value converted, scanf ``returns'' it by filling in one of the locations you've passed pointers to. To fix the fragment above, change it to scanf("%d", &i) .
2015-11-02, 1189👍, 0💬

Why does the call char scanf work?
Why does the call char s[30]; scanf("%s", s); work? I thought you always needed an & on each variable passed to scanf. You always need a pointer; you don't necessarily need an explicit &amp;. When you pass an array to scanf, you do not need the &, because arrays are always passed to func...
2015-10-30, 1095👍, 0💬

Why doesnt this code work?
Why doesn't this code: double d; scanf("%f", &amp;d); work? Unlike printf, scanf uses %lf for values of type double, and %f for float.%f tells scanf to expect a pointer-to-float, not the pointer-to-double you gave it. Either use %lf, or declare the receiving variable as a float.
2015-10-30, 1108👍, 0💬

Why doesnt that code work?
Why doesn't the code short int s; scanf("%d", &s); work? When converting %d, scanf expects a pointer to an int. To convert to a short int, use %hd .
2015-10-28, 1042👍, 0💬

How can I specify a variable width in a scanf format string?
How can I specify a variable width in a scanf format string? You can't; an asterisk in a scanf format string means to suppress assignment. You may be able to use ANSI stringizing and string concatenation to construct a constant format specifier based on a preprocessor macro containing the desired wi...
2015-10-26, 1179👍, 0💬

How can I read data from data files with particular formats?
How can I read data from data files with particular formats? How can I read ten floats without having to use a jawbreaker scanf format like "%f %f %f %f %f %f %f %f %f %f"? How can I read an arbitrary number of fields from a line into an array? In general, there are three main ways of parsing data l...
2015-10-26, 985👍, 0💬

When I read numbers from the keyboard with scanf ...
When I read numbers from the keyboard with scanf and a "%d\n" format, like this: int n; scanf("%d\n", &amp;n); printf("you typed %d\n", n); it seems to hang until I type one extra line of input. Perhaps surprisingly, \n in a scanf format string does not mean to expect a newline, but rather to re...
2015-10-23, 1049👍, 0💬

I am reading a number with scanf and d, and then a string with gets
I'm reading a number with scanf and %d, and then a string with gets(): int n; char str[80]; printf("enter a number: "); scanf("%d", &amp;n); printf("enter a string: "); gets(str); printf("you typed %d and "%s"\n", n, str); but the compiler seems to be skipping the call to gets()! If, in response...
2015-10-23, 992👍, 0💬

Why does everyone say not to use scanf? What should I use instead?
Why does everyone say not to use scanf? What should I use instead? scanf has a number of problems, its %s format has the same problem that gets() has --it's hard to guarantee that the receiving buffer won't overflow. More generally, scanf is designed for relatively structured, formatted input (its n...
2015-10-21, 1159👍, 0💬

I am using scanf c to read a Y/N response
I'm using scanf %c to read a Y/N response, but later input gets skipped. You wanted scanf %c to read a single character, and it tried to, but when you tried to type that single character at it, before the rest of the input system would accept it, you had to hit the RETURN key, too. scanf read only t...
2015-10-21, 1056👍, 0💬

I figured I could use scanf more safely if I checked its ...
I figured I could use scanf more safely if I checked its return value to make sure that the user typed the numeric values I expect: int n; while(1) { printf("enter a number: "); if(scanf("%d", &amp;n) == 1) break; printf("try again: "); } printf("you typed %d\n", n); but sometimes it seems to go...
2015-10-19, 1293👍, 0💬

How can I tell how much destination buffer space ...
How can I tell how much destination buffer space I'll need for an arbitrary sprintf call? How can I avoid overflowing the destination buffer with sprintf? When the format string being used with sprintf is known and relatively simple, you can sometimes predict a buffer size in an ad-hoc way. If the f...
2015-10-19, 1149👍, 0💬

What is the deal on sprintfs return value?
What's the deal on sprintf's return value? Is it an int or a char *? The Standard says that it returns an int (the number of characters written, just like printf and fprintf). Once upon a time, in some C libraries, sprintf returned the char * value of its first argument, pointing to the completed re...
2015-10-16, 958👍, 0💬

Why does everyone say not to use gets?
Why does everyone say not to use gets? Unlike fgets(), gets() cannot be told the size of the buffer it's to read into, so it cannot be prevented from overflowing that buffer if an input line is longer than expected--and Murphy's Law says that, sooner or later, a larger-than-expected input line will ...
2015-10-16, 1047👍, 0💬

I thought I would check errno after a long string of printf calls ...
I thought I'd check errno after a long string of printf calls, to see if any of them had failed: errno = 0; printf("This\n"); printf("is\n"); printf("a\n"); printf("test.\n"); if(errno != 0) fprintf(stderr, "printf failed: %s\n", strerror(errno)); Why is it printing something strange like ``printf f...
2015-10-14, 1024👍, 0💬

What is the difference between fgetpos/fsetpos and ftell/fseek? What are fgetpos and fsetpos good for?
What is the difference between fgetpos/fsetpos and ftell/fseek? What are fgetpos and fsetpos good for? ftell and fseek use type long int to represent offsets (positions) in a file, and may therefore be limited to offsets which can be represented in a long int. (Type long int is not guaranteed to hol...
2015-10-14, 1877👍, 0💬

How can I flush pending input so that a user ...
How can I flush pending input so that a user's typeahead isn't read at the next prompt? Will fflush(stdin) work? fflush is defined only for output streams. Since its definition of ``flush'' is to complete the writing of buffered characters (not to discard them), discarding unread input would not be ...
2015-10-12, 1000👍, 0💬

If fflush wont work, what can I use to flush input?
If fflush wont work, what can I use to flush input? It depends on what you're trying to do. If you're trying to get rid of an unread newline or other unexpected input after calling scanf you really need to rewrite or replace the call to scanf. Alternatively, you can consume the rest of a partially-r...
2015-10-12, 1040👍, 0💬

<< < 1 2 3 4 5 6 7 8 9 10 11 > >>   Sort: Rank