Library Functions Examples

advertisement

Partial C/C++ Function List

Functions Listed Alphabetically abort()

Prototype: void abort(void);

Header File: stdlib.h

Explanation: Similar to exit, it will return exit code three to the operating system, and then abort the program.

Example:

//Example aborts the program

#include <stdlib.h> int main()

{ abort();

} abs()

Prototype: int abs(int aNum);

Header File: math.h and stdlib.h

Explanation: The abs function returns the absolute value of a number (makes it positive).

Example:

//Program asks for user input of an integer

//It will convert it to positive if it was negative

#include <math.h>

#include <iostream.h> int main()

{ int aNum; cout<<"Enter a positive or negative integer"; cin>>aNum; cout<<abs(aNum); return 0;

} acos()

Prototype: double acos(double a_cos);

Header File: math.h

Explanation: Acos is used to find the arccosine of a number (give it a cosine value and it will return the angle, in radians corresponding to that value). It must be passed an argument between -1 and 1.

//Example gives the angle corresponding to cosine .5

#include <iostream.h>

#include <math.h> int main()

{ cout<<acos(.5); return 0;

} asin()

Prototype: double acos(double a_cos);

Header File: math.h

Explanation: Acos is used to find the arccosine of a number (give it a cosine value and it will return the angle, in radians corresponding to that value). It must be passed an argument between -1 and 1.

//Example gives the angle corresponding to cosine .5

#include <iostream.h>

#include <math.h> int main()

{

cout<<acos(.5); return 0;

} atan()

Prototype: double asin(double a_sin);

Header File: math.h

Explanation: Asin is used to find the arcsine of a number (give it a sin value and it will return the angle, in radians corresponding to that value). It must be passed an argument between -1 and 1.

//Example gives the angle corresponding to sine 1

#include <iostream.h>

#include <math.h> int main()

{ cout<<asin(1); return 0;

} atexit()

Prototype: double atan(double a_tan);

Header File: math.h

Explanation: Acos is used to find the arctangent of a number (give it a tangent value and it will return the angle, in radians corresponding to that value). It accepts all real numbers.

//Example gives the angle corresponding to tangent 10

#include <iostream.h>

#include <math.h> int main()

{ cout<<atan(10); return 0;

} atof()

Prototype: float atof(const char *string);

Header File: stdlib.h

Explanation: This function accepts a string and converts it into a floating point number. For example, if "1234.34" is passed into the function, it will return 1234.34. If the string contains a decimal place, the number will not be truncated. If a valid number is contained in the string, but is terminated by a non-valid character, the number will be returned, and the non-valid character will be ignored

Example:

//Example reads in a string, and converts it to an float

#include <stdlib.h>

#include <iostream.h> int main()

{ char a_char[10]; cin>>a_char; cout<<"As a float: "<<atof(a_char); return 0;

} atoi()

Prototype: int atoi(const char *string);

Header File: stdlib.h

Explanation: This function accepts a string and converts it into an integer. For example, if "1234" is passed into the function, it will return 1234, an integer. If the string contains a decimal place, the number will be truncated. Eg,

"104.21" will be returned as 104.

//Example reads in a string, and converts it to an integer

#include <stdlib.h>

#include <iostream.h>

int main()

{ char a_char[10]; cin>>a_char; cout<<"As an integer: "<<atoi(a_char); return 0;

} atol()

Prototype: long atol(const char *string);

Header File: cstdlib or (stdlib.h for C)

Explanation: This function accepts a string and converts it into a long. For example, if "1234" is passed into the function, it will return 1234. It is similar to atoi, except that it can handle larger numbers (up to the maximum size of a long). If the string contains a decimal place, the number will be truncated. Eg, "104.21" will be returned as 104.

//Example reads in a string, and converts it to a long

#include <stdlib.h>

#include <iostream> using namespace std; int main()

{ char a_char[10]; cin>>a_char; cout<<"As a long: "<<atol(a_char); return 0;

} clock()

