Showing posts with label atoi in c. Show all posts
Showing posts with label atoi in c. Show all posts

Thursday, December 17, 2020

, , , ,

Typecast Functions Like atoi in c,atof, itoa, atol more

 atoi in c is used to convert the string data type into int data type.

atoi function in c
 atoi function in c

 

Syntax for atoi in c:-

int atoi (const char * str);


 atoi in c function in c is used with the “stdlib.h” header file Well this function is also known as the typecast functions in C.

Example program for the atoi in c

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char a[10] = "100";
    int value = atoi(a);
    printf("Value = %d\n", value);
    return 0;
}


Lets check the similar typecast functions Like atoi in c.

atof() function in c  :- atof function work similar like atoi the only difference is atof in c used to converts string data type into float data type.

 Example program for atof() function in c :-

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char a[10] = "3.18";
    float pi = atof(a);
    printf("Value of pi = %f\n", pi);
    return 0;
}

 atol() function in c  :-  This function is used to change the string data type to long data type like it's name(atol).

Syntax for atol in c:  -

long int atol ( const char * str );

Example program for atol() function in c :-


#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char a[20] = "100000000000";
    long value = atol(a);
    printf("Value = %ld\n", value);
    return 0;
}


 itoa() function in c  :- This function is reversed of atoi in c, itoa function is used to convert the int data type to string data type.

Syntax : - 

char *  itoa ( int value, char * str, int base );

Example program for itoa() function in c


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main()
{
    int a=54325;
    char buffer[20];
    itoa(a,buffer,2);   // 2 means decimal
    printf("Binary value = %s\n", buffer);
 
    itoa(a,buffer,10);   // 10 means decimal
    printf("Decimal value = %s\n", buffer);
 
    itoa(a,buffer,16);   // 16 means Hexadecimal
    printf("Hexadecimal value = %s\n", buffer);
    return 0;
}