1数据结构

2019-01-19  本文已影响0人  别花春水
#include <stdio.h> //standard input & output
#include <stdlib.h> //pause

def

#define PRICE 30;  
void main()
{
    int num, total;
    num = 10;
    total = num * PRICE;
    printf("total=%d\n", total); 
    system("pause");
}

sizeof 看int的字节是4

void main()
{
    printf("%d\n",sizeof(int)); 
    //short int,float,long int
    system("pause");
}

赋值计算,自右向左计算。

void main()
{
    int a, b, c, d;
    unsigned u;
    a = 12;
    b = -24;
    u = 10;
    c = a + u;
    d = b + u;
    printf("a+u=%d,\nb+u=%d\n", c, d);
    printf("%d+%d=%d,%d+%d=%d\n",a, u, c, b, u, d);
    system("pause");
}

数据溢出 舍入误差

float浮点数据

void main()
{
    float a, b;
    a = 12e2;
    b = 1200.;
    printf("%f\n",a); //%float
    printf("%f\n",b);
    system("pause");
}

\n换行 \t制表位 \b退格 \r回车 \f走纸换页 \a鸣铃

void main()
{
    int a, b, c;
    a = 5; b = 6; c = 7;
    printf("ab c\tde\rf\n"); //f被放在第一个字母,置换了a
    printf("hijkL\bM\n"); //L被删除了
    system("pause");
}

char

void main()
{
    char a, b;
    a = 120;    //code120=x
    b = 121;    //code121=y
    printf("ab\ncdsf\n");
    printf("%c,%c\n", a, b); //ASCII码:%character,输出每个数字代表的符号
    printf("%d,%d\n", a, b); //%data,输出数字
    system("pause");
}

void main()
{
    char a, b;
    a = 'x';
    b = 'y';
    printf("%c,%c\n", a, b);    //x,y
    printf("%d,%d\n", a, b);    //120,121
    system("pause");
}

小写字母转换为大写

void main()
{
    char a, b;
    a = 'a';
    b = 'b';
    a=a-32;
    b=b-32;
    printf("%c,%c\n%d,%d\n", a, b,a,b); //字符;整形
    system("pause");
}

void main()
{
    char a;
    int b;
    a = 33; //对应同一个符号
    b = 33; //对应同一个符号
    a=a-32;
    b=b-32;
    printf("%c,%c\n%d,%d\n", a, b,a,b); //字符;整形
    system("pause");
}

字符串常量

/*"CHINA" "C program"
可以char a='a'但不能char a="a"*/
/*
int a=3;
int b,c=5;
float x=3.2,y=3.,z=0.75;
char ch1='K',ch2='P';
*/

临时强制类型转换

(float)a
(int)(x+y)

void main()
{
    float f = 5.57;
    printf("(int)f=%d,f=%f\n",(int)f,f);
    system("pause");
}


void main()
{
    printf("%d\n",3/2);
    system("pause");
}
/*printf("%d\n",3/2);
3/2==1;
3/4==0;
    printf("%f\n",3.0/4);==0.75
    printf("%d\n",7%2);==1  //%是mod,取余数
    printf("%d,%d\n",20/7,-20/7);==2,-2
    printf("%f,%f\n", 20.0 / 7, -20.0 / 7);==2.8**,-2.8**

*/

运算符的优先级

自增、自减运算符

/*
++i i自增1后再参与运算
--i i自减1后再参与运算
i++ i参与运算后,i的值再自增1
i-- i参与运算后,i的值再自减1
*/

void main()
{
    int i = 8;
    printf("%d\n",++i); //9
    printf("%d\n", --i);    //8
    printf("%d\n", i++);    //8
    printf("%d\n", i--);    //9
    printf("%d\n", -i++);   //-8
    printf("%d\n", -i--);   //-9
    system("pause");
}

不同fmt的变量互相赋值

void main()
{
    int a, b = 322;
    float x, y = 8.88;
    char c1 = 'k', c2;
    a = y;
    x = b;
    a = c1; //第二次赋值覆盖了前一个值
    c2 = b;
    printf("%d,%f,%d,%c", a,x,a,c2);
    system("pause");
}

复合赋值(缩写)

/*
a+=5    a=a+5
x*=y+7  x=x*(y+7)
r%=p    r=r%p
*/

逗号运算符-->取逗号右边的值

/*分别求n个表达式的值,并以最后一个表达式的结果赋值给y*/
void main()
{
    int a = 2, b = 4, c = 6, x, y;
    y = ((x = a + b), (b + c)); //y=10,x=6
    printf("y=%d,x=%d",y,x);
    system("pause");
}
上一篇 下一篇

猜你喜欢

热点阅读