- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在阅读 Stephen G. Kochan 的“C 语言编程”一书学习数组。尽管我花了很长时间试图找出语法错误,但我已经到了对输出感到非常困惑的地步。
这是我的代码:
#include <stdio.h>
int main (void)
{
int values[10];
int index;
values[0] = 197;
values[2] = -100;
values[5] = 350;
values[3] = values[0] + values[5];
values[9] =
values[5] / 10;
--values[2];
for ( index = 0; index < 10; ++index )
printf("values[%i] = %i\n", index, values[index]);
return 0;
}
根据书本应该如何输出:
values[0] = 197
values[1] = 0
values[2] = -101
values[3] = 547
values[4] = 0
values[5] = 350
values[6] = 0
values[7] = 0
values[8] = 0
values[9] = 35
我的输出是这样的:
values[0] = 197
values[1] = -2
values[2] = -101
values[3] = 547
values[4] = 4200832
values[5] = 350
values[6] = 4200926
values[7] = 4200832
values[8] = 7680288
values[9] = 35
就好像所有的零(未初始化的值)都被这些大数字代替了。这是怎么回事,我该如何改变它?
更新
Peter Griffiths 提到了这一点,这是来自 Stephen G. Kochan 的“Programming in C 3rd edition”一书的引述:
Because you never assigned values to five of the elements in the array - elements 1, 4, and 6 through 8 - the values that are displayed for them are meaningless. Even though the program's output shows these values as zero, the value of any uninitialized variable or array element is not defined. For this reason, no assumption should ever be made as to the value of an uninitialized variable or array element.
这清楚地表明这本书没有任何问题。所以这不是一本坏书!目前只有良好的体验!
我使用的是 Windows 7 家庭高级版 64 位,并且我使用的是 gcc (GCC) 4.7.2 编译器。不同输出的原因可能是不同的操作系统和编译器。
最佳答案
这是那本书的引述,来自您引述输出后立即的段落:
Because you never assigned
values
to five of the elements in the array - elements 1, 4, and 6 through 8 - thevalues
that are displayed for them are meaningless. Even though the program's output shows these values as zero, the value of any uninitialized variable or array element is not defined. For this reason, no assumption should ever be made as to the value of an uninitialized variable or array element.
寓意:当你真正阅读编程书籍时,你会从中收获更多。
关于c - 数组的奇数输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17709392/
我的任务是编写一个java程序,首先询问用户将输入多少个数字,然后输出输入的奇数和偶数个数。它限制为整数 0-100。我的问题是:我的代码中缺少什么? import java.util.Scanner
我正在寻找有关 VBA 脚本的帮助。我一直在试图弄清楚如何使用 mod 功能。 这是我到目前为止所做的: Function AddOddNumbersWithMod(nr) Dim i, su
我只是想从 .NET 调用 kernel32 中的 GetPrivateProfileString 和 GetPrivateProfileSection,但遇到了一些我不明白的奇怪问题。 让我们从这个
我需要做的是在列表中交替应用 2 个函数。例如: (*2) (-3) [4,5,6,7,8] 会导致[8,2,12,4,16] , 因为 4*2 , 5-3 , 6*2 , 7-3 , 8*2 ...
我尝试在 JavaScript 中创建一个函数来判断一个数字是否为偶数,或者它是否是一个数字。我收到此错误: 这是 CodeCademy 中的类(class)。 最佳答案 您正在检查函数 isNaN
当我运行此命令时,不会打印任何内容,我尝试根据用户输入的内容打印一条消息,显示奇数或偶数。 import java.util.Scanner; public class Questions {
我必须编写一个程序来读取 3 个数字(使用输入框),并根据它们的值写入以下消息之一: 所有 3 个数字都是奇数或 所有 3 个数字都是偶数或 2 个数字是奇数,1 个数字是偶数或 1 个数字是奇数
我正在构建一个谷歌图像搜索的示例。我有一个图像网格(搜索结果)。当单击其中一张图像时,我正在使用 jquery 创建的部分标记中加载 html 文档。奇怪的是,如果你查看开发人员工具,html 已加载
我试图仅在偶数行上打印单词 * Even * ,而不包括第一行和第二行(最终不包括最后两行),但它打印 15 行,然后在整个过程中随机打印 * Even * 。使用 6 表示宽度,使用 15 表示高度
我有一个数学函数,它取决于由 给出的三个变量 {n、a 和 b} {a = n+1, b=n} 当 n 为偶数时 {b = n+1, a=n} 当 n 为奇数时 我的函数被调用了很多 次,n 各不相同
我有一个查询,其中有一个条件来检查房间号是否为奇数/偶数。问题在于房间号与建筑物信息一起存储在字符串中。 以下是数据库中数据的格式: ABC-0101A (Odd) ABC-0112B (Even
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我有一个包含类似内容的页面。 ################################# # __________________________ # # | |
这个问题在这里已经有了答案: Can I combine :nth-child() or :nth-of-type() with an arbitrary selector? (9 个回答) Ho
我正在尝试了解这个素数分解的特定解决方案(取自 http://rosettacode.org/wiki/Prime_decomposition#Python:_Using_floating_point
我知道这可能是一个愚蠢而简单的问题,但我对编程还很陌生。我有以下关于我在一个程序中看到的 if 运算符的问题。这是代码: d= -12.4; if(d) printf("%d \n", abs
我正在尝试制作一个脚本,根据天气情况输出用户名,他们被分配奇数或偶数值。我想我已经设法让奇数的工作,但偶数的不会输出。这是它的样子。 'commentid' 是确定将它们分配给奇数还是偶数的值。 ";
我的 NPE 的 Stacktrace 开始于: Caused by: java.lang.NullPointerException at pl.yourvision.crm.web.serv
我正在尝试查找给定数字(用户输入)是偶数还是奇数。 I'm simply applying AND operation on binary digits of a no. with 1, If the
有谁知道用于可变范围的桶的哈希函数(对于字符串,如果它重要的话),它总是奇数(或质数,如果需要的话)? 本质上,我正在寻找一个散列函数,它将在 n 个桶上提供均匀分布,其中 n 是奇数(或质数,因为
我是一名优秀的程序员,十分优秀!