Yes.
Printable View
Don't worry, I might be new to C but I'm well aware of the dangers of bad habits and buffer overflows. Thank you for the heads up. :)
Basically data types are just a way to impose boundaries on continous, but these boundaries aren't absolute.Quote:
It will actually work just fine to print with printf("%s", s), because gets() and printf() will both interpret the string as a char array. The fact that you declared it as an int array will only come into effect if you try to step throgh it, then you will get 'abcd' (0x64636261) as the integer value [1] of the first integer - because gets() have stuffed four bytes into the first four bytes of your array.
I probably sound silly restating what you guys are saying, but it's helping me absorb the information.
Types are a way for the language to interpret the raw data into something more suitable for humans (huwoman?).
And indexes in arrays are just for the compiler to calculate an offset in the big memory block to read/write to/from.
Arrays are just a way to create big blocks of memory to use.
Thank you for all the help guys and girls. I don't think It'd even be possible for me to learn C without these forums.
Splitting hairs, but actually printf RECEIVES that char as a integer, because the compiler has magically converted it - this is because we don't want the compiler to have to understand "%c" inside the argument for printf means a char, and "%d" means an integer - what happens if you write this:
And we could easily make an ever more complex situation where the actual string CAN NOT POSSIBLY be known by the compiler. So there has to be rules on how the arguments are passed to printf, so that printf itself can figure out what's what of its arguments.Code:char *strs[3] = { "%d", "%c", "%x" };
char ch;
for(i = 0; i < 3; i++) printf(strs[i], ch)
There are some compilers (such as gcc) that understand how to figure out if you pass the right things to a printf function, but that's only as a way to help you get the code right.
[And as a side not, any other "variable argument function" (printf can take any number of arguments from 1 up to hundreds) will follow the same rule].
--
Mats
Yes, built-in types are about supporting ranges of values. It's one of the more important C "secrets." Along the lines of what mats was saying, I suspect that the practical reason printf promotes the way it does is due to specific issues when the function either does arithmetic, or calls something that does arithmetic on your variables. (Functions promote lesser types of arguments according to their signatures.) The printf your standard library supplies is often built out of system specific API calls that would have their own signatures.Quote:
Basically data types are just a way to impose boundaries on continous, but these boundaries aren't absolute.
Of course, that's just based on information I gleaned from here. I expect if you read that you might gain a clearer understanding.
>>matsp
This conversion is only done in the context of printf's presence right?
>>citizen
Thanks, I'll have a look when I return. Hopefully it's not too much over my head.
I do not get it. printf as any other function that does not has specified types of argumants in the signature promotes integer types to int, float types to double as Standard specifies. Standard says nothing about "arithmetic" done by function - only about missing type specification in the signature
Not going to argue that point. There are practical reasons why the standard is written the way it is for implementors, though.
Honestly though, you should have read a little further. I'm discussing implementation detail. I suspect that somewhere down the line arithmetic (in C) is a factor in all of these promotions as dictated by the standard. A function would need to be called to do the work of converting doubles to strings and printing them to precision -- you can't convert from a numerical type to a string type without some arithmetic operations. And as my source explains, the compiler promotes accordingly to make sure that arithmetic is only performed on certain types.
And if you declare your own variable argument functions:
In this case, func will be called with two integers (c1 and c2 extended to integer) and two double values (d1 as it is and f1 extended). Of course, it would be up to the func() implementation to make meaningfull use of the data passed in, and that may not be easy with the above function...Code:#include <stdarg.h>
int func(int firstarg, ...);
...
char c1, c2;
float f1;
double d1;
func(42, c1, c2, d1, f1);
...
printf() and it's close siblings are not "magical" in this respect. It's a simple fact that "if you don't give the arguments a name, it can will be converted".
--
Mats