Prototype: clock_t clock(void);

Header File: time.h

Explanation: This function returns the number of clock ticks (the CPU time taken) the program has taken. To convert to the number of seconds, divide by CLOCKS_PER_SEC, which is defined in time.h

//Example will run a loop, and then print the number of clock ticks and

//number of seconds used

#include <time.h>

#include <iostream.h> int main()

{ for(int x=0; x<1000; x++) cout<<endl; cout<<"Clock ticks: "<<clock()<<" Seconds: "<<clock()/CLOCKS_PER_SEC; return 0;

} cosh()

Prototype: double cosh (double a);

Header File: math.h

Explanation: Returns the hyperbolic cosine of a.

//Example prints the hyperbolic cosine of .5

#include <math.h>

#include <iostream.h> int main()

{ cout<<cosh(.5); return 0;

} ctime()

Prototype: char *ctime(const time_t *time)

Header File: time.h

ANSI: C and C++

Explanation: Use ctime to convert a time_t variable into a string of 26 characters in the format Fri Jan 28 09:12:21

2000\n\0 where \n and \0 are the newline and terminating null character. The string it returns will be overwritten unless you copy the string to another variable before calling ctime again.

//Example:

//Program uses ctime to convert a time_t variable into a human readable

//string in form Fri Jan 28 00:00:00 2000\n\0

#include <time.h>

#include <iostream.h> int main()

{ time_t hold_time; hold_time=time(NULL); cout<<"The date is: "<<ctime(&hold_time);

} div()

Prototype: div_t div(int numerator, int denominator);

Header File: stdlib.h

Explanation: Div takes a fraction (numerator/denominator) and places the quotient andthe remainder into the struct it returns, div_t. In div_t the two ints are quot and rem, quotient and remainder.

//Example shows division

#include <stdlib.h>

#include <iostream.h> int main()

{ div_t answer; answer = div(76, 5) cout<<"As an improper fraction, 76/5"; cout<<"As a proper fraction, "<<div_t.quot<<" "<< div_t.rem<<"/5"; return 0;

} exit()

Prototype: void exit(int ExitCode);

Header File: stdlib.h and process.h

Explanation: Exit ends the program. The ExitCode is returned to the operating system, similar to returning a value to int main.

//Program exits itself

//Note that the example would terminate anyway

#include <stdlib.h>

#include <iostream.h> int main()

{ cout<<"Program will exit"; exit(0) //Return 0 is not needed, this takes its place

} fabs()

Prototype: double fabs(double number);

Header File: math.h

Explanation: This function returns the absolute value of a number. It will not truncate the decimal like abs() will, so it is better for certain calculations.

//Example outputs absolute value of -12.3 with fabs and abs

#include <math.h>

#include <iostream.h>

int main()

{ cout<<"Abs(-12.3)="<<abs(-12.3)<<endl; cout<<"Fabs(-12.3)="<<fabs(-12.3); return 0;

} floor()

Prototype: double floor(double Value);

Header File: math.h

Explanation: Returns the largest interger value smaller than or equal to Value. (Rounds down)

//Example will output 5.9 rounded down

#include <math.h>

#include <iostream.h> int main()

{ cout<<"5.9 rounded down: "<<floor(5.9); return 0;

} fmod()

Prototype: double fmod(double numone, double numtwo);

Header File: math.h

Explanation: This function is the same as the modulus operator. It returns the remainder of the division numone/numtwo. I include it to avoid it being confused with modf, a previous function of the day.

//Example will output remainder of 12/5 (2)

#include <math.h>

#include <iostream.h> int main()

{ cout<<"The remainder of 12/5: "<<fmod(12/5); return 0;

} getchar()

Prototype: int getchar(void);

Header File: stdio.h

Explanation: This function reads in a character. It returns the character as the ASCII value of that character. This function will wait for a key to be pressed before continuing with the program.

//Example waits for a character input, then outputs the character

#include <stdio.h>

