Saturday, September 6, 2008

C...learning: Cleared up printf and the "%" operator

Finally I've managed to cover my empty spaces about this 2 elements of C.

In the printf fuction before outputing a value you have to declare its kind in the brackets ("...") by using the % symbol in the brackets followed by the type the value is gonna be (decima,float,char) 

C does not support the cout function from C++ as an older language.

"C does not have overload operators, so there is only one printf fuction that accepts any number of parameters - type of the passed parameter is not known to the function.
to get the function some idea - how it should interpreter the passed parameters - format specifiers are used, each format specifier starts with %
"

Also I couldnt understand how precision works in printf (%x.xf). The right part was  cuting numbers but the left part  seemed to do nothing

But as found at MSDN library

"...If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers)."

There was finally another wonder. I couldn't find why someone would use the hexademical system for variables? Finally someone told me that there could be 3 reasons for that

"1st off you are required to have a hex editor to read the file (when looking at a hex file through notepad or th elike it will just be jumbled text).

2ond, you can store all values between 0 and 255 in 2 characters instead of 3, which can cut down on the number of characters you need and thus the file size when you are programming say a game where inventory and things can be changed and have nultiple values.

3rd, it really doesn't matter to the C/C++ compiler. Both use a base 2 (Binary) system for numeric calculations so it has to convert anyway, base 16 (Hex) compared to base 10 (Decimal) isn't very different after being converted to base 2."

I admit that I can't fully understand all of the reasons wright now nor apllicate the reasons. But just this knowledge is enough for now.

Well, by the present facts and calculations, I  think that I can proceed to the 2nd of 8 chapter which is about the "Control Commands" (if,else...). Probably I will be finishing it rather quickly as I know most of the functions from C++ but never swear on these things.



No comments: