Common functions of C language

Source: Internet
Author: User
Tags floor function mathematical functions

1.i/o function

(1) scanf function:

int scanf (const char *format ...);

Writes the data to the parameter table in formatted format from the standard input stream stdin, returns the number of arguments written to the parameter table if the operation succeeds, or EOF;

Note the following points:

①scanf function has no precision control

As appears:

scanf ("%6.2f", &a);

is absolutely wrong.

② all inputs are valid characters when entering character data in%c format

Such as:

scanf ("%c%c%c", &a,&b,&c);

Input: D e F

Then a= ' d ', B is the space character ', c= ' d '

If the string is read in%s, the SPACEBAR and the ENTER key can be used as the end of the input flag

Such as:

scanf ("%s%s", p,q),---------p,q are pointers

Input: Hello World and

Hello

World

The effect is the same, p all points to the Hello string, and Q all points to the world string

③ If there are non-formatted characters in the format control string, enter them as they are;

Such as:

scanf ("%d,%d,%d,", &,&b,&c);

The input must be preceded by a comma: 5,6,7

④ If you are using the same while, be careful to remove the ENTER key

If you have the following procedure:

while (scanf ("%d", &n) ==1&&n!=0)

{

Char ch;

scanf ("%c", &ch);

printf ("%c\n", ch);

}

The output is:

2                                                                                                                                                                                   

3

0

The result shows that the CH character is the ENTER key

So if you want to eliminate the impact:

There are two ways of doing this:

1) plus getchar ();

while (scanf ("%d", &n) ==1&&n!=0)

{

GetChar ();

Char ch;

scanf ("%c", &ch);

printf ("%c\n", ch);

}

2) Add%*c in while

while (scanf ("%d%*c", &n) ==1&&n!=0)

2. Mathematical functions

(1) Seeking absolute value

Double fabs (double);

float FABSF (float);

A long double fabsl (long double);

int abs (int);

Long int Labs (long int);

(2) Floor function: Returns an integer that is not greater than the argument

Float floor (float);

Double floor (double);

(3) Ceil function: Returns an integer not less than the parameter

float ceil (float);

Double floor (double);

(4) POW: return to x^y;

Double pow (double x,double y);

(5) Sqrt: Return the root of X

Double sqrt (double x);

(6) Log2,log10: Returns the natural value of the base

Double log2 (double x);

Double log10 (double x);

3. String handling functions

(1) Char *strchr (const char *str,int ch);

Returns the position pointer of the first occurrence of the character ch in str, otherwise returns null;

(2) Char *strstr (const char *str1,const char *STR2);

Returns the position pointer of the first occurrence of the str2 in str1, otherwise returns null;

(3) int stricmp (const char *str1,const char *STR2);

Ignore the case of characters to compare

(4) int strncmp (const char *str1,const char *str2,int count);

Compares the first n characters of a str1 to the first n characters of a str2

(5) Char *strncpy (char *str1,const char *str2,int count);

Copy the first n characters of a str2 into a str1

(6) Char *strrev (char *str);

Reverse the string str and return the inverted result

4. Commonly used functions

(1) Double atof (const char *STR);

Converts the number of string str representation to floating-point numbers

(2) int atoi (const char *STR);

Converts the number represented by the string str to an integer

(3) void qsort (void *buf,int count,int size,cmp); ———— >count the number of elements to be ordered, size is the space occupied by each element

Quick Sort

Common functions of C language

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.