輸出星星
#include <stdio.h> void printStart( int num ) { while(num-->0) { printf("*"); }; } int main() { int n = 20, i; for(i=0; i<n; i++) { printStart(i); printf("\n"); } printStart(4); return 0; }
默認情況下, C++函數按照值傳遞參數, 這就意味著函數中定義的形參是新的變量, C函數通過通過使用拷貝, 保護了原始數據的完整性;
雖然C語言數組名就是一個地址指針, 但是它們仍然還是按照值傳遞的;
如果參數是數組的話, 默認傳遞的是數組的復制品:
#include <stdio.h> void change(int array[2]) { array[0] = 2; } int main() { int arr[2] = {1,2}; printf("arr的第一個值是:%d\n", arr[0]); change(arr); printf("arr的第一個值是:%d\n", arr[0]); return 0; }
輸出為:
arr的第一個值是:1 arr的第一個值是:2
如果參數為數組,接收的參數設置為指針, 修改指針即會修改原來的數組:
#include <stdio.h> void change(int *p) { p[0] = 2; } int main() { int arr[2] = {1,2}; printf("arr的第一個值是:%d\n", arr[0]); change(arr); printf("arr的第一個值是:%d\n", arr[0]); return 0; }
從一堆數組中找出指定的的數字:
#include <stdio.h> #define NUMBER 5 int search(int *p, int n) { int index = 0; printf("string\n"); while(true) { if(*(p+index) == n) { return index+1; } printf("%d\n", *p); index++; } return -1; } int main() { int arr[NUMBER] = {0}; int num; int i = 0; for(i=0; i<NUMBER; i++) { printf("輸入索引為%d的值\n", i); scanf("%d",&arr[i]); } printf("輸入需要查找的數字\n"); scanf("%d", &num); int index = search(arr, num); printf("找到的索引為%d", index); return 0; }
靜態變量, 靜態變量
#include <stdio.h> int foo = 0; void func() { static int sx = 0; foo++; sx++; printf("%d--%d\n", foo, sx); } int main() { func(); func(); func(); func(); return 0; }
輸出:
1--1 2--2 3--3 4--4
如果聲明了靜態變量, 那么這個變量就會和函數掛鉤, 相對于函數的屬性, 靜態變量只會初始化一次
數字后面不同的后綴,說明了不同的類型;
1u:unsigned int 1l:long 1ll: long long 1UL:unsigned long 0.5f:float
查看二進制數字包含1的進制位:
#include <stdio.h> int count_bit(unsigned x) { int count = 0; while(x) { if(x & 1U) count++; x >>= 1; } return count; } int main() { unsigned bits = 3; int length = count_bit(bits); printf("length is %d\n", length); return 0; }
如果數據太大, 超出了能夠存儲的大小, 計算結果會出現問題, 尷尬😅:
#include <stdio.h> int main() { printf("int 的位數 是 %d\n", sizeof(int)); int x = 4294967296-1; int y = 333333; x += y; printf("result is %d\n", x);//輸出 : result is 333332 return 0; }
又是精度問題, 使用小樹會導致計算結果有誤差:
#include <stdio.h> int main () { float i; for(i=0.0; i<1.0; i+=0.01) { printf("%f", i); } return 0; }
統計用戶輸入數字的個數:
#include <stdio.h> int main() { int count = 0; int arr[5] = {0}; while( true ) { printf("請輸入數字0-4\n"); char c = getchar(); switch( c ) { case '0': arr[0]++; break; case '1': arr[1]++; break; case '2': arr[2]++; break; case '3': arr[3]++; break; case '4': arr[4]++; break; } printf("統計位數: \n 0 : %d \n 1 : %d \n 2 : %d \n 3 : %d \n 4 : %d \n", arr[0], arr[1], arr[2], arr[3], arr[4]); } return 0; }
以上的代碼經過優化以后, 變成這樣:
#include <stdio.h> int main() { int count = 0; int arr[5] = {0}; while( true ) { printf("請輸入數字0-4\n"); char c = getchar(); if( c >= '0' && c <= '4') { printf("%d\n",c); int i = c; arr[i-48]++; } printf("統計位數: \n 0 : %d \n 1 : %d \n 2 : %d \n 3 : %d \n 4 : %d \n", arr[0], arr[1], arr[2], arr[3], arr[4]); } return 0; }
如果直接使用雙引號""聲明字符串, 那么字符串的最后默認會被添加一個0,作為結束標記;
#include <stdio.h> int main() { char str[] = "ABCD"; char str1[] = {'A','B','C','D'}; printf("%s ==>> %d \n", str, sizeof(str)); //長度為5 printf("%s ==>> %d \n", str1, sizeof(str1)); //長度為4, 少了一個結束位, 必須自己給上 return 0; }
字符整理成大寫:
#include <stdio.h> #include <ctype.h> int main() { while(true) { char ch = getchar(); if(ch == EOF) { break; } printf("%c",toupper(ch)); } return 0; }
字符串整理成大寫:
#include <stdio.h> #include <ctype.h> void up(char str[]){ int i = 0; while(str[i]) { str[i] = toupper(str[i]); i++; } } int main() { char str[10]; while(true) { scanf("%s",str); printf("you enter => %s\n", str); up(str); printf("to uppper => %s\n", str); } return 0; }
還數組和指針:
#include <stdio.h> int main() { int data[] = {22,33,44,55,66}; int *p = data; // int *p = &data[0]; 這樣寫也行, 因為數組中對一個數據的地址就是數組的地址; int i; for( i = 0; i < 5; i++ ) { printf("i = %d ==> %d\n", i, data[i]); printf("i = %d ==> %d\n", i, p[i]); printf("i = %d ==> %d\n", i, *(p+i)); } return 0; }
使用指針會比使用字符串數組更加靈活:
#include <stdio.h> #include <string.h> int main() { //char arr[] = "ABC"; //arr = "EDG"; //此法是不行的,arr其實是一個地址,必須這么干:strcpy(arr,"EDG"); //但是指針可以修改 char *p = "ABC"; p = "EDG"; printf("%s\n\n", p); return 0; }
指針數組 相對于 標準的數組, 更靈活, 而且存放的空間會更小:
#include <stdio.h> int main() { int data[] = {22,33,44,55,66}; int *p = data; // int *p = &data[0]; 這樣寫也行, 因為數組中對一個數據的地址就是數組的地址; int i; for( i = 0; i < 5; i++ ) { printf("i = %d ==> %d\n", i, data[i]); printf("i = %d ==> %d\n", i, *(p+i)); } return 0; }
如果是標準的數組, 需要給一個固定的長度, 但是使用指針數組的話, 并不會出現多余的數據, 圖示:
調用函數的時候, 如果直接把結構體作為參數, 其實傳遞的時候結構體的副本,( 這個和js有點區別, js中對象就是是指針) 如果在函數中修改結構體, 要注意必須傳遞指針, 數組傳遞的也是副本, (除非你傳遞指針或者引用), 這個要注意:
#include <stdio.h> struct student{ char name[10]; int age; }; void showAge(struct student *s) { printf("%d\n",s->age); } int main() { struct student stu = {"abc", 10}; printf("%s\n", stu.name); showAge(&stu); return 0; }
以上的代碼中, 我們定義了stu這個結構體, 但是 使用了比較多的的代碼:
student struct stu
通過使用typedef, 可以進行簡化:
#include <stdio.h> typedef struct { char name[10]; int age; } student; void showAge(student *s) { printf("%d\n",s->age); } int main() { student stu = {"abc", 10}; printf("%s\n", stu.name); showAge(&stu); return 0; }
文件讀取操作的代碼, 創建一個文件對象, 然后讀區copy.cpp文件,通過putchar輸出到控制臺:
#include <stdio.h> int main () { FILE *f; int c; f = fopen("copy.cpp","r"); if(f == NULL) { printf("error\n"); }else{ while( (c=fgetc(f) )!=EOF ) { //printf("%c",c); putchar(c); } printf("open\n"); fclose(f); } return 0; }
通過描述符打開文件, 區別于fopen和FILE句柄:
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h> #include <memory.h> int main() { int fd = -1; char filename[] = "fileopen.cpp"; fd = open(filename, O_RDWR); if( -1 == fd ) { printf("error\n"); }else{ printf("fd is %d\n", fd); } int size = -1; char buffer[80]; while(size) { memset(buffer, 0, sizeof(buffer)); size = read(fd, buffer, sizeof(buffer)); //printf("%s",buffer); for(int i=0 ; i<size; i++) { printf("%c",buffer[i]); } } close(fd); return 0; }
文件寫入并查看:
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h> #include <memory.h> int main() { int fd = -1; char filename[] = "test.txt"; fd = open(filename, O_RDWR); if( -1 == fd ) { printf("error\n"); }else{ printf("fd is %d\n", fd); } //write char str[] = "12345"; write(fd, str, sizeof(str)); //read int size = -1; char buffer[10]; fd = open(filename, O_RDWR); while(size) { memset(buffer, 0, sizeof(buffer)); size = read(fd, buffer, sizeof(buffer)); for(int i=0 ; i<size; i++) { printf("%c",buffer[i]); } } close(fd); return 0; }
給文件的固定位置添加數據, lseek :
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h> int main() { int fd = -1, i; char buf1[] = "123456"; char buf2[] = "abcdefg"; char filename[] = "hole.txt"; fd = open(filename, O_RDWR|O_CREAT, S_IRWXU); if(fd == -1) { printf("error\n"); return 0; } int size = write(fd, buf1, sizeof(buf1)); //set offset off_t offset = lseek(fd, 32, SEEK_SET); if( -1 == offset ) { printf("error\n"); return 0; } write(fd, buf2, sizeof(buf2)); close(fd); return 0; }
獲取文件的狀態 stat的結構為:
struct stat { mode_t st_mode; //文件對應的模式,文件,目錄等 ino_t st_ino; //inode節點號 dev_t st_dev; //設備號碼 dev_t st_rdev; //特殊設備號碼 nlink_t st_nlink; //文件的連接數 uid_t st_uid; //文件所有者 gid_t st_gid; //文件所有者對應的組 off_t st_size; //普通文件,對應的文件字節數 time_t st_atime; //文件最后被訪問的時間 time_t st_mtime; //文件內容最后被修改的時間 time_t st_ctime; //文件狀態改變時間 blksize_t st_blksize; //文件內容對應的塊大小 blkcnt_t st_blocks; //偉建內容對應的塊數量 };
創建結構:
#include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <stdio.h> int main(void) { struct stat st; if(-1==stat("test.txt",&st)) { printf("get file status failure\n"); return -1; } printf("此文件的大小:%d\n",st.st_size); printf("此文件的租后修改時間:%d\n",st.st_mtime); printf("此文件的節點:%d\n",st.st_ino); printf("此文件的保護模式:%d\n",st.st_mode); }
廈門點燃未來網絡科技有限公司, 是廈門最好的微信應用, 小程序, 微信網站, 公眾號開發公司
EOF
文章列表