printing a number that starts with 0

This is a discussion on printing a number that starts with 0 within the C Programming forums, part of the General Programming Boards category; I am trying to print a number that starts with 0 and incremented 100 times ex: 0200, 0201, 0202,....... I ...

  1. #1
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    printing a number that starts with 0

    I am trying to print a number that starts with 0 and incremented 100 times

    ex: 0200, 0201, 0202,.......

    I used a for loop but the program outputs some other weird numbers....I guess it does not like teh "zero" at the begining of a number.

    num=99001;

    for(k=0;k<100;k++)
    {
    printf("%d",num);
    printf("\n");
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    A 0 prefix on an integer in C means that the value is in octal base, the 0 isn't printed unless you specify it to be. You can fake it by adding the 0 manually:
    Code:
    #include <stdio.h> 
    
    int main ( void )
    {
      int i;
    
      for ( i = 200; i < 300; i++ )
        printf ( "%04d\n", i );
      
      return 0;
    }
    My best code is written with the delete key.

  3. #3
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    Thanks Prelude......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  3. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM
  4. reading a text file printing line number
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-16-2005, 10:31 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

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