1. function declaration
ø need to keep the variable type, the variable name is not neccessary.
2. function as a parameter to another function
ø passing the address of the function to another function
// g( func(), a)
int g(float (* func)(int), int a){
return func(a);
}
3. recursion funciton
ø consider in different situation is the key.
4. two-dimension array stores in sequence
5. gcd and lcm
6. sprintf()
ø format the string
7. strlen
// return the length of a string
strlen (string);
8. strcpy
// copy string to copy
strcpy (copy, string);
9.Lexicographical order is used to compare strings.
10. strcmp
// compare two strings, string_1 < string_2 return negative number
// string_1 == string_2 return 0
// string_1 > string_2 return positive number
strcmp (string_1, string_2);
11. strcat
// add string_2 into string_1
strcat (string_1, string_2);
12. strncpy
// copy n characters from string_2 to string_1
strncpy (string_1, string_2, n_size);
13. apply memory
int *arr = (int *) malloc (n * sizeof (int));
int *arr = (int *) calloc (n, sizeof (int)); //two parameter, initialize elements to 0.
14. release memory
// Every time we need to release the memory after using it.
free (arr);
arr = NULL;
15. double pointer
int a, *p, **q;
a = 4;
p = &a;
q = &p;
printf ("a in %p, p is %p, *q is %p", &a, p, *q);
16. explicit type conversion
float num = 2.3f;
(int) num;
16a. implicit type conversion
- //try to keep the accuracy of variables. e.x. int to long, float to double.
- // data type is based on the data on the left side of the data.
17. literal
2.3f // float type
4u // unsigned type
2.4lf // long float type
18. bit operation
bitwise NOT ~
bitwise AND &
bitwise OR |
left shift <<
right shift >>
^
左补齐 %08x
19. longest name
#include <stdio.h>
#include <string.h>
int main() {
int i, n, cnt = 0, max = 0;
char *name_1 = (char *) calloc (100, sizeof (char));
char *name;
char c;
scanf ("%d\n", &n);
while(scanf ("%c", &c) != EOF)
{
if (c != '\n'){
name_1[cnt] = c;
cnt ++;
//printf("%c", c);
}
else{
//printf ("\n");
//printf ("%s\n", name_1);
if (max < cnt)
{
max = cnt;
free (name);
name = (char *) calloc (max, sizeof (char));
strcpy (name, name_1);
}
free (name_1);
name_1 = (char *) calloc (100, sizeof (char));
cnt = 0;
}
}
if (max < cnt)
{
max = cnt;
free (name);
name = (char *) calloc (max, sizeof (char));
strcpy (name, name_1);
cnt = 0;
}
printf ("%s\n", name);
return 0;
}
output error???
20. Hash Function
#include <stdio.h>
int main() {
int arr[32] = {0}, bits[32] = {0};
char a;
int i = 1, j = 0;
while (scanf ("%c", &a) != EOF)
{
arr[i % 32] += a;
i++;
}
for (j = 0; j < 32; j++)
{
bits[j] = (arr[31 - j] ^ (arr[j] << 1));
bits[j] = (bits[j] % 85) + 34;
printf("%c", bits[j]);
}
return 0;
}
21. typedef
typedef original_type_name Alias_name
22. struct pointer
(* struct pointer_name).element;
// is equal to
struct pointer_name -> element;
23. linked list
typedef struct node {
int number;
struct node *next;
} Node;
Node *create_node (int number) {
Node *temp_node;
temp_node = (Node *) malloc (sizeof(Node));
temp_node -> number = new_number;
temp_node -> next = NULL;
return temp_node;
}
24.Union
// visit the same memory bloack
// embedded programming to save memory
unsigned char b1:1 // only use 1 bit (bit field)
25. enumeration
26. compile file
gcc -c -o set.o set.c // *.o relocatable object file. produce a relocatable object file. -o which called after -o
gcc -o program main.o set.o others.o // put relocatable file main.o, set.o and others.o together(目标代码文件), then output a excutable file program.
./program // run excutable file program.
27.Macro define
#ifndef
#define
#endif
28.makefile
array.o: array.c array.h
gcc -c -o array.o array.c
### target: dependency 1, dependency 2 ...
### command
###
### when we run make array.o
### system will check if there is no array.o or any of array.c and array.h is newer, the system will run gcc -c -o array.o array.c
###
### can write different command in same makefile, divid them by suffix.
.PHONY: clean ## Tell the system that if already exist a clean file, we can still run this command
clean:
rm -f array.o main.o main ## force delete array.o main.o main file. rm: delete. -f: means force
Apply variable in makefile
# sharp sign is a comment
# set the C compiler
CC = gcc
# -g add the debug information
# -Wall open the warning information
CFLAGS = -g -Wall
# main target dependency file
MAINOBJS = main.o array.o
.PHONY: clean
main: $(MAINOBJS)
$(CC) $(CFLAGS) -o main $(MAINOBJS)
array.o: array.c array.h
$(CC) $(CFLAGS) -c -o array.o array.c
main.o: main.c array.h
$(CC) $(CFLAGS) -c -o main.o main.c
clean:
rm if $(MAINOBJS) main
29.command line parameter
// ./program hello world
int main (int argc, char **argv)
// three parameters, argc = 3, argv[1] = hello, argv[2] = world, argv[0] = ./main hello world
30. file operation
// declare a file pointer
FILE *fp;
fp = fopen (file_path, visit_type); // "r" read, "w" write, "a" add a
fgetc (fp); // get one character from fp
fputc ('c', fp); // write 'c' into fp.
fscanf (in_fp, "%c", &a); //read from in_fp.
fscanf (stdin, "%c", &a); // is equal to
scanf ("%c", &a);
fclose (fp); // close file fp
// copy the content from one file to another
void filecopy (FILE *in_fp, FILE *out_fp){
char ch,
while ((ch = fgetc (in_fp)) != EOF)
{
fputc (ch, out_fp);
}
}
31.debug program
gcc -o program -g main.c
gdb ./program #to use gdb debug app, need to type -g before the code.