1. 程式人生 > 其它 >C語言atoi()函式

C語言atoi()函式

技術標籤:C

描述

int atoi(const char *str)把引數str所指向的字串轉換為一個整數(型別為 int 型)。

宣告

下面是 atoi() 函式的宣告。

int atoi(const char *str)

引數

  • str-- 要轉換為整數的字串。

返回值

該函式返回轉換後的長整數,如果沒有執行有效的轉換,則返回零。

例項

下面的例項演示了 atoi() 函式的用法。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>//包含atoi()函式
 
int main(void){

    char str1 [12] = "99999";
    char str2 [12] = "HelloWorld";

    int value1 = atoi(str1);
    int value2 = atoi(str2);

    printf("str1的字串值:%s\n",str1);
    printf("atoi的整型值:%d\n",value1);

    printf("atoi的整型值:%d\n",value2);
     
    return 0;
}

列印結果:

程式碼分析:

atoi()函式作用是把字串轉換成整數int型別,如果字串裡面的是數字返回數字、是字元則返回為0。

以上就是atoi()函式使用方式以及說明!