zoukankan      html  css  js  c++  java
  • C语言之运算符、表达式和语句

    #include<stdio.h>
    #define ADJUST 7.31
    
    int main(void)
    {
    	const double SCALE = 0.333;
    	double shoe, foot;
    	printf("Shoe size foot length
    ");
    	shoe = 3.0;
    	while (shoe < 18.5) 
    	{
    		foot = SCALE * shoe + ADJUST;
    		printf("%10.1f %15.2f inches
    ", shoe, foot);
    		shoe = shoe = 1.0;
    	}
    	printf("If the shoe fits,wear it.
    ");
    	system("pause");
    	return 0;
    }
    /*该代码会一直运行下去*/
    

    基本运算符

    赋值运算符
    #include<stdio.h>
    
    int main(void)
    {
    	int jane, tarzan, cheeta;
    	cheeta = tarzan = jane = 68;
    	printf("cheeta  tarzan  jane
    ");
    	printf("First round score %4d  %8d  %8d
    ", cheeta, tarzan,jane);
    	system("pause");
    	return 0;
    }
    
    加法运算符
    printf("%d",4+20);
    
    减法运算符
    someone = 22-12;
    
    符号运算符
    value = -1;
    value = +1;
    
    乘法运算符
    cm = 2.5*inch;
    
    #include<stdio.h>
    
    int main(void)
    {
    	int num = 1;
    	while (num < 21)
    	{
    		printf("%4d %6d
    ", num, num*num);
    		num = num + 1;
    	}
    	system("pause");
    	return 0;
    }
    
    除法运算符
    value_2 = 6.0/3.0
    

    浮点数除法的结果是浮点数,而整数除法的结果是整数。在浮点数和整数的运算中,会把整数先化为浮点数。

    #include<stdio.h>
    
    int main(void)
    {
    	printf("integer division:5/4 is %d
    ", 5 / 4);
    	printf("float division:7/4 is %1.2f
    ", 7. / 4.);
    	system("pause");
    	return 0;
    }
    
    /*
    result:
    integer division:5/4 is 1
    float division:7/4 is 1.75
    */
    

    其他运算符

    sizeof运算符和size_t类型

    sizeof运算符以字节为单位返回对象的大小。

    #include<stdio.h>
    
    int main(void)
    {
    	int n = 0;
    	size_t intsize;
    	intsize = sizeof(int);
    	printf("n = %d, n has %zd bytes; all ints have %zd bytes.
    ", n, sizeof n, intsize);
    	system("pause");
    	return 0;
    }
    /*
    result:
    n = 0, n has 4 bytes; all ints have 4 bytes.
    */
    
    求模运算符%
    #include<stdio.h>
    #define SEC_PER_MIN 60
    
    int main(void)
    {
    	int sec, min, left;
    	printf("Convert seconds to minutes and seconds!
    ");
    	printf("Enter the number of seconds(<=0 to quit):
    ");
    	scanf_s("%d", &sec);
    	while (sec > 0)
    	{
    		min = sec / SEC_PER_MIN;
    		left = sec % SEC_PER_MIN;
    		printf("%d seconds is %d minutes,%d seconds.
    ", sec, min, left);
    		printf("Enter next value(<=0 to quit):");
    		scanf_s("%d", &sec);
    	}
    	printf("Done!
    ");
    	return 0;
    }
    
    递增运算符
    #include<stdio.h>
    
    int main(void)
    {
    	int a = 1, b = 1;
    	int a_post, pre_b;
    	a_post = a++; //后缀递增
    	pre_b = ++b;  //前缀递增
    	printf("a:%ld a_post:%d b:%d pre_b:%d
    ",a,a_post,b,pre_b);
    	system("pause");
    	return 0;
    }
    /*
    result:
    a:2 a_post:1 b:2 pre_b:2
    */
    
    递减运算符
    #include<stdio.h>
    
    int main(void)
    {
    	int a = 100, b = 100;
    	int a_post, pre_b;
    	a_post = a--;
    	pre_b = --b;
    	printf("a:%ld a_post:%d b:%d pre_b:%d
    ",a,a_post,b,pre_b);
    	system("pause");
    	return 0;
    }
    /*
    result:
    a:99 a_post:100 b:99 pre_b:99
    */
    
    优先级

    只有圆括号的优先级比递增/递减运算符优先级高,而且其只影响一个可修改的左值。

    表达式和语句

    表达式

    表达式是由运算符和运算符对象组成,一些表达式由子表达式组成。还有一些表达式是逻辑表达式,其值不是0就是1.

    语句

    语句是C语言程序的基本构建块,一条语句相当于一条完整的计算机指令,且大多数都是以分号结尾。

    副作用:如state=50,C会对其求值,而对该表达式求值的副作用就是把变量states的值改为50.
    序列点:在C语言中,语句中的分号标记了一个序列点。意思是,在一个语句中,赋值运算符、递增运算符和递减运算符对运算对象做的改变必须在程序执行下一条语句之前完成。
    
    符合语句

    符合语句是用花括号括起来的一条或多条语句,复合语句也称为块。

    类型转换

    基本规则
    1. 当类型转换出现在表达式时,无论是unsigned还是signed的char和short都会被自动转换成int,如有必要会被转换成unsigned int(如果short与int的大小相同,unsigned short就比int大。这种情况下,unsigned short会被转换成unsigned int)。在K&R那时的C中,float会被自动转换成double(目前的C不是这样)。由于都是从较小类型转换为较大类型,所以这些转换被称为升级。
    2. 涉及两种类型的运算,两个值会被分别转换成两种类型的更高级别。
    3. 类型的级别从高至低依次是long double、double、float、unsigned long long、long long、unsigned long、long、unsigned int、int。例外的情况是,当long 和 int 的大小相同时,unsigned int比long的级别高。之所以short和char类型没有列出,是因为它们已经被升级到int或unsigned int。
    4. 在赋值表达式语句中,计算的最终结果会被转换成被赋值变量的类型。这个过程可能导致类型升级或降级(demotion)。所谓降级,是指把一种类型转换成更低级别的类型。
    5. 当作为函数参数传递时,char和short被转换成int,float被转换成double。
    待赋值的值与目标类型不匹配时的规则
    1. 目标类型是无符号整型,且待赋的值是整数时,额外的位将被忽略。例如,如果目标类型是 8 位unsigned char,待赋的值是原始值求模256。
    2. 如果目标类型是一个有符号整型,且待赋的值是整数,结果因实现而异。
    3. 如果目标类型是一个整型,且待赋的值是浮点数,该行为是未定义的。
    强制类型转换运算符
    mice = (int)1.2+(int)2.3
    

    带参数的函数

    #include<stdio.h>
    void pound(int n);
    
    int main(void)
    {
    	int times = 5;
    	char ch = '!';
    	float f = 6.0f;
    	pound(times);
    	pound(ch);
    	pound(f);
    	system("pause");
    	return 0;
    }
    
    void pound(int n) //函数声明
    {
    	while (n-- > 0)
    		printf("#");
    	printf("
    ");
    }
    
  • 相关阅读:
    P1144 最短路计数 题解 最短路应用题
    C++高精度加减乘除模板
    HDU3746 Teacher YYF 题解 KMP算法
    POJ3080 Blue Jeans 题解 KMP算法
    POJ2185 Milking Grid 题解 KMP算法
    POJ2752 Seek the Name, Seek the Fame 题解 KMP算法
    POJ2406 Power Strings 题解 KMP算法
    HDU2087 剪花布条 题解 KMP算法
    eclipse创建maven项目(详细)
    maven的作用及优势
  • 原文地址:https://www.cnblogs.com/MingleYuan/p/10628529.html
Copyright © 2011-2022 走看看