getting invalid atoi value for the below program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jagadeeshaHS
    New Member
    • Jan 2010
    • 1

    #1

    getting invalid atoi value for the below program

    Hi,
    getting invalid atoi value for the below program, help me resolving this issue.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    
    int main () {
        char *num = "222211113333";
        int atNum ;
        printf ("character value :%s\n ", num);
        atNum = atoi (num);
        printf ("\n\ninteger number : %d\n", atNum);
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    OK Please put code tags, [code] ... [/code] round your code.

    Also give us all the information you say you are getting an invalid vale well what was the value you where getting? What was the value you where expecting?

    I am guessing you where expecting the value 222211113333 to be output and you either got 3167781237 or 2222111133 but you will need to confirm that.

    You need to ask yourself 2 questions:
    1. How many bits do I need to hold the value 22221111333? You should easily be able to answer this with the aid of the calculator on your computer.
    2. How many bits does an int hold on my platform? Something you should be able to answer by reading the platform documentation or looking in the header files (limits.h for example).

    Comment

    • Ectara
      New Member
      • Nov 2009
      • 24

      #3
      Also consider whether or not your datatype is unsigned; it has an effect on the maximum size of your positive integer. That said, your string literal still contains a number larger than both signed and unsigned 32 bit integers can hold.

      Comment

      Working...