User Program Communication

Print in C

Printing is used to output text directly to the console.

printf("Text variableSpecifier Text", Variable); //with a variable


variableSpecifier is used to output a variable in its place, indicated by %.

Specifiers depend on variable type: %d (decimal), %f (floating-point), %e or %E (floating-point in scientific notation), %g or %G (uses the shorter version of %f or %e / %E), %s (strings).

For floating point numbers, precision can be indicated (the number of decimal points wanted) by adding: %.3f, where 3 represents the number of deicmal points.

To pad a number with zeros, add %03f, where 3 indicates the total number of values. Using %#x (with the x specifier), the number will be converted to hexadecimal.

Multiple specifiers can be used in this order: %[flag][width][.][precision][conversion specifier]. The flag can be: #, 0 (padding), -, + (shifting). The precison determines the max number placements where the width determines the min number placements %4.3f, where 4 represents the width and 3 represents the precision. The conversion specifier indicates the variable type of the output.

float pie = 3.14159; 

printf("The value of pi is: %g \n", pie);

Add to Slack

< Including Header Files   |   User Input >

© 2019 SyntaxDB. All Rights Reserved.