Programming Reference

Programming Reference

Programming Reference

Handbook

C Users Ian Desktop computer programming jpg

Linux 指令參考

 ls – list direction contents

 Synopsis : ls [option]…[file]…

 Option

 –l : use a long listing format

 –a : do not ignore entries starting with .

 cd – change directory

 Synopsis : cd [directory]

 pwd – print name of current/working directory

 Synopsis : pwd [option]

 Option

 -L : use PWD from environment, even it contains symlinks

 -P : avoid all symlinks

 clear – clear terminal screen

 Synopsis : clear

 mkdir – make directories

 Synopsis : mkdir [option]…[directory]…

 Option

 -p : no error if existing, make parent directories as needed

 rmdir – remove empty directories

 Synopsis : rmdir [option]…[directory]…

 Option

 -p : remove DIRECTORY and its ancestors

 rm – remove files or directories

 Synopsis : rm [option]…[file]…

 Option

 -f : ignore nonexistent files, never prompt

 -r : remove directories and their contents recursively

 cp – copy files and directories

 Synopsis : cp [option]…[source]…[destination]

 Option

 -r : copy directories recursively

 -f : if an existing destination file cannot be opened, remove it and try again

 -l : link files instead of copying

 mv – move (rename) files

 Synopsis : mv [option]…[source]…[destination]

 Option

 -f : do not prompt before overwrite

 -n : do not overwrite an existing file

 -u : move only when the SOURCE file is newer than the DESTINATION file

 vi – a programmers text editor

 Synopsis : vi [file]

 gedit – text editor for GNOME Desktop

 Synopsis : gedit [file]

 [Ctrl] + [c] – force to break off the program which is running

 [Tab] – complete instruction / complete file

 [↑] / [↓] – search for previous instruction or next instruction

 man – an interface to the on-line reference manuals

 Synopsis : man [instruction]

 cat – concatenate files and print on the standard output

 Synopsis : cat [option]…[file]…

 Option

 -n : number all output lines

 -E : display $ at end of each line

 more – file perusal filter for crt viewing

 Synopsis : more [file]

 less – opposite of file

 Synopsis : less [file]

 grep – print lines matching a pattern

 Synopsis : ls | grep [file]

 gcc – GNU project C and C++ compiler

 Synopsis

 gcc [file] – compile the file and generate a binary file named a.out

 gcc -c [file] – compile the file and generate an object file named file.o

 gcc –o outputfile [file] – compile the file and generate an binary file named outputfile

Functions 參考

int rand ( void );

 *Returns a pseudo-random integral number in the range 0 to RAND_MAX.

void srand ( unsigned int seed );

 The pseudo-random number generator is initialized using the argument passed as seed.

 seed : An integer value to be used as seed by the pseudo-random number generator algorithm.

Int sizeof(Variable or Data Type)

 sizeof is used to calculate the sizes of datatypes

char * gets ( char * str );

 *get string

 str : Pointer to an array of chars where the C string is stored.

int puts ( const char * str );

 *put string

 str : C string to be written.

FILE * fopen ( const char * filename, const char * mode );

C string containing a file access modes. It can be:

"r" / Open a file for reading. The file must exist.
"w" / Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
"a" / Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist.
"r+" / Open a file for update both reading and writing. The file must exist.
"w+" / Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
"a+" / Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek, rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.

int fclose ( FILE * stream );

 Closes the file associated with the stream and disassociates it.

 stream : Pointer to a FILE object that specifies the stream to be closed.

int fprintf ( FILE * stream, const char * format, ... );

 stream :Pointer to a FILE object that identifies the stream.

specifier / Output / Example
c / Character / a
d or i / Signed decimal integer / 392
e / Scientific notation (mantise/exponent) using e character / 3.9265e+2
E / Scientific notation (mantise/exponent) using E character / 3.9265E+2
f / Decimal floating point / 392.65
g / Use the shorter of %e or %f / 392.65
G / Use the shorter of %E or %f / 392.65
o / Signed octal / 610
s / String of characters / sample
u / Unsigned decimal integer / 7235
x / Unsigned hexadecimal integer / 7fa
X / Unsigned hexadecimal integer (capital letters) / 7FA
p / Pointer address / B800:0000
n / Nothing printed. The argument must be a pointer to a signed int, where the number of characters written so far is stored.
% / A % followed by another % character will write % to the stream.

int fscanf ( FILE * stream, const char * format, ... );

 Reads data from the stream and stores them according to the parameter format into the locations pointed by the additional arguments.

 stream :Pointer to a FILE object that identifies the stream to read data from.