#include <iostream.h> int main()

{ char a_char; a_char=getchar(); cout<<a_char; return 0;

} getenv()

Prototype: char *getenv(const char *atypeofinformation);

Header File: stdlib.h

Explanation: Getenv wll return a pointer to a string that contains system information pertaining to atypeofinformation. For example, path names, or devices. This is implementation-defined, so you might need to check

//Example shows the currently defined paths (usually set in autoexec.bat)

#include <iostream.h>

#include <stdlib.h> int main()

{ cout<<getenv("PATH"); return 0;

} isalnum()

Prototype: int isalnum(int ch);

Header File: ctype.h

Explanation: Isalnum does exactly what is sounds like, checks if the ASCII value passed in has a character equivalent to a number of letter. It returns non-zero for true, and zero for false.

//Example reads in a character and checks if it is alpha-numeric

#include <iostream.h>

#include <ctype.h> int main()

{ char x; cin>>x; if(isalnum(x)) cout<<"It's alpha-numeric"; else cout<<"It's not alpha-numeric; return 0;

} isalpha()

Prototype: int isalpha(int character);

Header File: ctype.h

Explanation: Even though it accepts an int, it really deals with characters. Based on the ASCII value of the character it will determine if it is an alphabetical character or not.

//Program accepts a user input, and checks to see if it is a letter

#include <stdlib.h>

#include <iostream.h> int main()

{ char d; cout<<"Enter a character, number, or punction sign: "; cin>d; if(isalpha(d)) cout<<"You entered an alphabetical character."; return 0;

} isdigit()

Prototype: int isdigit(int Character);

Header File: ctype.h

Explanation: This function accepts an ASCII value, and returns whether or not it is a digit (0 to 9) when converted to its equivalent ASCII character. It returns a zero if it is not a digit, and non-zero if it is.

//Example reads in a character and checks to see if it is a digit

#include <ctype.h>

#include <iostream.h> int main()

{ char a_char; cin>>a_char; if(isdigit(a_char)) cout<<"Is a digit!"; else cout<<"Is not a digit!"; return 0;

}

ispunct()

Prototype: int ispunct (int c);

Header File: ctype.h

Explanation: Tests if the ASCII character corresponding to the integer stored in c is a punctuation character.

//Example reads in a character and tests if it punctuation int main()

{ char x; cin>>x; if(isspunct(x)) cout<<"Punctuation"; else cout<<"Not punctuation."; return 0;

} isspace()

Prototype: int isspace(int ch);

Header File: ctype.h

Explanation: Isspace does exactly what is sounds like, checks if the ASCII value passed in is that of a space key

(such as tab, space, newline, etc). It returns non-zero for true, and zero for false.

//Example reads in a character and checks if it is a type of space

#include <iostream.h>

#include <ctype.h> int main()

{ char x; cin>>x; if(isspace(x)) cout<<"It's a space"; else cout<<"It's not a space; return 0;

} supper()

Prototype: int isupper (int c);

Header File: ctype.h

Explanation: Tests to see if the ASCII character associated with the integer c is an uppercase letter.

//Example reads in a character and tests if it uppercase int main()

{ char x; cin>>x; if(isupper(x)) cout<<"Uppercase"; else cout<<"Not uppercase."; return 0;

} kbhit()

Prototype: int kbhit(void);

Header File: conio.h

Explanation: This function is not defined as part of the ANSI C/C++ standard. It is generally used by Borland's family of compilers. It returns a non-zero integer if a key is in the keyboard buffer. It will not wait for a key to be pressed.

Example:

//Example will loop until a key is pressed

//Example will not work with all compilers

#include <conio.h>

#include <iostream.h> int main()

{ while(1)

{ if(kbhit()) break;

} return 0;

} log10()

Prototype: double log10(double anumber);

Header File: math.h

Explanation: Log10 returns the logarithm for anumber, as long as it is not negative, or zero. To find the logarith of another base, use log base 10 of the number divided by log base 10 of the base to find the logarithm for.

