You have printed the address of cont
since you used the operator &
. If you want to print the counter, have it printed and not other information. If you wanted to print a pointer you could use %p
. Formatting documentation.
#include <stdio.h>
int main() {
char var[50] = " Write Once Run Everywere ";
int cont = 0;
for (int i = 0; i < 50; i++) if (var[i] == ' ') cont++;
printf("Existem %d espaços em branco", cont);
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
I believe the mistake occurred because in scanf()
, usually requires the address. This is because you will change your value, so you pass the address of the variable to the function knowing where to put what was typed. The printf()
will only use the value, do not need to pick up your address.
Read more about the operator and pointers. C is rough, has to take care of everything.
All types can be accessed through your address. The pointer type is obviously already an address and usually the array is accessed by your address.
You have to read the function documentation to see what you need to pass to it.
I would like to be able to put both answers as certain thanks for answering! :)
– William.Andrade