Solved

Simple library Q

Posted on 1998-11-09
5
167 Views
Last Modified: 2010-04-15
What function in what library do I need to convert an integer to a string
0
Comment
Question by:martincollis
  • 2
  • 2
5 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 20 total points
ID: 1254118
_itoa (not all compilers have this - some omit underscore) or sprintf

#include <stdlib.h>
#include <stdio.h>

int main( int argc, char * argv[] )
{
char s1[256] ;
char s2[256] ;
int i1 = 1 ;
int i2 = 2 ;

sprintf( s1, "%d", i1 ) ;
_itoa i2, s2, 10 ) ; /* 10 = base ten */

return 0 ;

}



0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1254119
oops typo, I meant:
_itoa( i2, s2, 10 ) ; /* 10 = base ten */
0
 

Author Comment

by:martincollis
ID: 1254120
I am compiling in gcc (on unix), and the _itoa seems to be accepted except the demonstration code returns a segmentation fault when executed.
Any ideas?
0
 

Author Comment

by:martincollis
ID: 1254121
By the way I am removing the sprintf line!
0
 
LVL 84

Expert Comment

by:ozo
ID: 1254122
You'd be more portable keeping the sprintf and removing the _itoa line
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now