Thread: Printf and variable formatting

  1. #1
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158

    Printf and variable formatting

    Hello,

    I have a variable, TablePrecision, that specifies the precision of a number. How do i get printf to use that variable in its formatting without me having to hardcode it?

    Code:
    int TablePrecision = 2;
    double number = .839234;
    
    printf("%.2g", number); // put TablePrecision where the 2 is
    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Maybe
    printf("%.*g", TablePrecision, number);

    The manual page tells all.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    oh!

    i thought it was only for white space. thanks, sorry for the stupid question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems reading entered race times C
    By loopymoo26 in forum C Programming
    Replies: 12
    Last Post: 05-23-2009, 07:38 AM
  2. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  3. Replies: 4
    Last Post: 04-01-2003, 12:49 AM
  4. Need help
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 12-09-2001, 03:02 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM