leading zero's..... ( reading int )

This is a discussion on leading zero's..... ( reading int ) within the C Programming forums, part of the General Programming Boards category; i have a question about leading zero's... now, if i have made a program, and declared everything to be of ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    36

    leading zero's..... ( reading int )

    i have a question about leading zero's...


    now, if i have made a program, and declared everything to be of type int

    if i prompt the user to enter in an integer value, and the user enters:

    0005

    then designed my program to read in the integer, using scanf(), then print the integer back using printf()

    why would the leading zeros not be shown???

    is it something to do w/ the properties of int?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Numbers are just numbers. In regular math, 1 is 01 is 001 is 0001. It's all just different ways of representing the number 1.

    When you read in 0005 via scanf(), it's just 5. That's what the number really is, right? Therefore 5 is stored. When you print it back, it prints 5 back. Remember, memory is laid out in terms of bytes. You can't just tell it not to store the extra bits.

    Pretending you read it into a one-byte memory address, 5 is stored as this:

    00000101

    The 0's and 1's have to be set. They can't just disappear.

    Now if you want to print it back with leading zero's, you can do that via printf().

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    36
    Quote Originally Posted by MacGyver View Post
    Numbers are just numbers. In regular math, 1 is 01 is 001 is 0001. It's all just different ways of representing the number 1.

    When you read in 0005 via scanf(), it's just 5. That's what the number really is, right? Therefore 5 is stored. When you print it back, it prints 5 back. Remember, memory is laid out in terms of bytes. You can't just tell it not to store the extra bits.

    Pretending you read it into a one-byte memory address, 5 is stored as this:

    00000101

    The 0's and 1's have to be set. They can't just disappear.

    Now if you want to print it back with leading zero's, you can do that via printf().

    so if i want to display the leading zero's, i can keep type int?
    but maybe just display something other than %d?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int x = 5;
    	
    	printf("&#37;05d\n",x);
    	
    	return 0;
    }
    Output:

    Code:
    00005
    Google for information on printf()'s format specifiers.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    36
    Quote Originally Posted by MacGyver View Post
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int x = 5;
    	
    	printf("%05d\n",x);
    	
    	return 0;
    }
    Output:

    Code:
    00005
    Google for information on printf()'s format specifiers.


    ohhhhhh ok, i get it
    thanks !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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