how to convert integer to string in C?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: how to convert integer to string in C?

  1. #1
    Join Date
    Mar 2009
    Posts
    55

    how to convert integer to string in C?

    Hi,

    I am facing problem in converting integer to string in C programming?Iam using Code composer Studio IDE for debugging the program.There not accepting itoa() function for string conversion.Please tell me some other method for intger to string conversion.Any body please help me.

    Thanks

  2. #2
    Join Date
    Jul 2005
    Location
    E: 120.6, N: 31.3′
    Posts
    795

    Re: how to convert integer to string in C?

    A example to illustrate:
    Code:
    #include <stdlib.h>	// for itoa() call
    #include <stdio.h>	// for printf() call
    
    int main() {
    	int num = 123;
    	char buf[5];
    
    	// convert 123 to string [buf]
    	itoa(num, buf, 10);
    
    	// print our string
    	printf("&#37;s\n", buf);
    
    	return 0;
    }
    Little by little one goes far
    Keep moving.......!
    Nothing is impossible !

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,294

    Re: how to convert integer to string in C?

    Quote Originally Posted by sunny_sz
    A example to illustrate:
    Except that itoa is non-standard, and sudha_sakthi explicitly noted that it was not available.

    Quote Originally Posted by sudha_sakthi
    Iam using Code composer Studio IDE for debugging the program.
    With a Microsoft compiler? Note that you posted this thread in the Visual C++ Programming forum.

    Quote Originally Posted by sudha_sakthi
    Please tell me some other method for intger to string conversion.
    One way is to use sprintf() with a sufficiently large buffer. To modify sunny_sz's example:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int num = 123;
    
        char buf[12];
        sprintf(buf, "&#37;d", num);
    
        printf("%s\n", buf);
    
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: how to convert integer to string in C?

    With a Microsoft compiler? Note that you posted this thread in the Visual C++ Programming forum.
    That means that he is in the right place.

    Except that itoa is non-standard, and sudha_sakthi explicitly noted that it was not available.
    Explicitly ? And he said that because it was non-standard ? I guess I need reading glasses....


    There not accepting itoa() function for string conversion.
    Why not ? The whole world uses it ...

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,294

    Re: how to convert integer to string in C?

    Quote Originally Posted by Skizmo
    That means that he is in the right place.
    If the answer is yes, that is. The Code Composer Studio IDE appears to come with a compiler from Texas Instruments rather than Microsoft.

    Quote Originally Posted by Skizmo
    Explicitly ? And he said that because it was non-standard ? I guess I need reading glasses....
    Yes, you do I stated that it was non-standard. sudha_sakthi stated that it was "not accepting itoa() function for string conversion". Granted, I might have misinterpreted the statement, but since it is non-standard, it is reasonable to suppose that "not accepting" was due to "unavailable", considering that I already suspect that the compiler is from Texas Instruments.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Mar 2009
    Posts
    55

    Re: how to convert integer to string in C?

    Hi all,

    yes. the code composer IDE is from Texas Instrument .Using sprintf() function it is working fine.

    Thank u....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center