main function?
main.c?
printf function? Why do you need to do this? How do you link the printf function?
stdio.h? Why angle-brackets in #include<stdio.h>? Hint:
C preprocessor: including_files.
printf
function (the more possibilities you name, the better)?
tgmath.h? If you use tgmath.h do you need to link any library?
int,
float,
double,
long double,
double complex,
long double complex;
printf function.
You might want to read the printf
format string article, section format placeholders.
creal (double),
creall (long double),
cimag (double), and
cimagl (long double)
functions. complex.h
defines the imaginary unit as I. printf function with the command man 3 printf
although the abovementioned wikipedia article https://en.wikipedia.org/wiki/Printf
is probably a bit more readable.float, double, and long double
hold in them by, for example, trying to store the number
0.7777777777777777777777777777
in the variable and then printing out what is stored. The long
double number must end with L:
long double x = 0.7777777777777777777777777777L;
otherwise it will be considered as double and truncated. Print numbers
with format placeholders "%.20g" for float, "%.20lg" for double,
"%.20Lg" for long double.
printf functionBefore using printf you need to #include<stdio.h>.
The first parameter to printf function is a string of
characters which is called 'format string' or 'template'. If it
is the only parameter, this string is simply printed to the standard
output (which by default is your terminal). For example,
printf("hello\n");
produces
hello
(the string \n represents the newline character).
printf can have more parameters
which then must be the names of the variables (or expressions) which you
want to print out. In this case the format string must contain placeholders for the
parameters to be printed. Placeholders must correspond to the types of
parameters to be printed!
By default the first parameter after the format string takes the first placeholder, the second parameter after the format string takes the second placeholder and so on.
We shall mostly print out numbers, therefore here are the simplest forms of placeholders for numbers,
%i
For example
int n=123; printf("n = %i, twice n = %i, five times n = %i \n", n, 2*n, 5*n);
produces
n = 123, twice n = 246, five times n = 615
%g (actually %lg but %g seems to work somehow)
For example
double x=1.23; printf("x = %g, twice x = %g, five times x = %g \n", x, 2*x, 5*x);
produces
x = 1.23, twice x = 2.46, five times x = 6.15
%Lg
The full description of format placeholders can be found
in
wikipedia or in the man pages (man 3 printf).