//Example gives the log base ten of 100

#include <iostream.h>

#include <math.h> int main()

{ cout<<log10(100); return 0;

} log2()

Prototype: double log2(double anumber);

Header File: math.h

Explanation: Log2 returns the base 2 logarithm for anumber, as long as it is not negative, or zero.

//Example gives the log base 2 of 32

#include <iostream.h>

#include <math.h> int main()

{ cout<<log2(32); return 0;

} log()

Prototype: double log(double anumber);

Header File: math.h

Explanation: Log returns the natural logarithm for anumber, as long as it is not negative, or zero. .

//Example gives the natural logarithm of 100

#include <iostream.h>

#include <math.h> int main()

{ cout<<log(100); return 0;

} memcmp()

Prototype: int memcmp(const void *buffer1, const void *buffer2, size_t count);

Header File: string.h

Explanation: Alphabetically compares two arrays passed in to it. The fact that it is a void * simply means that it can have non-character arrays passed in to it. It is also necessary to pass how far to compare (ie, the size of the arrays)

The return value is:

Less than zero buffer1 is less than buffer2

Zero buffer1 is equal to buffer2

Greater than zero buffer1 is greater than buffer2

Example:

//Program compares two character arrays

//Program compares only to eight letters

#include <stdlib.h>

#include <iostream.h> int main()

{ int comp_val=memcmp("Andersronc", "Zandimva", 8); if(comp_val==0) cout<<"strings are equal"; if(comp_val<0) cout<<"String one is alphabetically equal"; else cout<<"String one is alphabetically greater"; return 0;

} modf()

Prototype: double modf(double number, double *intpart);

Header File: math.h

Explanation: This function will return the decimal part of number, and it will place the integer part of number into the variable pointed to by intpart.

//Example will output decimal and integer parts of 12.34

#include <math.h>

#include <iostream.h> int main()

{ double x; modf(12.34, &x); //Needed to set x (otherwise it will not set x

//if used called in the output statement cout<<"Decimal: "<<modf(12.34, &x)<<"Fractional: "<<x; return 0;

} pow()

Prototype: double pow(double b, double p);

Header File: math.h

Explanation: This function raises b to the p power.

//Example will computer 4 to the 5th

#include <math.h>

#include <iostream.h> int main()

{ cout<<"4 to the 5th power is: "<<pow(4, 5); return 0;

} putchar()

Prototype: int putchar(int achar);

Header File: stdio.h

Explanation: Putchar writes the character corresponding to the ASCII value of achar to stdout, which is usually the monitor. It returns achar.

//Example outputs the character corresponding to the ASCII value 65 int main()

{ putchar(65); return 0;

} putenv()

Prototype: int putenv(const char *asetting);

Header File: stdlib.h

Explanation: Use putenv to modify the environmental settings for the program. It should be used in the form:

TYPE_OF_SETTING (such as PATH, DEVICE, etc.)=WHAT_TO_SET_TO.

//Example sets the defined paths for the program to C:\

//Example then lists the paths

#include <iostream.h>

#include <stdlib.h> int main()

{ putenv("PATH=C:\"); cout<<getenv("PATH"); return 0;

} puts()

Prototype: int puts(const char *astring);

Header File: stdio.h

Explanation: This function, puts, will output the string pointed to by astring. It will then add a newline character. This is built in to the function. It returns a nonnegative integer if it was successful. It returns EOF if there was an error.

//Example outputs "Hello, World" with a newline

#include <stdio.h> int main()

{ puts("Hello World"); return 0;

} remove()

Prototype: int remove(const char *filename);

Header File: stdio.h

Explanation: Remove will erase a file specified by filename. Upon success it will return zero, upon failure it will return nonzero.

//Example will erase useless.txt, in the current directory

//It is recommend that useless.txt be...useless

#include <stdio.h> int main()

{ if(!remove("useless.txt")) cout<<"Successful deletion"; else cout<<"Oops, file not deleted (is it there?)"; return 0;

} rename()

Prototype: int rename(const char *afilename, const char *newname);

Header File: stdio.h

Explanation: This function will rename a file of name afilename, to whatever is newname.

//Example requires example.txt to be in working directory

//Example renames example.txt to newname.txt

#include <stdio.h> int main()

{ rename("example.txt", "newname.txt"); return 0;

} srand()

Prototype: void srand(unsigned int seed);

Header File: stdlib.h

Explanation: Srand will seed the random number generator to prevent random numbers from being the same every time the program is executed and to allow more pseudorandomness.

//Program uses time function to seed random number generator

//and then generates random number

#include <stdlib.h>

#include <time.h>

#include <iostream.h> int main()

{ srand((unsigned)time(NULL)); //Casts time's return to unsigned int d=rand()%12; //Should be more random now cout<<d; return 0;

} strcat()

Prototype: char *strcat(char *Destination, char *Source);

Header File: string.h

Explanation: This function will concatenate (add to the end) the string pointed to by source on to the string pointed to by Destination. Returns a pointer to the Destination string.

//Example concatenates two strings to output

//By asking the user for input

#include <string.h>

#include <iostream.h> int main()

{ char *s1 = new char[30]; char *s2 = new char[30]; cout<<"Enter string one(without spaces): "; cin>s1; cout<<"Enter string two(without spaces): "; cin>s2; cout<<strcat(s1, s2); return 0;

} strcmp()

Prototype: int strcmp (const char *string1, const char *string2);

Header File: string.h

Explanation: Tests the strings for equality. Returns a negative number if string1 is less than string2, returns zero if the two strings are equal, and returns a positive number is string1 is greater than string2

//Example reads in two strings (w/out spaces) and compares them for equality

#include <string.h>

#include <iostream.h> int main()

{ char *str1=new char[20]; char *str2=new char[20]; cin>>str1; cin>>str2; if(!strcmp(str1, str2) cout<<"Strings are equal!"; return 0;

} strerror()

Prototype: char *strerror(int errnum);

Header File: string.h

Explanation: Strerror returns a pointer to a string that contains the identification for an error-number. This is usually used with a function that returns an error number based on the result of an operation. It is a very bad idea to start modifying the string it returns.

//Example prints out error number 2 #include <iostream.h> #include <string.h> int main() { cout<<strerror(2); return

0; }

time()

Prototype: time_t time(time_t* timer);

Header File: time.h

ANSI: C and C++

Explanation: Returns and sets the passed in variable to the number of seconds that have passed since 00:00:00

GMT January 1, 1970. If NULL is passed in, it will work as if it accepted nothing and return the same value.

Example:

//Program will use time_t to store number of seconds since 00:00:00 GMT Jan.

//1, 1970

#include <time.h>

#include <iostream.h> int main()

{ time_t hold_time; hold_time=time(NULL); cout<<"The number of elapsed seconds since Jan. 1, 1970 is "<<hold_time; return 0;

} tolower()

Prototype: int tolower(int chr);

Header File: ctype.h

Explanation: Tolower will return the ASCII value for the lowercase equivalent of the ASCII character chr, if it is a letter. If the character is already lowercase, it remains lowercase

//Example reads in a character and makes up lowercase

#include <iostream.h>

#include <ctype.h> int main()

{ char x; cin>>x; x=tolower(x); cout<<x; return 0;

} toupper()

Prototype: int toupper(int aChar);

Header File: ctype.h and stdlib.h

Explanation: toupper accepts a character as an argument (it actually accepts an integer, but the two are interchangeable) and will convert it to uppercase, and will return the uppercase character, in the form of an ASCII integer, and leave the parameter unchanged.

//Program creates char d, sets it equal to lowercase letter

//Converts it to uppercase and outputs it

#include <ctype.h>

#include <iostream.h> int main()

{ char d='a'; d=toupper(d); cout<<d; return 0;

}

Download