LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-19-2008, 08:59 PM   #1
hoshangi
Member
 
Registered: Nov 2007
Posts: 88

Rep: Reputation: 15
Convert string to integer in c


hi
i have a problem;when i want to convert a string from text file to an integer (using sprintf) when i run ;the content of variable getting 0 please help me
 
Old 04-19-2008, 09:52 PM   #2
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
sprintf is for converting to strings. To convert the other way, use atoi or strtol.
 
Old 04-20-2008, 01:24 AM   #3
hoshangi
Member
 
Registered: Nov 2007
Posts: 88

Original Poster
Rep: Reputation: 15
thanks my friend for your help
do you have an example?
 
Old 04-20-2008, 01:32 AM   #4
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
int a = atoi ("56");
long int b = strtol ("56", NULL, 10);

edit: read the man or info pages ("man atoi", "man strtol", "info libc" (or use tkinfo as its easies to use I think) arithmetic functions->parsing of integers)

Last edited by SciYro; 04-20-2008 at 01:34 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to convert an integer type to string? nesta Programming 1 04-11-2007 07:15 PM
c convert integer to string? kpachopoulos Programming 5 08-23-2006 11:57 AM
C how to convert an integer to string? totti10 Programming 10 11-10-2005 05:49 AM
convert string to integer with python Kanaflloric Programming 2 05-27-2005 11:04 AM
How do you convert a string to an integer? wbdune Linux - Newbie 11 11-01-2003 02:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration