1. 程式人生 > >C/C++學習記錄

C/C++學習記錄

第一部分 C語言基礎

字串庫函式

gets(char * _str)

char str[100] = {0};
gets(str);//結束標誌:回車(不是空格)
/*
    存在緩衝區溢位的問題
*/

fgets()

char str[100] = {0};
fgets(str, sizeof(str),stdin);

puts()

char str[100] = "hello c";
puts(str);
/*
    puts會自動新增一個'\n',輸出到螢幕上
*/

fputs()

char str[100] = "hello c";
fputs(str, stdout);

strlen()

返回不包含字串結尾’\0’的長度

char str[100] = "hello c";
int len = strlen(str);
printf("%d\n", len);//7

strcat(char * _str1, const char * _str2);

將字串_str2追加到 _str1後面

char str1 = "heoll ";
char str2 = "world";
strcat(str1, str2);
printf("%s\n",str1);
/*
    strcat存在緩衝區溢位的問題
*/

strncat(char * _str1, const char * _str2, unsigned int len)

將字串_str2前len個字元追加到 _str1後面

char str1 = "heoll ";
char str2 = "world";
strcat(str1, str2,3);
printf("%s\n",str1);

strcmp(const char * _str1, const char * _str2)

char str1[100] = "bbb";
char str2[100] = "aaa";
int  a = strcmp(str1, str2);
printf("%d\n", a);//1

strcpy(str2,"bbb");
a = strcmp(str1, str2);
printf
("%d\n",a);//0 strcpy(str2,"ccc"); a = strcmp(str1, str2); printf("%d\n",a);//-1

strncmp(const char* _str1, const char * _str2, size_t len)

char str1[100] = "aaa";
char str2[100] = "bbbbb";
int a = strncmp(str1, str2, 3);

strcpy(char * _str1, const char * _str2)

將引數_str2拷貝到 _str1,

char str1[100] = {0};
char str2 = "hello c";
strcpy(str1,str2);
printf("%s\n",str1);//"hello c"

strncpy(char * _str1, const char * _str2, size_t len)

char str1[100] = {0};
char str2 = "hello c";
strncpy(str1,str2,5);
printf("%s\n",str1);//"hello"

sprintf()

將格式化結果輸出到字串中

int i = 200;
char str[100] = {0};
sprintf(str, "i = %d", i);
printf("%s\n", str);//"i = 200"

sscanf()

從指定格式化字串讀取輸入,而scanf是從鍵盤中讀取輸入

char str[100] = "60+50=";
int a = 0;
int b = 0;
sscanf(str,"%d+%d=",&a,&b);
printf("a + b = %d\n", a + b);//"a + b = 110"

strchr(char * _str, int _ch)

在引數_str中查詢引數 _ch指定字元,找到返回字元 _ch在 _str中所在位置,沒有找到返回NULL

char ch = 'o';
char str[100] = "hello c";
char *p = strchr(str, ch);
printf("%s\n",p);//"o c"

strstr(char * _str, const char * _substr)

在引數_str中查詢引數 _substr指定子串,找到返回子串在 _str中所在位置,沒有找到返回NULL

char str2 = 'll';
char str1[100] = "hello c";
char *p = strstr(str1, str2);
printf("%s\n",p);//"llo c"

strtok(char * _str1, char * _str2)

字元在第一次呼叫時strtok()必需給予引數_str字串,往後的呼叫則將引數 _str設定成NULL每次呼叫成功則返回指向被分割出片段的指標

char buf[] = "[email protected]@igk";
char *p = strtok(buf, "@");
while(p)
{
    printf("%s\n",p);
    p = strtok(NULL,"@");
}

atoi(const char * _str)、atof(const char * _str)、atol(const char * _str)

需要包含標頭檔案stdlib.h,atoi是c語言的標準庫函式,itoa不是

int a = atoi("123");
printf("%d\n",a);

子函式中申請堆的錯誤方法

//錯誤方法
#include<stdio.h>
#include<stdlib.h>

void getheap(int *p)
{
    p = (int*)malloc(sizeof(int)*10);
}
int main()
{
    int *p = NULL;
    int i = 0;
    getheap(p);//p的生命週期在getheap執行完就結束了
    for(i = 0; i<10;i++)
    {
        p[i] = i;
    }
    for(i = 0; i < 10; i++)
    {
        printf("%d\n", p[i]);
    }
    free(p);
    return 0;
}
//正確的方法
#include<stdio.h>
#include<stdlib.h>

void getheap(int **p)
{
    *p = (int*)malloc(sizeof(int)*10);
}
int main()
{
    int *p = NULL;
    int i = 0;
    getheap(&p);//通過指標間接改變變數的值
    for(i = 0; i<10;i++)
    {
        p[i] = i;
    }
    for(i = 0; i < 10; i++)
    {
        printf("%d\n", p[i]);
    }
    free(p);
    return 0;
}