c:how to program2.3另一个简单的程序:两个整数相加

  我们下一个程序是用标注库中的scanf函数去获得用户输入的两个整型数,计算出他们的和并用printf打印出来,这个程序和输出在下面

//fig2.5
// Addition program. #include <stdio.h>
//function main begins program execution int main( void ) { int integer1; // first number to be entered by user int integer2; // second number to be entered by user int sum; // variable in which sum will be stored
printf( "Enter first integer\n" ); // prompt scanf( "%d", &integer1 ); // read an integer
printf( "Enter second integer\n" ); // prompt scanf( "%d", &integer2 ); // read an integer
sum = integer1 + integer2; // assign total to sum
printf( "Sum is %d\n", sum); } // end function main




Enter first integer 45 Enter second integer 72 Sum is 117

  第一行注释是说明函数目的

①变量和变量定义

  第8-10行,是定义,这些变量名存在内存中以备程序使用这些参数。这些定义描述了这三个变量是整型的,他们支持整型数据,-1,7,0,666等

  所有的变量必须得有个名字而且前面必须有数据类型才能在程序中使用,对于使用Microsoft VC++ complier编译的读者,谨记把变量定义放在前面,C标准允许把变量定义在该变量第一次使用前的任何一个位置,有些编译器比如gcc,实现了这个功能,我们会在后面的章节深度讨论这个问题。

  可以吧变量定义整合在一起,但是不方便描述这些变量具体是什么,不方便结合注释一起理解。

②标识符和大小写

  C中的变量名要保证有效。变量名室友字母数字下划线构成,不能以数字开头,C是大小写敏感的,同一个单词大写和小写在C中是不同的。NULL不同于null

③函数错误

④提示信息

⑤scanf函数与格式化输入

⑥打印格式控制流

⑦printf中的计算

 

posted on 2016-10-11 22:12  Gilfoyle 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/jieyaren/p/5950996.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++大学教程(第七版)的示例源代码,详细而规范。 例:// Fig. 2.5: fig02_05.cpp // Addition program that displays the sum of two integers. #include <iostream> // allows program to perform input and output // function main begins program execution int main() { // variable declarations int number1; // first integer to add int number2; // second integer to add int sum; // sum of number1 and number2 std::cout << "Enter first integer: "; // prompt user for data std::cin >> number1; // read first integer from user into number1 std::cout << "Enter second integer: "; // prompt user for data std::cin >> number2; // read second integer from user into number2 sum = number1 + number2; // add the numbers; store result in sum std::cout << "Sum is " << sum << std::endl; // display sum; end line } // end function main /************************************************************************** * (C) Copyright 1992-2010 by Deitel & Associates, Inc. and * * Pearson Education, Inc. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * **************************************************************************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值