[source code] C Programming Tutorial - 6 - Conversion Characters

+2 Bucky Roberts · August 4, 2014



#include <stdio.h>
#include <stdlib.h>

int main()
{
    // If you want to use strings, use %s
    // Good for programs where users can enter their name
    printf("%s is the best person ever\n", "Bucky");

    // Use %d for integers
    printf("I ate %d corndogs at the fair\n", 9);

    // %f for numbers with decimal places (floating point values)
    //6 decimal places by default
    printf("Pi is %f \n", 3.1415926535);     
    printf("Pi is %.1f \n", 3.1415926535);
    printf("Pi is %.3f \n", 3.1415926535);

    return 0;
}


Post a Reply

Replies

Oldest  Newest  Rating
0 Jordasn Carlile · October 4, 2014
When the object code you have posted executes, It will round the values to the the specified decimal
e.g.
printf("Pi is %.3f \n", 3.1415926535);

will print
Pi is 3.142

Is there any way to have the it truncated instead of rounded? So it will print.
Pi is 3.141

Thanks
  • 1

C

107,268 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator