LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to convert char value into integer value in c programming (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-convert-char-value-into-integer-value-in-c-programming-651512/)

kpratheep 06-25-2008 05:30 AM

How to convert char value into integer value in c programming
 
solution needed

tronayne 06-25-2008 06:46 AM

Individual characters are integers
Code:

#include <stdio.h>

char    c = 'A';

main    ()
{
        printf ("%d\n", c);
}

Strings are not and you need to use a function to convert "12345" to 12345:
Code:

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

char    *string = "12345";

main    ()
{
        printf ("%d\n", atoi (string));
}


alan_ri 06-25-2008 07:09 AM

Welcome!
Will this help you;
Code:


#include <string.h>
#include <stdio.h>
int main()
{
char szTemp[10];
int nNumber = 0;
strcpy(szTemp, "10");
// Convert the string to number.
nNumber = atoi(szTemp);
printf("The converted number is : %d \n",nNumber);
return 0;
}

Also I've found this site while searching a little,it has some nice things.Anyway you could search yourself a little,because there are so many guides,tutorials and books about C on the net that will help you understand C much better and we have Programming forum here,so you could've started your thread there and searched it a little too.


All times are GMT -5. The time now is 05:54 AM.