Programming C in Linux

0. install compiler and associate environment.

sudo apt-get install build-essential

or install with CD(without internet):

sudo apt-cdrom add

sudo aptitude update

sudo aptitude install build-essential

gcc -v

 

1. The printf("Content-type:text/plain\n\n"); is mandatory for cgi program, otherwise the first part of content won't display in browser.

 

2. int main(int argc, char *argv[], char *env[]) {}

    argc ##the count of arguments

    argv[] ## argument array

    env[] ## environment variables array

 

3. #define STRINGSIZE 256

   not #DEFINE, should be lowercase in Ubuntu cc.

 

4. char=1 byte, int=4 bytes, float=4 bytes

Each data type has a specified size and the sizeof() library function will return this as an integer.

 

5. %c ## character

   %d ## integer

   %s ## string

   %f ## floating pointer number

 

6. compile a set of c file

create a file named Makefile, and write as below:

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

#Makefile

all:chap1

chap1:1-1       1-2     1-3     1-4

1-1:

        cc -o hello chapter1_1.c

1-2:

        cc -o args chapter1_2.c

1-3:

        cc -o args.cgi chapter1_3.c

1-4:

        cc -o env.cgi chapter1_4.c

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

note: the tab after each make target is vital to the syntax of make.

 

7. string.h

strtok

atoi

strcpy

getenv

 

8. sprintf

char SQL[1024] = "";

sprintf(SQL, "insert into CIL values('%s', '%s')", argv[1], argv[2]);

 

 

9. putchar / getchar

10. printf / scanf

%[m/-m]d

%ld ## long int

%o ## octal

%x ## hex

%u ## unsigned

%m.ns ## capture the left n characters, hold m columns, align right

%.ns ## m=n

%m.nf ## n is decimal

%m.ne ## index

 

11. real number (float, double)

1.0f ## float

compile:

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

1. <math.h> --- -lm

2. <mysql.h> --- -lmysqlclient

 

 

FILE

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

fopen:

File *fp = fopen("filename", "openType");

exception: NULL(NULL == 0)

openType:

r ## file must exsit

w ## if file exsits, delete it first, and create a new one

a ## file must exsit

rb

wb

ab

r+ ## file must exsit

w+

a+ ## file must exsit

rb+

wb+

ab+

if ((fp=fopen("filename", "r")) == NULL) {

printf("error open file.");

exit(0);

}

note: exit(0); will close file, and halt process.

 

fclose:

fclose(fp);

success: return 0

failure: return EOF(-1)

 

fputc/fgetc/putc/getc/feof:

fputc(ch, fp);

success: return ch

failure: return EOF(-1)

note: #define putchar(ch) fputc(ch, stdout)

-----

fgetc(fp);

success: return ch

failure: return EOF(-1)

-----

feof(fp);

1 : end of file

0 : not yet

-----

#define putc(ch, fp) fputc(ch, fp)

#define getc(fp) fgetc(fp)

-----

fread/fwrite:

fread(buffer, size, count, fp);

fwrite(buffer, size, count, fp);

buffer - pointer of location

size - read/write how many bytes one time

count - how many times

fp - FILE pointer

-----

fprintf/fscanf:

fprintf(fp, "format", i, j, k, ...);

fscanf(fp, "format", &i, &j, &k,...);

-----

putw/getw: (w = 2 bytes)

putw(10, fp);

-----

fputs/fgets:

fputs("China", fp); ## the first argument can be literal, char[], char *

success: return 0

failure: return EOF(-1)

 

fgets(str, n, fp);

str - string array

n - array size, (n-1)char + '\0'

fp - FILE pointer

return first address of string array.

 

rewind/fseek/ftell:

rewind(fp);

fseek(fp, offset, direction);

direction - 0 ## header

 - 1 ## current

 - 2 ## end

int i = ftell(fp); ## the current offset of pointer

success: return i

failure: -1L

 

ferror/clearerr:

ferror(fp);

no error: return 0

has error: return non 0

 

clearerr(fp); ## set the ferror=0

 

 

Struct/Union

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

.has highest priority

 

 

Bit Operation

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

&:

1. reset to 0;

2. obtain specified bits

3. only remain specified bits

|:

1. set specified bits to 1

^:

1. swap two int without "middle temp variable"

2. reverse the specified bits

3. a ^ 0 = a

~:

1. 1->0, 0->1 increase migration capability

<<:

1. append 0 in the right side

>>:

1. logical >>: append 0 in the left side

2. arithmetial >>: append 1 in the left side if it's a negative

  append 0 if it's positive

   

你可能感兴趣的:(C++,c,linux,C#,FP)