求序列和的简单C程序
对于序列求和的程序大家不会陌生,而我今天看到的这个程序个人觉得比较好,所以贴上来共享一下。
要求:输入类似于87 98 67 56 0的任意序列,但是以零结尾。然后输出序列的和。
程序如下 :
/********************************************
* Name : sum.c
* Purpose : sum
* Author : zimo
* Date : 01/21/2010
* *****************************************/
#include <stdio.h>
int main(void)
{
int n , sum = 0;
printf("This program sums a series of integers. \n");
printf("Enter integers (0 to terminate): ");
scanf("%d", &n);
while(n != 0)
{
sum += n;
scanf("%d", &n);
}
printf("The sum is: %d \n", sum);
return 0;
}
两次的输入语句个人觉得非常好。(所输入的序列要求必须是最后一个数为零,当然也可以简单的修改)
相关文档:
1、隐式转换
C在以下四种情况下会进行隐式转换:
1、算术运算式中,低类型能够转换为高类型。
2、赋值表达式中,右边表达式的值自动隐式转换为左边变量的类型,并赋值给他。
& ......
#include <assert.h> //设定插入点
#include <ctype.h> //字符处理
#include <errno.h> //定义错误码
#include <float.h> //浮点数处理
#include <fstream.h> //文件输入/输出
#include <iomanip.h> //参数化输入/输出
#include & ......
函数名: stpcpy
功 能: 拷贝一个字符串到另一个
用 法: char *stpcpy(char *destin, char *source);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
stpcpy(string, str1);
& ......
作者:刘洪涛,华清远见嵌入式学院讲师。
四、在内核里写i2c设备驱动的两种方式
前文介绍了利用/dev/i2c-0在应用层完成对i2c设备的操作,但很多时候我们还是习惯为i2c设备在内核层编写驱动程序。目前内核支持两种编写i2c驱动程序的方式。下面分别介绍这两种方式的实现。这里分别称这两种方式为“Adapter方式(LEGAC ......
出于性能的考虑,标准c库中的某些函数是以宏的方式实现的。
大部分情况下这都是一个有益的方式,但是在跟踪或调试程序时,可能会使你产生困惑。
此时可以使用undef来避免这个问题。
例:
默认情况下
#include <ctype.h>
some code...
....
isspace ......