Hey
I am trying to find a way for the user(not the programmer) to determine how many decimal places a number will print out to. I have tried pointers, precision mods and even a sub program to bypass my lack of knowledge in this area. Any help would be greatly appreciated.

Thanks!

void main(void)
{
    float total;
    float a;
    float b;
    float n;
    float SUM(float , float); /*function prototype*/

    printf("enter 2 numbers you would like to add separated by a space\n");
    scanf("%f %f", &add, &add1);
    printf("How many decimal places would you like your answer to go out to?\n");
     /*this is to determine how many decimal places user wants to print out to*/
    scanf("%f", &n); 

    total = SUM(a,b); /*subprogram that u can see below*/

    /*(this is where i have tried to put pointers and different precision mods)*/
    printf("Added together they are %f\n",n, total);
}

float SUM(float a, float b)
{
    return(a+b);
}

Dani AI

Generated

Short answer: read the precision as an integer and print the sum with a run‑time precision instead of trying to store the precision in a float. The two safe patterns already suggested in the thread are (a) pass the precision value to the printf family using the precision * (most concise) and (b) build a format string and use it. The * precision method is part of the printf format specification and is the simplest way to let the user pick the decimal count at run time. (docs.cppreference.com)

Type and input rules to fix in the original code: use an integer type for the number of decimals (not float), use double for arithmetic unless single precision is specifically required, and use the correct scanf conversion codes (%d for int, %lf for double). Also remember that a float argument passed to a variadic function like printf is promoted to double, so %f will consume a double. These are common causes of bad output and undefined behaviour. (documentation.help)

Safety and robustness notes: prefer snprintf (or check buffer sizes) rather than sprintf if constructing a format or output string, and always validate/clamp the user’s precision to a sensible range (for example 0..20) to avoid extremely large outputs or surprising behaviour. Check the return value of scanf to verify successful conversions and declare main with an int return type per the standard. (en.cppreference.com)

Troubleshooting checklist (fast): make sure all variables are initialized before use; match each scanf/printf format specifier to the correct argument type and count; clamp the precision value; prefer double for storage and %lf when reading; and prefer the * precision approach for clarity, or snprintf if a bounded buffer is needed. For format details and exact behaviour of precision and conversion specifiers consult the printf/snprintf reference. (docs.cppreference.com)

Notes: builds on ’s and ’s suggestions while adding type, safety, and validation points for reliable, standards‑conforming output.

Recommended Answers

All 7 Replies

You can set the precision of a number using sprintf:

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

int main(int argc, char** argv)
{
    float nr1 = 0.0f;
    float nr2 = 0.0f;
    float sum = 0.0f;
    int n = 0;
    char buffer[30];

    printf("Enter the two numbers:\n");
    scanf("%f %f",&nr1,&nr2);

    printf("How many decimal places would you like your answer to go out to?\n");
    scanf("%d", &n);

    /*Here you set the precision*/
    sprintf(buffer,"Added together they are %%.%df\n",n);
    printf("The printf format string will be: %s",buffer);

    sum = nr1+nr2;

    printf(buffer,(sum));
    return 0;
}
/*Output
Enter the two numbers:
32.3 83.2
How many decimal places would you like your answer to go out to?
6
The printf format string will be: Added together they are %.6f
Added together they are 115.500000
 */

main is not void and you have not defined add and add1, a and b are uninitialized... you have not enough formats for your parameters in printf.

Dheaven-thank you. I didnt know about sprintf and now my knowledge of c programming is growing. I will read up on this some but would you mind explaining line 19 of your code

sprintf(buffer,"Added together they are %%.%df\n",n);

because i do not quite understand the purpose what the sprintf does and the function it plays with the buffer preceding the print statement.

pyTony-thank you as well. What do you mean by i do not have enough formats for my parameters, i dont quite understand what you mean

I said you have too many paramaters in printf compared to the format string.

Good, but little excessive code for my taste dheaven. As you gave solution, here is my fixed code:

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

int main(int argc, char** argv)
{
    float nr1 = 0.0f;
    float nr2 = 0.0f;
    int n = 0;

    printf("How many decimal places would you like your answer to go out to?\n");
    scanf("%i", &n);

    printf("Enter the two numbers:\n");
    scanf("%f %f",&nr1,&nr2);

    printf("Added together they are %.*f\n", n, nr1+nr2);
    return 0;
}
/*Output:
How many decimal places would you like your answer to go out to?
3
Enter the two numbers:
1.345 7.51212
Added together they are 8.857
*/
commented: yes! +2

pyTony- I understand your code better due to the lack of strings and buffers which i am not yet familiar. Thank you
Would you be willing to further explain line 19 and 20 of dheavens code

sprintf(buffer,"Added together they are %%.%df\n",n);
printf("The printf format string will be: %s",buffer);

so that i can completely understand all of the points you guys have made

Hello sebass123

pyTony variant is more elegant (I forgot about * option)

sprintf is used for formatting a string. I practically inserted the number representing the precision of the floating point number into the format string used by printf.

You can read here to find more about sprintf and format specifiers:

On the other hand, pyTony eliminated the need for sprintf by using * width specifier.

According the cplusplus.com, the width specifier is used when:

The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.