atoi() - C語言庫函數

C庫函數 int atoi(const char *str) 轉換為字串參數str為整數(int型)。 

聲明

以下是atoi() 函數的聲明。

int atoi(const char *str)

參數

  • str -- 這是一個整數的字串表示形式。

返回值

這個函數返回一個int值轉換的整數。如果沒有有效的轉換可以執行,它返回零。

例子

下麵的例子顯示atoi() 函數的用法。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
   int val;
   char str[20];

   strcpy(str, "98993489");
   val = atoi(str);
   printf("String value = %s, Int value = %d
", str, val);

   strcpy(str, "xuhuhu.com");
   val = atoi(str);
   printf("String value = %s, Int value = %d
", str, val);

   return(0);
}

讓我們編譯和運行上面的程式,這將產生以下結果:

String value = 98993489, Int value = 98993489
String value = xuhuhu.com, Int value = 0

上一篇: atof() - C語言庫函數 下一篇: atol() - C語言庫函數