softwaretore.blogg.se

Freemat erroneous format specification printf
Freemat erroneous format specification printf













  1. #FREEMAT ERRONEOUS FORMAT SPECIFICATION PRINTF CODE#
  2. #FREEMAT ERRONEOUS FORMAT SPECIFICATION PRINTF PLUS#

You basically wrote char c Īt first this might seem to be kinda, sorta, almost correct. So now, with all that background out of the way, we can look at your code. So the rule with %s is that scanf expects a pointer to the first of several characters, and printf expects a pointer to the first of several characters, too. So the exception is really for printf and %s: printf and %s does expect a pointer, and the reason printf and %s is designed to expect a pointer is that there's no way to not give it one: it has to accept a pointer, because for strings, that's what you always end up giving it.) The only reason scanf and %s doesn't need an & is that when you pass an array, you get a pointer to the array's first element automatically. Remember, the rule is really, scanf always needs pointers. (And if we think about it a bit more carefully, the exception is not so much that scanf and %s does not need the &. But with %s, you usually don't want the & for either printf or scanf. With %d and %c and just about every other format specifier, you usually need a & when you call scanf, and you usually don't want that & when you call printf. So %s is special with printf and scanf, because strings are special (because arrays are special). When you use %s with printf, it expects a pointer to the first of several characters which it should begin printing, until it finds the terminating '\0' character. This is, again, just as if you had written printf("%s\n", &s) You can print strings with %s too, of course, and it looks like this: printf("%s\n", s) (How does it know how big the array is? What if the user types a string that's too long to fit in the array? We'll get to those points in a moment.) When you use %s with scanf, it expects a pointer to the first of several characters, that is, a pointer to the beginning of an array of characters, where it should begin writing the string it reads. And in fact, when you called scanf("%s", s), it was just as if you had written scanf("%s", &s) You always need a pointer when calling scanf. "I thought you always needed an & when calling scanf!"

freemat erroneous format specification printf

What this means is that to read a string with scanf and %s, we can just call: scanf("%s", s)

#FREEMAT ERRONEOUS FORMAT SPECIFICATION PRINTF PLUS#

That gives us room for 9 characters, plus the terminating '\0'.īut arrays are special in C: Whenever you pass an array to a function, or whenever you do anything that would require the "value" of the array, what you get instead (what the compiler automatically generates for you) is a pointer to the array's first element. So if we wanted to declare a variable that could contain strings up to 9 characters long, we might write char s Also strings in C are always terminated by a special null character, '\0', that marks the end of the string. But printf just needs the value, so we pass plain c. scanf needs a pointer, so that it can fill in the value, so we pass &c. We can read one character with scanf: char c Īnd we can print it with printf: printf("%c\n", c) Īgain, the pattern is exactly the same. So now we've learned that for integers with %d, printf wants an int and scanf wants a pointer-to- int. It wouldn't work, because printf expects an int, and here we're wrongly handing it a pointer-to- int. If we were to call printf("%d\n", &i) /* WRONG */ We do want to pass i's value to printf so that it can print it out. When we call printf, on the other hand, the regular way of passing arguments works just fine. That's what the & does - it generates a pointer to i. For that to work, we instead pass scanf a pointer to the variable i where we want it to store the just-read integer. In other words, we want scanf to, in effect, pass the new value of i back to us. But we don't want to pass the (old) value of i to scanf, we want scanf to read a new value, and store it into i. We would be passing the value of i to scanf. Why does one use an & and one does not? Well, C uses what's called "pass by value". You can read an integer like this: int i Īnd you can print it back out like this: printf("%d\n", i)

freemat erroneous format specification printf

The format specifier %d is for values of type int. Let's review a few things about printf and scanf.

#FREEMAT ERRONEOUS FORMAT SPECIFICATION PRINTF CODE#

What you have here is a classic example of code that seems to work, but for the wrong reasons.















Freemat erroneous format specification printf