作者bat205 (時速200km)
看板C_and_CPP
標題[問題] 關於字元和字串顯示錯誤問題 (已解決)
時間Thu May 1 16:53:47 2014
開發平台(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過,但都無法解決問題
懇請各位幫忙.
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.44.179.16
※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1398934430.A.8DF.html
→ diabloevagto:scanf_s 改成 scanf 05/01 16:56
→ bat205:如果不改scanf_s的話會無法建置 05/01 17:03
推 holik0123:需要指定buffer長度嗎? 05/01 19:32
→ EdisonX:scanf_s("%s",name, _countof(name)); 05/01 20:19
推 h520:建議還是多使用標準函式庫 其他像bzero之類的非標準 大多NG 05/03 01:47
※ 編輯: bat205 (114.45.94.105), 05/04/2014 16:18:30
→ bat205:多謝各位解答,在屬性那加上_CRT_SECURE_NO_WARNINGS 05/04 16:20
→ bat205:就可以不使用scanf_s了 05/04 16:21