Printf(function)

From LQWiki
Jump to navigation Jump to search

Printf() is a function in C that allows text to be printed to a console. Obivously this comes in handy, and is one of the most used functions. Printf() is defined in the stdio.h include file, but most compilers, including gcc, will compile without this declaration.

Possible usages are as follows:

printf("I am a duck.\n");
strcpy(animal, "duck"); //copy "duck" to the string "animal"
printf("I am a %s.\n",animal);
int i=6;
printf("Value: %d\n",i);
float f=0.68;
double d=12.19;
printf("Values: %f, %f\n",f,d);

As you can see, there are a number of uses. A few things to keep in mind:

1) The "\n" is a "Character-return Line feed", or a "Next Line". Basically, this is just like pressing enter. Others include \b (backspace), \\ (back slash), and \" (a quote)

2) the %f can be used for both doubles and floats. Also, you can format how many digits in front and behind the decimal point. Thus, for 3 spaces in front of the point, and 4 behind, we could use "%3.4f"