当前位置:网站首页>C language learning log 1.17

C language learning log 1.17

2022-06-13 04:57:00 Today is also a day without baldness

Dynamic distributed memory :

        malloc function : Call... Before using this function “stdlib” This library , The format is : void*malloc(size_t size); The return type is void.

towards malloc The size of the space requested is in bytes , The result is void*, The required type is converted to the required type ; (int*)malloc(n*sizeof(int)).

TIPS: malloc The amount of memory allocated is limited , When the allocation fails, an empty address or NULL, We can write a code to determine how much memory our computer can allocate .

int mallocSize()
{
	void *p;
	int cnt = 0;
	while((p = malloc(100*1024*1024)))
	{
		cnt++;
	}
	printf(" Successfully assigned %d00MB Space ",cnt);
	free(p); 
	return 0;
}

 

        free function :free() It is used to return the space obtained from the application to “ System ”, Applied space , In the end, it should be returned , And you can only return the first address of the requested space .(1. Applied for memory , Be sure to free, Otherwise, the memory will gradually decrease after long-time operation , When there is not enough memory , The system will automatically clear all memory .2.free After that, you can't free The same thing )

String manipulation :

        getchar Represents reading in a character ,putchar Represents the output of a character .getchar The usage of is to use directly when entering getchar() Function to get the character ,putchar The usage of is to pass in character variables for printing .

        getchar Implemented by macro :#define getchar() getc(stdin).getchar There is one int The return value of type , When the program calls getchar when , The program is waiting for the user to press the button . The characters entered by the user are stored in the keyboard buffer , Until the user presses enter ( The carriage return character is also placed in the buffer ). When the user types enter ,getchar Just started from stdio One character at a time in the stream .getchar The return value of the function is the number of characters entered by the user ASCII code , If the end of the file (End-Of-File) Then return to -1(EOF), And the characters entered by the user will be displayed back to the screen .       

          Such as : The user entered more than one character before pressing enter , Other characters will remain in the keyboard cache , Waiting for follow-up getchar Call read . in other words , Follow up getchar The call does not wait for the user to press the key , And read the characters in the buffer directly , Until the characters in the buffer are read , Waiting for the user to press the button .

        putchar The function is to output a character to the terminal . The format for int putchar(int c), among c It can be a sheet ( In English ) A leading character , Can be between 0~127 A decimal integer between , It can also be used in advance char A well-defined character variable

原网站

版权声明
本文为[Today is also a day without baldness]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280517425858.html