Code::Blocks simple guide – formatted print statement

 

I will discuss on formatted print statement here using the example below:


The first line “print* ” is meant to simply create an empty line. Next we have a print statement whereby the number “101” provide a unique identity to the format that we will specify below.


There are three parts (or items) to be printed. The first is a series of characters i.e. the line “The momentum of the object is”. The second is a variable “momentum” in which the value is obtained from calculation, and finally another series of characters.


Now when referring to the next line which specifies the format of the print statement, we see that characters are associated with the alphabet “a”. Next is the format for the variable. The variable “momentum” here is a real data type. Therefore, the format for the print statement of this variable starts with “F” or “f“. The first number following it (here it being 6) specifies the amount of digits plus decimal mark that we want in the value to be printed. The number after the decimal mark (here 3) specifies the number of decimal points.  For a more illustrative example, let us consider the result after taking 3 divided by 2.


If we choose a format of F6.4, we will have

1.5000


and with F6.3 we get

1.500


and finally with F3.1, we will have

1.5

When printing integers, the same concept applies except that the starting alphabet is now “I” or “i“. Here are examples for the print format for integers:

1000 – I4

200 – I3

10 – I2


Finally, a last remark is with regards to the symbol “1x” in the print statement above. This is used to create one empty (horizontal) space. You may use other numbers followed by “x” to create larger empty spaces.