- 环礁(): 此函数将作为参数传递给函数调用的C类型字符串转换为长整数。它解析C字符串str,将其内容解释为整数,该整数作为long int类型的值返回。函数将丢弃字符串开头的空白字符,直到找到非空白字符。如果C-string str中的非空白字符序列不是有效的整数,或者由于str为空或仅包含空白字符而不存在此类序列,则不执行转换,并返回零。
语法:
long int atol ( const char * str )
参数: 该函数接受一个强制参数 str 这是一个整数的表示。
返回值: 该函数将转换后的整数作为长整数返回。如果无法执行有效转换,则返回零。
// CPP program to illustrate
// working of atol() function.
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
// char array of numbers
char
str1[] =
"5672345"
;
// Function calling to convert to a long int
long
int
num1 =
atol
(str1);
cout <<
"Number is "
<< num1 <<
""
;
// char array of numbers of spaces
char
str2[] =
"10000002 0"
;
// Function calling to convert to a long int
long
int
num2 =
atol
(str2);
cout <<
"Number is "
<< num2 <<
""
;
return
0;
}
输出:Number is 5672345 Number is 10000002
- 环礁: 此函数将作为参数传递给函数调用的C类型字符串转换为长整数。它解析C字符串str,将其内容解释为整数,该整数作为long long int类型的值返回。函数将丢弃字符串开头的空白字符,直到找到非空白字符。 如果C-string str中的非空白字符序列不是有效的整数,或者由于str为空或仅包含空白字符而不存在此类序列,则不执行转换,并返回零。
语法:
long long int atoll ( const char * str )
参数: 该函数接受一个强制参数 str 这是一个整数的表示。
返回值: 该函数将转换后的整数作为long-long int返回。如果无法执行有效转换,则返回零。
// CPP program to illustrate
// working of atol() function.
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
// char array of numbers
char
big_num1[] =
"8239206483232728"
;
// Function calling to convert to a long int
long
long
int
num1 = atoll(big_num1);
cout <<
"Number is "
<< num1 <<
""
;
// char array of numbers of spaces
char
big_num2[] =
"100000 9 1324100"
;
// Function calling to convert to a long int
long
long
int
num2 = atoll(big_num2);
cout <<
"Number is "
<< num2 <<
""
;
return
0;
}
输出:Number is 8239206483232728 Number is 100000
- 函数: 此函数将作为参数传递给函数调用的C类型字符串转换为double。它解析C字符串str,将其内容解释为浮点数,并以double类型的值返回。函数将丢弃字符串开头的空白字符,直到找到非空白字符为止。如果C字符串str中的非空白字符序列不是有效的浮点数,或者由于str为空或仅包含空白字符而不存在此类序列,则不执行转换,并返回0.0。
语法:
double atof ( const char * str )
参数: 该函数只接受一个强制参数 str 它是浮点数的表示形式。
返回值: 该函数将转换后的浮点数作为双精度值返回。如果无法执行有效的转换,函数将返回零(0.0)。
返回值:
// CPP program to illustrate
// working of atol() function.
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
// char array
char
pi[] =
"3.1415926535"
;
// Calling function to convert to a double
double
pi_val =
atof
(pi);
// prints the double value
cout <<
"Value of pi = "
<< pi_val <<
""
;
// char array
char
acc_g[] =
"9.8"
;
// Calling function to convert to a double
double
acc_g_val =
atof
(acc_g);
// prints the double value
cout <<
"Value of acceleration due to gravity = "
<< acc_g_val <<
""
;
return
0;
}
输出:Value of pi = 3.14159 Value of acceleration due to gravity = 9.8
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END