[問題] 關於字元和字串顯示錯誤問題(已解決)

作者: bat205 (時速200km)   2014-05-01 16:53:47
開發平台(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過,但都無法解決問題
懇請各位幫忙.
作者: diabloevagto (wi)   2014-05-01 16:56:00
scanf_s 改成 scanf
作者: bat205 (時速200km)   2014-05-01 17:03:00
如果不改scanf_s的話會無法建置http://ppt.cc/zJ9h
作者: holik0123   2014-05-01 19:32:00
需要指定buffer長度嗎?
作者: EdisonX (卡卡獸)   2014-05-01 20:19:00
scanf_s("%s",name, _countof(name));
作者: h520 (恩哈恩哈恩哈哈)   2014-05-03 01:47:00
建議還是多使用標準函式庫 其他像bzero之類的非標準 大多NG
作者: bat205 (時速200km)   2014-05-04 16:20:00
多謝各位解答,在屬性那加上_CRT_SECURE_NO_WARNINGS就可以不使用scanf_s了

Links booklink

Contact Us: admin [ a t ] ucptt.com