Re: [問題] 關於字元和字串顯示錯誤問題

作者: RealJack   2014-05-01 21:19:01
scanf_s不是ANSI C的函式
應該是微軟的library裡面的函式,scanf的安全版
其實還是可以在visual studio用scanf
http://kevincg.wordpress.com/2008/06/12/warningc4996/
意思就是不讓compiler把這種警告當錯誤看待
建議你換個GNU的編譯器來練習標準的C
例如: code::block也不錯用,是個不錯的IDE
※ 引述《bat205 (時速200km)》之銘言:
: 開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
: Visual Studio 2013
: win7 64bit
: 問題(Question):
: 為兩題洪維恩的C語言教學手冊範例 prog4_13和prog4_15
: 餵入的資料(Input):
: Q1:輸入R字元
: Q2:輸入字串Alice
: 預期的正確結果(Expected Output):
: Q1:ch=R, ASCII code is 82
: Q2:Hi, Alice, How are you?
: 錯誤結果(Wrong Output):
: Q1:出現無法正確啟動(0xc0000142),錯誤視窗如下
: http://ppt.cc/q4KU
: Q2:字串Alice無法顯示出來,錯誤視窗如下
: http://ppt.cc/WnE4
: 程式碼(Code):(請善用置底文網頁, 記得排版)
: Q1:/* prog4_13, 輸入字元 */
: #include <stdio.h>
: #include <stdlib.h>
: int main(void)
: {
: char ch;
: printf("Input a character:");
: scanf_s("%c",&ch); /* 由鍵盤輸入字元並指定給變數ch */
: printf("ch=%c, ascii code is %d\n",ch,ch);
: system("pause");
: return 0;
: }
: Q2:/* prog4_15, 輸入字串 */
: #include <stdio.h>
: #include <stdlib.h>
: int main(void)
: {
: char name[10]; /* 宣告字元陣列 */
: printf("What's your name: ");
: scanf_s("%s",name); /* 輸入字串,並由字元陣列name所接收 */
: printf("Hi, %s, How are you?\n",name); /* 印出字串的內容 */
: system("pause");
: return 0;
: }
: 補充說明(Supplement):
: 完全依書上範例無更動程式碼
: 已google過,但都無法解決問題
: 懇請各位幫忙.
作者: danny8376 (釣到一隻猴子@_@)   2014-05-02 02:26:00
Code::Block不一定用GUN compiler啊 用VC的也行www
作者: bat205 (時速200km)   2014-05-04 16:25:00
多謝~參考您的方式不把scanf當錯誤看待就可以正常了

Links booklink

Contact Us: admin [ a t ] ucptt.com