C++ Basic 問題

  1. #include <iostream>
  2. using namespace std;

  3. int main(){
  4.        
  5.         char trying;
  6.         trying = "A";

  7.         cout << trying;

  8.         return 0;

  9. }
複製代碼
點解我打左呢段code, Build 果陣會有Error?
  1. error C2440: '=' : cannot convert from 'const char [2]' to 'char'
複製代碼
點先可以一開始定義一個char同埋比個值佢?

char要用Single quote括住。例如:
  1. char c = 'A';
複製代碼
Double quote係用黎括住string嘅。

TOP

char要用Single quote括住。例如:Double quote係用黎括住string嘅。
GraphiteCube 發表於 2010-10-14 19:30

明白啦…thank you very much

TOP

本帖最後由 kazenorin 於 2010-10-19 07:55 編輯
明白啦…thank you very much
benleung 發表於 2010-10-14 22:08



另外 C String 其實係 character array, 最後一個 character 係 NULL ( byte value「00」)
所以 "A" 其實係兩個 character, index [0] == 'A', index[1] == 0

所以 compiler 唔比你塞兩個 character 入一個 char 度:
cannot convert from 'const char [2]' to 'char'

TOP