C primer 程序清单4.1源代码,调试显示 run time check error #3-T ?

C primer 程序清单4.1源代码,调试显示 run time check error #3-T ?,第1张

#include <stdio.h>

#include <string.h> // for strlen() prototype

#define DENSITY 62.4 // human density in lbs per cu ft

int main()

{

float weight, volume

int size, letters

char name[40] // name is an array of 40 chars

printf("Hi! What's your first name?\n")

scanf("%s", name)

printf("%s, what's your weight in pounds?\n", name)

scanf("%f", &weight)

size = sizeof name

letters = strlen(name)

volume = weight / DENSITY

printf("Well, %s, your volume is %2.2f cubic feet.\n", name, volume)

printf("Also, your first name has %d letters,\n", letters)

printf("and we have %d bytes to store it.\n", size)

return 0

}

没错,运行环境: gcc 5.4.0

少了 using namespace std

而且 cout 配合 << *** 作符。不是 cout >>这样是错误的。

cout >>"word read is: " >>word >>'\n'

cout >>"ok: no more words to read: bye!\n"

改为:

cout <<"word read is: " <<word <<'\n'

cout <<"ok: no more words to read: bye!\n"

#include <iostream>

#include <string>

using namespace std

int main()

{

string word

while ( cin >>word )

cout <<"word read is: " <<word <<'\n'

cout <<"ok: no more words to read: bye!\n"

return 0

}

运算符的结合顺序与表达式求值是两个概念,请务必分清楚。

*start++右至左结合的意思是start先与++结合,而不是先与*结合,用括号表示,就是*start++相当于*(start++),而不是(*start)++。结合性影响的是表达式的语义。

*(start++)先求表达式start++的值再对其解引用。start++是一个表达式,它的值等于start变量的当前值,这是后缀++的运算规则。所以,当start为n时,start++的值也为n。*start++的结果就是*n。对表达式start++求值还会导致start的值增加1,所以在start++求值之后的某一个时间点,start变量的值会变成n+1,这称为表达式的副作用。表达式的值和副作用也是完全不同的两个概念。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/11076541.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-13
下一篇2023-05-13

发表评论

登录后才能评论

评论列表(0条)

    保存