type / Qualifying Input / Type of argument
c / Single character: Reads the next character. If a width different from 1 is specified, the function reads width characters and stores them in the successive locations of the array passed as argument. No null character is appended at the end. / char *
d / Decimal integer: Number optionally preceeded with a + or - sign. / int *
e,E,f,g,G / Floating point: Decimal number containing a decimal point, optionally preceeded by a + or - sign and optionally folowed by the e orE character and a decimal number. Two examples of valid entries are -732.103 and 7.12e4 / float *
o / Octal integer. / int *
s / String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab). / char *
u / Unsigned decimal integer. / unsigned int *
x,X / Hexadecimal integer. / int *

int fgetc ( FILE * stream );

 * Returns the character currently pointed by the internal file position indicator of the specified stream.

 stream :Pointer to a FILE object that identifies the stream on which the operation is to be performed.

int fputc ( int character, FILE * stream );

 Writes a character to the stream and advances the position indicator.

 character :Character to be written. The character is passed as its int promotion.

 Stream :Pointer to a FILE object that identifies the stream where the character is to be written.

 char * fgets ( char * str, int num, FILE * stream );

 Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or a the End-of-File is reached, whichever comes first.

 str :Pointer to an array of chars where the string read is stored.

 Num :Maximum number of characters to be read (including the final null-character). Usually, the length of the array passed as str is used.

 Stream :Pointer to a FILE object that identifies the stream where characters are read from.

 To read from the standard input, stdin can be used for this parameter.

 int fputs ( const char * str, FILE * stream );

 Writes the string pointed by str to the stream.

 str :An array containing the null-terminated sequence of characters to be written.

 Stream :Pointer to a FILE object that identifies the stream where the string is to be written.

 int feof ( FILE * stream );

 Checks whether the End-of-File indicator associated with stream is set, returning a value different from zero if it is.

 stream :Pointer to a FILE object that identifies the stream.

 Int main ( int argc, char* argv[] )

 The parameters argc( argument count) and argv (argument vector), respectively give the number and value of the program's command-line arguments.

 ex. hello.exe hello world

 argc = 3

 argv[0] is "hello.exe",argv[1] is "hello",argv[2] is "world"。

 #define

 The #define directive substitutes token-string for all subsequent occurrences of an identifier in the source file. The identifier is replaced only when it forms a token.

 math.h

 cmath declares a set of functions to compute common mathematical operations and transformations:

 常用函式使用範例 :

 double pow(double x, double y)

 double sqrt(double x)

 double fabs(double x)

 double exp(double x)

 double log(double x)

 double log10(double x)

 double ceil(double x)

 double floor(double x)

 double fmod(double x, double y)

 int abs(int x)

 sin(x)

 cos(x)

 tan(x)

 string.h

 This header file defines several functions to manipulate C strings and arrays.

 常用函式使用範例 :

 int strcmp(char *s1,*s2)

 char *strcpy(char *dest, char *src)

 int strlen(char *s1)

 int atoi(char *s)

 char *gets(char *s)

 int puts(char *s)

 ctype.h

This header declares a set of functions to classify and transform individual characters.

 常用函式使用範例 :

 tolower(ch)

 toupper(ch)

 islower(ch)

 isspace(ch)

 isdigit(ch)

 isalpnum(ch)

 getchar()

 conio.h

 conio.h is a C header file used in old MS-DOS compilers to create text user interfaces.

 int kbhit(void)

 Determines if a keyboard key was pressed.

 int getch(void)

 Reads a character directly from the console without buffer, and without echo.

 int getche(void)

 Reads a character directly from the console without buffer, but with echo.

 int ungetch(int c)

 Puts the character c back into the keyboard buffer.

 char *cgets(char *buffer)

 Reads a string directly from the console.

 int cscanf(char *format, arg0,... argn)

 Reads formatted values directly from the console.

 int putch(int c)

 Writes a character directly to the console.

 int cputs(const char *string)

 Writes a string directly to the console.

 int cprintf(const char *format, arg0,... argn)

 Formats values and writes them directly to the console.

 pointer

 In computer science, a pointer is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address.

 ex.

 int *ptr;

 int num = 100;

 ptr = #

 num = 100

 &ptr = 0028FF1C

 Ptr = 0028FF18

 *ptr = 100

 Structure

 A struct in C programming language is a structured (record) type that aggregates a fixed set of labelled objects, possibly of different types, into a single object.

 ex1. ex2.

struct account typedef struct

{{

int account_number;int account_number;

char *first_name;char *first_name;

char *last_name;char *last_name;

float balance;float balance;

}a1; }account;

中正大學 – 電機工程學系,通訊工程學系 – 程式設計 1