Last time we took a look at a simple program in C that used the printf function to output some string to the screen. Today, we will look further at printf and discuss other cool things that you can do with it.
A standard printf statement will look something like this:
printf(format-string, arguments);
A format-string can consists of three components: regular characters, control characters, and formats.
Regular Characters: Any letter or number.
Control Characters: (Note the new line character '\n' that we saw in the past tutorial)
\a Write a <bell> character.
\b Write a <backspace> character.
\c Ignore remaining characters in this string.
\f Write a <form-feed> character.
\n Write a <new-line> character.
\r Write a <carriage return> character.
\t Write a <tab> character.
\v Write a <vertical tab> character.
\' Write a <single quote> character.
\\ Write a backslash character.
Formats: (A few basic ones are listed below)
%d Decimal integer
%c Character
%s String
%f Floating Point
%lf Double / Long Flaoting Poing
%p Pointer
Review of Variables and their Declarations
A variable is a place holder for a value stored in memory on you computer. A standard declaration of a variable is as follows:
variable_type variable_name = variable_value;
As an example, we can declare an integer, named height, and assign it the value of 6:
int height = 6;
Note: You MUST declare a variable before you can use it!
Variable Types
TYPEUSUAL SIZERANGE (signed) char1 byte-128...127 short int 2 bytes -32,768...32,767 or, int 4 bytes -2,147,483,648...2,147,483,647 float 4 bytes 3.4E-38 ... 3.4E+38 double8 bytes 1.7E-308 ... 1.7E+308
Below are to simple programs, leave a comment below with the correct output of both programs:
1.
#include <stdio.h> int main(void) { printf("*\n**\n"); printf("***\n****"); printf("\n***"); printf("**\n******\n"); return 0; }
2.
#include <stdio.h> int main(void) { int length = 10; printf("The value stored in the length variable is: %d", length); return 0; }
More tutorials coming soon! Let me know how you like the tutorials by commenting below and if there is any specific topics or languages you would like covered.