Printf and variable formatting

This is a discussion on Printf and variable formatting within the C Programming forums, part of the General Programming Boards category; Hello, I have a variable, TablePrecision, that specifies the precision of a number. How do i get printf to use ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 05:54 AM
  3. Replies: 4
    Last Post: 03-31-2003, 11:49 PM
  4. Need help
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 12-09-2001, 02:02 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21