C Language Common data type collation

Source: Internet
Author: User

Atof (Converts a string to a floating-point number)
Atoi (Converts a string to an integer number)
ATOL (Convert string to growth integer)
Strtod (Converts a string to a floating-point number)
Strtol (Convert string to growth integer)
Strtoul (converts string to unsigned long integer)
Toascii (converts an integer number to a valid ASCII code character)
ToUpper (converts lowercase letters to uppercase letters)
ToLower (convert uppercase letters to lowercase)


Atof (Converts a string to a floating-point number)
Correlation function Atoi,atol,strtod,strtol,strtoul
Table header file #include <stdlib.h>
Defines the function double atof (const char *nptr);
The function Description Atof () scans the parameter nptr string and skips the preceding space character until it encounters a number
Character or sign to start the conversion, and then encounter a non-numeric or string end
(') ' to end the conversion and return the result. The parameter nptr string can contain a plus or minus
Number, decimal point, or E (e) to represent an exponential part, such as 123.456 or 123e-2.
The return value returns the number of converted floating-point types.
The additional description atof () is the same as the result of using Strtod (nptr, (char**) NULL).
Example/* Converts string A to string B into a number and adds a */
#include <stdlib.h>
Main ()
{
Char *a= "-100.23";
Char *b= "200e-2";
float C;
C=atof (a) +atof (b);
printf ("c=%.2f\n", c);
}
Executive c=-98.23
Atoi (Converts a string to an integer number)
Correlation function Atof,atol,atrtod,strtol,strtoul
Table header file #include <stdlib.h>
Defines the function int atoi (const char *nptr);
The function Description Atoi () scans the parameter nptr string and skips the preceding space character until it encounters a number
Character or sign to start the conversion, and then encounter a non-numeric or string end
(') ' to end the conversion and return the result.
The return value returns the number of integers after the conversion.
Additional instructions atoi () with the use of Strtol (nptr, (char**) null,10);
Example/* Converts string A to string B into a number and adds a */
#include <stdlib.h>
Mian ()
{
Char a[]= "-100";
Char b[]= "456";
int C;
C=atoi (a) +atoi (b);
printf (c=%d\n ", c);
}
Executive c=356
ATOL (Convert string to growth integer)
Correlation function Atof,atoi,strtod,strtol,strtoul
Table header file #include <stdlib.h>
Defines a function long atol (const char *nptr);
The function Description Atol () scans the parameter nptr string and skips the preceding space character until it encounters a number
Character or sign to start the conversion, and then encounter a non-numeric or string end
(') ' to end the conversion and return the result.
The return value returns the number of long integers after the conversion.
Additional Instructions Atol () with the use of Strtol (nptr, (char**) null,10);
Example/* Converts string A to string B into a number and adds a */
#include <stdlib.h>
Main ()
{
Char a[]= "1000000000";
Char b[]= "234567890";
Long C;
C=atol (a) +atol (b);
printf ("c=%d\n", c);
}
Executive c=1234567890
GCVT (Converts a floating-point number to a string, rounding)
Correlation function ecvt,fcvt,sprintf
Table header file #include <stdlib.h>
Defines the function char *gcvt (double number,size_t Ndigits,char *buf);
The function Description GCVT () is used to convert the parameter number to an ASCII string, and the parameter ndigits represents
The number of digits displayed. The difference between GCVT () and ECVT () and FCVT () is that the GCVT ()
The converted string contains either a decimal point or a positive or negative symbol. If the conversion succeeds, the converted Word
The character string is placed in the space indicated by the parameter buf pointer.
The return value returns a string pointer, which is the BUF pointer.
Additional Instructions
Example #include <stdlib.h>
Main ()
{
Double a=123.45;
Double b=-1234.56;
Char *ptr;
int decpt,sign;
GCVT (A,5,PTR);
printf ("A value=%s\n", PTR);
PTR=GCVT (B,6,PTR);
printf ("b value=%s\n", PTR);
}
Execute a value=123.45
b value=-1234.56
Strtod (Converts a string to a floating-point number)
Correlation function Atoi,atol,strtod,strtol,strtoul
Table header file #include <stdlib.h>
Defines the function double strtod (const char *nptr,char **endptr);
The function Description Strtod () scans the parameter nptr string, skips the preceding space character until it encounters
A number or sign is only started to convert to when a non-numeric or string end occurs
(') ' to end the conversion and return the result. If the endptr is not NULL, the event will be
The character pointer in the nptr terminated by the endptr is passed back. Parameter nptr Word
The symbol string can contain a sign, a decimal point, or E (e) to represent the exponential portion. such as 123.456 or
123e-2.
The return value returns the number of converted floating-point types.
Additional instructions refer to Atof ().
Example/* Converts the string a,b,c to a number, respectively, using the 10,2,16 binary */
#include <stdlib.h>
Mian ()
{
Char a[]= "1000000000";
Char b[]= "1000000000";
Char c[]= "FFFF";
printf ("a=%d\n", Strtod (a,null,10));
printf ("b=%d\n", Strtod (b,null,2));
printf ("c=%d\n", Strtod (c,null,16));
}
Executive a=1000000000
b=512
c=65535
Strtol (Convert string to growth integer)
Correlation function Atof,atoi,atol,strtod,strtoul
Table header file #include <stdlib.h>
Defines a function long int strtol (const char *nptr,char **endptr,int base);
The function Description Strtol () converts the parameter nptr string to the number of growth integers based on the parameter base. Parameter
The number base range is from 2 to 36, or 0. The parameter base represents the way in which it is used, such as base
A value of 10 is 10, and if the base value is 16, 16 is used. When the base value is 0
The conversion is made with 10, but it will use 16 if it encounters a pre-character such as ' 0x '.
Do the conversion. First Strtol () scans the parameter nptr string, skipping the preceding space
Character until a number or sign is encountered, and then a non-numeric or word
At the end of the string (' + ') to end the conversion and return the result. If the parameter endptr is not
NULL, the character pointer in the nptr that terminates when the condition is encountered is returned by Endptr
Back.
The return value returns the number of converted long integers, otherwise erange is returned and the error code is saved errno
In
Additional instructions Erange The specified conversion string is outside the legal range.
Example/* Converts the string a,b,c to a number, respectively, using the 10,2,16 binary */
#include <stdlib.h>
Main ()
{
Char a[]= "1000000000";
Char b[]= "1000000000";
Char c[]= "FFFF";
printf ("a=%d\n", Strtol (a,null,10));
printf ("b=%d\n", Strtol (b,null,2));
printf ("c=%d\n", Strtol (c,null,16));
}
Executive a=1000000000
b=512
c=65535
Strtoul (converts string to unsigned long integer)
Correlation function Atof,atoi,atol,strtod,strtol
Table header file #include <stdlib.h>
Defines a function unsigned long int strtoul (const char *nptr,char
**endptr,int base);

The function Description Strtoul () converts the parameter nptr string to an unsigned long integer based on the parameter base.
Type number. The parameter base range is from 2 to 36, or 0. The parameter base represents the binary
10 If base value is 10, and 16 if base value is 16
such as When the base value is 0, the conversion is made with 10, but encounters a pre-character such as ' 0x '
The conversion is made using the 16 binary. At first Strtoul () scans the parameter nptr string,
Skips the preceding space string until a number or sign is encountered to begin the conversion.
The conversion is again encountered when the non-numeric or string ends (' + ') and returns the result. If
Parameter endptr is not NULL, the characters in nptr that are terminated when they encounter a non-conditional
The pointer is returned by ENDPTR.
The return value returns the number of converted long integers, otherwise erange is returned and the error code is saved errno
In
Additional instructions Erange The specified conversion string is outside the legal range.
Example Reference Strtol ()
Toascii (converts an integer number to a valid ASCII code character)
Correlation function Isascii,toupper,tolower
Table header file #include <ctype.h>
define function int toascii (int c)
The function Description Toascii () converts the parameter C to a 7-bit unsigned char value, and the eighth bit is
Cleared, this character is then converted to ASCII characters.
The return value returns the converted ASCII character value for success.
Example #include <stdlib.h>
Main ()
{
int a=217;
Char b;
printf ("Before Toascii (): A value =%d (%c) \ n", a,a);
B=toascii (a);
printf ("After Toascii (): A value =%d (%c) \ n", b,b);
}
Execution before Toascii (): A value =217 ()
After Toascii (): A value =89 (Y)
ToLower (convert uppercase letters to lowercase)
Correlation function Isalpha,toupper
Table header file #include <stdlib.h>
Defines the function int tolower (int c);
Function description if the argument c is an uppercase letter, the corresponding lowercase letter is returned.
The return value returns the converted lowercase letter, and if no conversion is required, the parameter C value is returned.
Additional Instructions
Example/* Convert uppercase letters in s string to lowercase */
#include <ctype.h>
Main ()
{
Char s[]= "abcdefgh12345;! #$ ";
int i;
printf ("Before ToLower ():%s\n", s);
For (i=0;i<sizeof (s); i++)
S[i]=tolower (S[i]);
printf ("After ToLower ():%s\n", s);
}
Executive before ToLower (): abcdefgh12345;! #$
After ToLower (): abcdefgh12345;! #$
ToUpper (converts lowercase letters to uppercase letters)
Correlation function Isalpha,tolower
Table header file #include <ctype.h>
Defines the function int toupper (int c);
Function description if the argument c is lowercase, the uppercase letter of the pair is returned.
The return value returns the converted uppercase letter, and if no conversion is required, the parameter C value is returned.
Additional Instructions
Example/* Converts lowercase letters in s string to uppercase letters */
#include <ctype.h>
Main ()
{
Char s[]= "abcdefgh12345;! #$ ";
int i;
printf ("Before ToUpper ():%s\n", s);
For (i=0;i<sizeof (s); i++)
S[i]=toupper (S[i]);
printf ("After ToUpper ():%s\n", s);
}
Executive before ToUpper (): abcdefgh12345;! #$
After ToUpper (): abcdefgh12345;! #$

C Language Common data type collation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.