Thread: printing a number that starts with 0

  1. #1
    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,897
    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
    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, 05:00 PM