C Basics
printf()
Before we start diving in to writing code we need to be able to output information to the screen so we can see what we are doing. (And so we can add outpu test to see why our code isn’t working properly :-)). The printf function is that standard way of outputting information.
printf() is the formatted print function. It is simple to use when you get the hang of it but can be tricky to understand at first. printf() is used as follows
printf("string to output %d with %c identifiers indicated by percentage symbols to show where to put variables\n", 5, 'c');
The first argument to printf is a string, using “” around it. printf() will output the contents of the string.
printf("hello world");
will output hello world
and leave the cursor at the end of the word world. The next output will start from that point.
printf("hello ");
printf("world")
will produce the same output as
printf("hello world");
as will
printf("he");
printf("ll");
printf("o ");
printf("wo");
printf("rl");
printf("d");
New Lines
printf("hello world\n");
will output hello world
and start a new line. The \n
in the string means inset a new line here.
printf("hello\nworld\n");
will output hello
on one line and world
on the next.
printf("\n\n\nhello world\n");
will output three blank lines then hello world
on the next and then start a new line.
\t
will insert a tab
Outputting Variables
To output variables (or values) you have to indicate where in the string to put them. This is doen using the percentage symbol %. A % symbol in the string indicates that a variable should be inserted at that point.
printf("Variable 0 %");
expects to output a variable (or value) at the %
symbol. But C needs more information than this. It needs to know which variable to output. We tell C which variables to output by adding it as a parameter after the string.
int iVal = 1;
printf("Variable 0 %", iVal);
C needs one more piece of information, it needs to know what variable type we want to output. iVal
is an integer so we use %d
(or %i
);
int iVal = 1;
printf("Variable 0 %d", iVal);
This will output Variable is 1
.
printf Variable Types
Code | Output Type |
---|---|
c | character (char) |
d or i | integer (int) |
e | scientific notation |
f | floating point (float) |
s | string of characters |
u | unsigned integer |
There are other possibilities we will discuss later on.
Why do we need to tell C what type of variable we want to output? Can’t it work it out for itself?
The reason is that we aren’t telling C what type of variable to be output but instead telling it how we want the contents of the variable to formatted on output. What does this mean? Consider the following example.
char cVal = 'A';
printf("The character is %c\n", cVal);
printf("The integer value of the character is %d\n, cVal");
The output would be
The character is A
The integer value of the character is 65
char Variable Type
The char
variable is used to store a single character. char cVal = 'A'
stores an A
in cVal
. A char is stored in memory as 1 byte (8 bits). It is stored in binary. Any binary number can be interpretted as an integer. So when we printf("The integer value of the character is %d\n, cVal");
we are taking the value stored in cVal
and interpretting it as an integer and that evaluates to 65
.
The integer value of a character is known as its ASCII value. The link below shows all the ASCII values. Note that some of them are not printable characters.
Precision Printing
float fVal = 3.5;
printf("float is %f\n", fVal);
results in something like
float is 3.500000
This is because a %f will display a floating point value to as many decimal places as it can. If you only want to see two decimal places (for example) you have to tell C that. You can do this by using %.2f
. The .2
between the %
and the f
tells C how many places to display after the decimal point.
The number before the decimal point indicates the minimum width of number to displayed (including the decimal place itself). If you want your numbers to line up to the right, as if they were right justified, you can use the number before the decimal point to force this.
So, in summary, using %x.yf
will output a floating point number in a way that is at least x characters wide (padded with spaces if it is too small) and has exactly y numbers after the decimal point (padded with zeroes if necessary).
Full details on printf()
printf() description on tutorialspoint
- Previous
- Next