convet numbers with one digit

This is a discussion on convet numbers with one digit within the C Programming forums, part of the General Programming Boards category; Hello every body i want to conver numbers : 0 , 1 , 2, 3,4 ,5 , 6, 7,8 , ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    10

    convet numbers with one digit

    Hello every body

    i want to conver numbers :
    0 , 1 , 2, 3,4 ,5 , 6, 7,8 , 9
    to the following forum :
    00 , 01 , 02 , 03, 04 , 05 , 06 , 07 ,08 , 09

    Thanks a lot
    Nada

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    34
    Nada,

    If you just want to display them in this form (01, 02), do the following:

    printf("%02d", 1);

    this should display 01 for you. If you want more zeros, just change the field width specifier. If you make the field width 5, it will display the number padded with as many zeros as it takes to fill the field.

    clu

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    10
    Thanks a lot for reply

    But i want to get 01 form without using printf .In anther word , i need to save number in array or any think else then when i need it i just type array name and the result apear in the follwoing form : 01 , 02, ...etc

    so pllllease i need solution with out depend on printf

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    34
    You will need to store the number as a string then. When a number is stored as an integer or long, it doesn't get stored with leading zeros.

    int num = 0;
    char str_num[3];

    sprintf(str_num, "%02d", num);

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    27

    int format

    Hi,

    Integers are stored in bytes( 2 in the old days and now mostly 4 bytes).
    In most systems they are stored low byte first.
    Take a 2 byte int for instance.

    The number 25 would be 19 00 HEX

    The number 255 would be FF 00 HEX
    Thus 256 would be 00 01 HEX

    You can see that leading zero's are not part of the format for int's.

    Hope this helps.
    Pappy
    You learn something new everyday.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    10
    Thaaaaaaks a lot for support

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple 5 digit
    By wonderpoop in forum C Programming
    Replies: 10
    Last Post: 10-16-2006, 04:26 AM
  2. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 03:43 PM
  3. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 09:45 PM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM

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