---
title: "Programming C in Linux"
author: "asran"
date: "2010-12-22T13:28:47Z"
tags: []
categories: ["编程语言"]
slug: "programming-c-in-linux"
draft: false
lastmod: "2010-12-22T13:28:47Z"
id: 849263
url: "https://asran.iteye.com/blog/849263"
---

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\. &lt;math.h&gt; --- -lm

2\. &lt;mysql.h&gt; --- -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-&gt;0, 0-&gt;1 increase migration capability

&lt;&lt;:

1\. append 0 in the right side

&gt;&gt;:

1\. logical &gt;&gt;: append 0 in the left side

2\. arithmetial &gt;&gt;: append 1 in the left side if it's a negative

  append 0 if it's positive