我正在努力提高我的 C 编程水平,我试图测试在获取字符的循环内显示输入流中的字符。我正在使用 getchar
方法。
当我的代码中存在 printf
语句时,我收到了一个异常抛出。 (如果我注释掉此函数中的 printf
行,则不会抛出异常)。
Exception: Unhandled exception at 0x611c91ad (msvcr90d.dll) in firstOS.exe: 0xC0000005: Access violation reading location 0x00002573.
这是代码……有什么想法吗?谢谢。
附言。我正在使用 stdio.h
库。
/*getCommandPromptNew - obtains a string command prompt.*/
void getCommandPromptNew(char s[], int lim){
int i, c;
for(i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i){
s[i] = c;
printf('%s', c);
}
最佳答案
尝试改变:
printf('%s', c);
到
printf("%c", c);
如果您希望在循环的结尾 打印整个字符串,您需要使用 NULL
字符终止它,如:
s[i] = 0;
然后您可以将其打印为:
printf("%s", s);
关于c - 为什么在visual studio C编译器中会抛出这个异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2485894/