CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
How can I convert a string to a number?
Category C Interview Questions
Rating (4.3) By 6 users
Added on 10/22/2004
Views 6918
Rate it!
Answers:

The standard C library provides several functions for converting strings to numbers of all formats (integers, longs, floats, and so on) and vice versa.

The following functions can be used to convert strings to numbers:

Function Name Purpose

atof() Converts a string to a double-precision floating-point value.

atoi() Converts a string to an integer.

atol() Converts a string to a long integer.

strtod() Converts a string to a double-precision floating-point value and reports any ?leftover? numbers that could not be converted.

strtol() Converts a string to a long integer and reports any ?leftover? numbers that could not be converted.

strtoul() Converts a string to an unsigned long integer and reports any ?leftover? numbers that could not be converted.



You can convert an string to an number.

#include<stdio.h>
#include<conio.h>
void main()
{
char ch[20];
int No;
clrscr();
printf("Enter the string which contain the No.:");
scanf("%s",&ch);
No=atoi(ch);
printf("Integer No.=%d",No);
getch();
}



 Posted by: Alok Bajpai    

Contact Alok Bajpai  Contact Alok Bajpai

Use sscanf to convert if you are sure that the string contains only digits.

#include<stdio.h>

int main()
{
char buf[20];
int num;
printf("Enter the string which contains integer number :");
scanf("%s", buf);
sscanf(buf, "%d", &num);
printf("Integer Num = %d ", num);
return (0);
}



 Posted by: Rohini V    

Contact Rohini V  Contact Rohini V

int main()
{
char str[6];
int num=0,i;
printf("Enter a string containing a number:");
scanf("%s",str);
for(i=0,str[i]!='

 Posted by: santosh kumar mallick    

Contact santosh kumar mallick  Contact santosh kumar mallick




If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
How do you print only part of a string?
View Answer
What is indirection?
View Answer
How many levels of pointers can you have?
View Answer
What is a null pointer?
View Answer
What is a void pointer?
View Answer
Is NULL always defined as 0?
View Answer
What does it mean when a pointer is used in an if statement?
View Answer
Can you add pointers together? Why would you?
View Answer
How do you use a pointer to a function?
View Answer
When would you use a pointer to a function?
View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/1013/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.61