kenics.net

Technical notes on perl, python, php, sql, cgi, c/c++, q/kdb+, unix/shell, revision control tools, data structures & algorithms, and their applications into web services and other various forms of software engineering.

The C standard library

############################### 
####  C standard library   #### 
############################### 
 
 
<assert.h> 
<ctype.h> 
<errno.h> 
<float.h> 
<limits.h> 
<locale.h> 
<math.h> 
<setjmp.h> 
<signal.h> 
<stdarg.h> 
<stddef.h> 
<stdio.h> 
<stdlib.h> 
<string.h> 
<time.h> 
 
 
(ref) 
http://www.tutorialspoint.com/c_standard_library/ 
 
 
 
######################### 
####    <math.h>     #### 
######################### 
 
 
#include <math.h> 
 
(ref) 
http://www.tutorialspoint.com/c_standard_library/math_h.htm 
 
e.g. 
 
int ceil(double x);             // smallest integer bigger than x 
int floor(double x);            // biggest integer smaller than x 
double fabs(double x);          // |x| 
double exp(double x);           // e^x 
double pow(double x, double y); // x^y 
double sqrt(double x);          // square root of x 
double log(double x);           // natural log(x) 
double log10(double x);         // base-10 logarithm of x 
double sin(double x);           // returns the sine of radian angle x 
double cos(double x); 
double tan(double x); 
 
 
NOTE: some math operations are in <stdlib.h> 
 
int abs(int n); 
div_t div(int num, int denom);  // numerator/denominator 
                                // div_t is a struct containing two ints; quot, rem 
                                // div_t result = div(5,3); 
                                // printf("%d %d", result.quot, result.rem); 
                                // quotient and remainder 
int atoi(const char* str);      // simple, you can write it yourself. 
double atof(const char* str); 
long int atol(const char* str); 
 
 
 
## 
##  atoi()  example 
## 
 
------------------------- // atoi.c 
 
//  it's all about ascii 
//  (ref) http://www.asciitable.com/ 
 
#include <stdio.h> 
 
int custom_atoi(const char* str) 

  int i; 
  int ret = 0; 
  for(i = 0; str[i] != 0 ; i++) 
    ret = ret * 10 + (str[i] - 48);  // can replace 48 with '0' 
  return ret; 

 
int main() 

  int result = custom_atoi("1234"); 
 
  printf("%d",result); 

 
------------------------- 
 
 
 
######################## 
####    <time.h>    #### 
######################## 
 
 
#include <time.h> 
 
time_t time(time_t* t);        // returns cumulative seconds since 19700101.000000 
char* ctime(const time_t* t);  // 
 
 
------------------------------ 
 
#include <stdio.h> 
#include <time.h> 
 
int main() 

 
  time_t total_sec; 
  total_sec = time(NULL);             // same as  time(&total_sec); 
 
  char* cal_time = ctime(&total_sec); // returns calendar time in char* 
 
  printf("%s",cal_time);              // Wed Jul  9 22:52:01 2014 
 

 
------------------------------ 
 
 

  1. 2014-07-09 23:21:41 |
  2. Category : unix
  3. Page View:

Google Ads