avatar

目录
c笔记

c-string

https://www.tutorialspoint.com/cprogramming/c_strings.htm

functions for c string:

c++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// copy string s2 into s1
strcpy(s1, s2);

// concat s2 into end of s1
strcat(s1, s2);

// return length of string s1
strlen(s1);

// return 0 if s1 and s2 are the same, less than 0 if s1<s2, greater than 0 if s1>s2
strcmp(s1, s2);

// return a pointer to the first occurrence of charactor ch in string s1
strchr(s1, ch);

// return a pointer to the first occurrence of string s2 in string s1
strstr(s1, s2);

c-array

size of array

c++
1
2
3
4
5
6
7
int prices[5] = { 1, 2, 3, 4, 5 };

int size = sizeof prices / sizeof prices[0];

int size = sizeof prices / sizeof *prices;

printf("%u", size); /* 5 */

c-pointer

check if pointer is not null

c++
1
2
if(ptr) /* succeeds if p is not null */
if(!ptr) /* succeeds if p is null */

Operators for pointer

https://www.codingeek.com/tutorials/c-programming/operations-on-pointers-c-programming-language/

dangling pointer

A pointer initially holding valid address, but later the held address is released or freed. Then such a pointer is called as dangling pointer.

https://zh.wikipedia.org/wiki/%E8%BF%B7%E9%80%94%E6%8C%87%E9%92%88

c-diff between pointer, array, string

diff between array and pointer

https://stackoverflow.com/questions/30647709/differences-between-arrays-pointers-and-strings-in-c

https://stackoverflow.com/questions/1641957/is-an-array-name-a-pointer

https://blog.csdn.net/Hackbuteer1/article/details/6706562

array names in a C program are (in most cases) converted to pointers. One exception is when we use the sizeof operator on an array. If a was converted to a pointer in this context, sizeof a would give the size of a pointer and not of the actual array, which would be rather useless, so in that case a means the array itself.

c++
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>

int main () {

char name[] = "Zara Alsssssssssi";
char *i = name;


printf("Size of names = %d\n", sizeof(name));
printf("Size of names = %d\n", sizeof(*i));

return 0;
}
Code
1
2
Size of names = 18
Size of names = 1

diff between char *str and char* str[]

char *str = “abcd” 先在静态区为”hello”常量分配5Byte,接着在栈里为指针str分配4Byte(32位机器)并指向”abcd”字串的首地址,因此此时str是指向第一个字符‘a’的一个指针。

char str[ ] = “abcd” 在栈里分配连续的5Byte,内容为’a’,’b’,’c’,’d’,’\0’ 此时str是数组名,同时也可转化为指向数组第一个字符‘a’的指针常量。
————————————————
版权声明:本文为CSDN博主「CHENG Jian」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/gatieme/article/details/48716093

c-structure

structure declaration

c
1
2
3
4
5
6
7
8
9
10
11
12
struct tag_name {
type member1;
type member2;
/* declare as many members as desired, but the entire structure size must be known to the compiler. */
};

struct tag_name my_structure; /* definition */

struct tag_name {
type member1;
type member2;
}my_structure1, my_structure2; /* directly define two struct variables */
c
1
2
3
4
5
6
typedef struct tag_name {
type member1;
type member2;
} struct_alias;

struct_alias my_structure;

c-enum

https://www.geeksforgeeks.org/enumeration-enum-c/

c-bitfield

c
1
2
3
4
5
6
7
8
struct {
type [member_name] : width ;
}

//example of a bit field variable which is able to hold a value up to 7
struct {
unsigned int age : 3;
} Age;

c-input and output

standard input and output

https://www.tutorialspoint.com/cprogramming/c_input_output.htm

character: getchar, putchar

string: get, put, scanf, prinf

File I/O

https://www.tutorialspoint.com/cprogramming/c_file_io.htm

c
1
2
3
4
5
6
7
8
9
10
11
FILE *fopen( const char * filename, const char * mode );
int fclose( FILE *fp ); // returns zero if success
int fputc( int c, FILE *fp );
int fputs( const char *s, FILE *fp );
int fprinf(FILE *fp,const char *format,...) // same as fputs
int fgetc( FILE * fp ); // read a single character
char *fgets( char *buf, int n, FILE *fp ); // read up to n-1 characters from the input stream referenced by fp, but it stops reading after encountering the first /n or end of file
int fscanf(FILE *fp, const char *format, ...) // read strings from a file, but it stops reading after encountering the first space character

size_t fread(void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);
size_t fwrite(const void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);

diff between prinft, springf and fprintf

https://www.geeksforgeeks.org/difference-printf-sprintf-fprintf/

c-preprocessors

https://www.tutorialspoint.com/cprogramming/c_preprocessors.htm

c-header

https://www.tutorialspoint.com/cprogramming/c_header_files.htm

c-type casting

https://www.tutorialspoint.com/cprogramming/c_type_casting.htm

c-error handling

https://www.tutorialspoint.com/cprogramming/c_error_handling.htm

how to use stderr

https://www.delftstack.com/zh/howto/c/c-print-to-stderr/

c-memory management

https://www.tutorialspoint.com/cprogramming/c_memory_management.htm

undefined behavior

https://stackoverflow.com/questions/4534780/writing-to-pointer-out-of-bounds-after-malloc-not-causing-error

should I cast result of malloc in C?

https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc

stack and heap

https://blog.csdn.net/mybelief321/article/details/8912736

c-command line arguments

https://www.tutorialspoint.com/cprogramming/c_command_line_arguments.htm

c-predefined standard micros

https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html

c-compiling process

https://www.cnblogs.com/mickole/articles/3659112.html

https://www.javatpoint.com/compilation-process-in-c


评论