C programming: How to convert a character to an integer, float in C?
Convert character to integer, float in C.
In C, you can use the atoi() function to convert a string of characters to an integer. Here's an example code snippet:
Code:
# include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "1234";
int x = atoi(str);
printf("The integer value of the string is: %d\n", x);
char str1[] = "*1234";
x = atoi(str1);
printf("The integer value of the string is: %d\n", x);
return 0;
}
Output:
The integer value of the string is: 1234 The integer value of the string is: 0
In the above code, the atoi() function is used to convert the string "1234" to the integer 1234. The function takes a string as input and returns the integer equivalent of the string. If the input string ("*1234") is not a valid integer, the atoi function returns 0.
Code to convert character to float in C:
In C, you can use the atof () function to convert a string of characters to a floating-point value. Here's an example code snippet:
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
char char_array1[] = "45.82734";
float float_value = atof(char_array1);
printf("The converted value is: %f\n", float_value);
return 0. ;
}
Output:
The converted value is: 45.827339
In the above code, atof() function is used to convert the character array "char_array1" to a floating-point value "float_value". The function atof() takes a character array as input and returns a floating-point value.
Note that atoi() , atof() functions requires the stdlib.h header file.
Contribute your code and comments through Disqus.
C Programming: Tips of the Day
C Programming - How do you pass a function as a parameter in C?
Declaration
A prototype for a function which takes a function parameter looks like the following:
void func ( void (*f)(int) );
This states that the parameter f will be a pointer to a function which has a void return type and which takes a single int parameter. The following function (print) is an example of a function which could be passed to func as a parameter because it is the proper type:
void print ( int x ) { printf("%d\n", x); }
Function Call
When calling a function with a function parameter, the value passed must be a pointer to a function. Use the function's name (without parentheses) for this:
func(print);
would call func, passing the print function to it.
Function Body
As with any parameter, func can now use the parameter's name in the function body to access the value of the parameter. Let's say that func will apply the function it is passed to the numbers 0-4. Consider, first, what the loop would look like to call print directly:
for ( int ctr = 0 ; ctr < 5 ; ctr++ ) { print(ctr); }
Since func's parameter declaration says that f is the name for a pointer to the desired function, we recall first that if f is a pointer then *f is the thing that f points to (i.e. the function print in this case). As a result, just replace every occurrence of print in the loop above with *f:
void func ( void (*f)(int) ) { for ( int ctr = 0 ; ctr < 5 ; ctr++ ) { (*f)(ctr); } }
Ref : https://bit.ly/3skw9Um
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises