Debug:
assert :
Evaluates an expression and when the
result is FALSE, prints a diagnostic
message and aborts the program.
#include <assert.h>
void
assert( int expression );
Example :
#include <assert.h>
void main()
{
int a;
a = 10 * 10;
assert(a
!= 100);
}
Output:
Assertion failed: a !=
100, file 1.cpp, line 8
abnormal program termination
Floating-point support:
modf :
Splits a floating-point value into
fractional and integer parts.
#include <math.h>
double
modf( double x, double *intptr
);
Example :
#include <math.h>
#include <stdio.h>
void main( void )
{
double x,
y, n;
x = -14.87654321;
y = modf( x, &n );
printf( "fraction : %f\ninteger : %.f\n", y, n );
}
Output :
fraction
: -0.876543
integer
: -14
Type transfer:
atoi :
Transfer a string into integer.
#include <stdlib.h>
int
atoi(const char *s);
Example :
#include <stdlib.h>
#include <stdio.h>
int
main(void)
{
int n;
char
*str = "12345.67";
n = atoi(str);
printf("string = %s
integer = %d\n", str, n);
return
0;
}
Output :
string = 12345.67 integer = 12345
itoa :
Transfer a int into string, there’s no itoa in Unix so we use sprintf
instead.
#include <stdlib.h>
char
*itoa(int value, char
*string, int radix);
Example :
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int number = 12345;
char
string[25];
itoa(number, string, 10);
printf("integer = %d
string = %s\n", number, string);
return 0;
}
Output :
integer = 12345 string = 12345
Input and output:
printf :
Print formatted output to the standard
output stream.
fprintf :
Print formatted data to a stream.
sprintf :
Write formatted data to a string.
#include <stdio.h>
int
printf( const char *format [, argument]... );
int
fprintf( FILE *stream, const char *format [, argument
]...);
int
sprintf( char *buffer, const char *format [,
argument] ... );
********************************************
scanf :
Read formatted data from the standard input
stream.
fscanf
: Read formatted data from a stream.
sscanf
: Read formatted data from a string.
#include <stdio.h>
int
scanf( const char *format [,argument]... );
int
fscanf( FILE *stream, const char *format [, argument
]... );
int
sscanf( const char *buffer, const char *format [,
argument ] ... );
********************************************
gets : Get a line from the stdin stream.
fgets
: Get a string from a stream.
#include <stdio.h>
char *gets( char *buffer );
char *fgets(
char *string, int n, FILE *stream );
NOTE :
(1) fgets 會保留換行字元。
(2) scanf後跟gets需要先用一次吃掉\n
Searching
and sorting:
qsort : Performs a quick sort.
#include
<stdlib.h>
void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )(const void *elem1, const void *elem2 ) );
int compare (const void *
elem1, const void * elem2 );
The function should receive two parameters (elem1 and elem2) that are pointers to elements, and should return an int value with the result of comparing them:
return value |
description |
<0 |
*elem1 goes
before *elem2 |
0 |
*elem1 ==
*elem2 |
>0 |
*elem1 goes
after *elem2 |
Example :
#include
<stdlib.h>
#include
<stdio.h>
int
compare(const void *a, const void *b)
{
int *x =
(int *)a;
int *y =
(int *)b;
if (*x > *y)
return 1;
if (*x == *y)
return 0;
return -1;
}
void
main()
{
int ele[8] = {3, 7, 5, 8, 1, 2, 4, 6};
qsort(ele, 8, sizeof(int), compare);
for (int
i = 0; i < 8; i++)
printf("%d ", ele[i]);
}
Output :
1
2 3 4 5 6 7 8
String
manipulation(in C):
strcat : Append a string.
#include
<string.h>
char *strcat( char *strDestination,
const char *strSource );
Example :
#include
<string.h>
#include
<stdio.h>
void
main()
{
char s[100] = "Hello!";
strcat(s,
" World!");
printf("%s",
s);
}
Output :
Hello!
World!
********************************************
strchr : Find a character in a string.
#include
<string.h>
char *strchr( const char *string, int c
);
Example :
#include
<string.h>
#include
<stdio.h>
void
main()
{
char *s = "This is a
sample!";
char *p1 = strchr(s,
's');
char *p2 = strchr(p1
+ 1, 's');
printf("%s\n%s\n%s", s, p1, p2);
}
Output :
This
is a sample!
s
is a sample!
s
a sample!
********************************************
strcmp : Compare strings.
#include
<string.h>
int strcmp(
const char *string1, const char *string2 );
Example :
#include
<string.h>
#include
<stdio.h>
void
main()
{
char *s1 = "This is a
sample";
char *s2 = "This is another
sample";
int
result = strcmp(s1, s2);
if (result < 0)
printf("s1 < s2");
if (result == 0)
printf("s1 == s2");
if (result > 0)
printf("s1 > s2");
}
Output :
s1
< s2
********************************************
strcpy : Copy a string.
#include
<string.h>
char *strcpy( char *strDestination,
const char *strSource );
Example :
#include
<string.h>
#include
<stdio.h>
void
main()
{
char s1[80] = "This is a
sample";
char s2[80];
strcpy(s2,
s1);
printf("s1
= %s\ns2 = %s", s1, s2);
}
Output :
s1
= This is a sample
s2
= This is a sample
********************************************
strlen : Get the length of a string.
#include
<string.h>
size_t strlen(
const char *string );
Example :
#include
<string.h>
#include
<stdio.h>
void
main()
{
char s1[80] = "This is a
sample";
int len = strlen(s1);
printf("s1
= %s\nlength of s1 = %d", s1, len);
}
Output :
s1
= This is a sample
length
of s1 = 16
********************************************
strstr : Find a substring.
#include
<string.h>
char *strstr( const char *string, const char *strCharSet
);
Example :
#include
<string.h>
#include
<stdio.h>
void
main()
{
char *s = "This is a
sample";
char *sub = "is a";
char *alt = "The";
char *sp = strstr(s,
sub);
char *ap
= strstr(s, alt);
printf("find
%s : %s\n", sub, sp);
printf("find
%s : %s\n", alt, ap);
}
Output :
find
is a : is a sample
find
The : (null)
********************************************
strtok : Find the next token in a
string.
#include
<string.h>
char *strtok( char *strToken, const
char *strDelimit );
Example :
#include
<string.h>
#include
<stdio.h>
void
main()
{
char *s = "This\tis a sample";
char *token;
token = strtok(s,
" \t");
while (token != NULL)
{
printf("%s\n", token);
token = strtok(NULL, " \t");
}
}
Output :
This
is
a
sample
********************************************
_strlwr : Convert a string to lowercase.
#include
<string.h>
char *_strlwr( char *string );
Example :
#include
<string.h>
#include
<stdio.h>
void
main()
{
char *s = "THIS IS A
SAMPLE";
printf("%s\n",
s);
_strlwr(s);
printf("%s\n",
s);
}
Output :
THIS
IS A SAMPLE
this
is a sample
********************************************
_strupr : Convert a string to uppercase.
#include
<string.h>
char *_strupr( char *string );
Example :
#include
<string.h>
#include
<stdio.h>
void
main()
{
char *s = "This is a
sample";
printf("%s\n",
s);
_strupr(s);
printf("%s\n",
s);
}
Output :
This
is a sample
THIS
IS A SAMPLE
********************************************