2012-07-30 115 views
0

有沒有辦法在C中將字符串轉換爲數字,反之亦然?我知道我可以在C++中使用iostream並使用atoi()或sprintf()等。我想知道是否有一種方法可以在不使用流的情況下在C中完成此操作。我現在看到的唯一解決方案是創建我自己的功能,除非有人知道已經存在且已經證實的解決方案。將字符串轉換爲數字,反之亦然C(NOT C++)

基本上,是否有C等價於C++的atoi()和sprintf()?

+6

這兩個都是C:[atoi](http://en.cppreference.com/w/c/string/byte/atoi)和[sprintf](http://en.cppreference.com/w/ C/IO/fprintf中)。 – chris 2012-07-30 15:21:44

+5

'atoi()'和'sprintf()'是C函數。然而,比使用atoi()更好的是使用'strto(u)l(l)()'。 – 2012-07-30 15:21:59

+0

標準C庫顯然是現代世界中一種古老而未知的藝術。 – tbert 2012-07-30 17:49:28

回答

4

這兩者都是C函數:

  • atoi是在stdlib.h
  • sprintf是在stdio.h

而且,由於這些被包括爲C的部分,也不需要C++流。

+0

看來我並沒有看到一個非常好的C參考。或者只是看錯了地方。謝謝! – Max 2012-07-30 15:39:11

+0

! atoi包含非標準化行爲... – 2012-07-30 15:50:05

+0

嗯...根據http://en.wikibooks.org/wiki/C_Programming/C_Reference/stdlib.h/atoi - 'atoi被認爲是由strtol棄用。所以他應該使用'strtol'而不是'atoi'? – 2012-07-30 15:55:58

相關問題