Math.pow(1.1, Day)

The language C notes

###A sample

#include  <stdio.h>
main ()
{
    printf("hello C.\n");
}

###Use GCC to Compile

gcc test1.c -o test1
./test1

###typed variables

  • type: int ,unsigned int , float , char
  • static , Once you declare the type of data , the type cannot be change later.

    int a = 1; float b = 0.2; // unsinged int // char

###typed funcitons and parameters

int func1( int x, int y)
{
    return 1;    
}

###Declare functions

Declare First (without curly braces), then call

int sum (int x, int y);

###Format Strings

In c, we cannot embeded variables directly inside to strings, we have to use a Format String with marker %i for the variables.

int num1 = 112
printf("num1 is : %i\n", num1);

here are all format makers:

Format markers
int %i / %d
unsigned int %u
float %f
char %c

###Type casting (converting type of data)

sometime we need to convert a variable form one type to another, the conversion is called casting.

Convert a float to an int will simply chop off the digits after decimal point, it will not Round number Up

float numF = 1.618;
int approxNumF = (int) numF ;

we can do the cast inside or outside the function instead of define a new variable.

int ret = funcX(A, (int)B);
float ret2 = (float) funX (a, b);

##Header files

we can declare function in header file and then implement it in implemention file

declaration file code:

// alex.h file
// declare func in here
int funcAdd (int A , int B);

implemention file code:

// alex.c file
// implement func in here
int funcAdd (int A , int B)
{
    return A + B ;
}

core file code:

// core.c
#include <stdio.h>
// important: just include header file , use source file to compile
#include "nice.h"

main ()
{
    int ret = funcAdd(5,6);
    printf("the ret is %i", ret);
}

let’s compile them:

// compile, change to the directory which contains these C files, and compile all C file like this:
// gcc core.c alex.c -o core

###structs

structrued groups of variables.

typedef struct {
    char name;
    int age;
} Human;
// like a javascript Object with two static property ?

// define a new structured type variable
Human Alex;

// we can assign a value to filed in a struct using dot syntax
Alex.name = 'alexander';
Alex.age = 18 ;

function can use structs as a input or output.

Human makeHuman (char name, int age);
void showHuman(Human theHuman);

// implemention
Human makeHuman(char name, int age) 
{
    Human aHuman ;
    aHuman.name = name ;
    aHuman.age = age ;
    return aHuman ;
}
void showHuman(Human theHuman)
{
    printf("theHuman name is : %c\n", name);
    printf("theHuman age is : %i\n", age);
}

// core 
main()
{
    Human aHuman = makeHuman("bill", 20);
    showHuman(aHuman);

    //specail syntax ,amazing
    Human bHuman = {"Jim", 19};
    showHuman(bHuman);
}

###enums

Group a series of related constants. cannot understand the benifit More. Orz

enum {
    NSAge =15,
    NSLong = 20 
}