实验2

#include <stdio.h>
int main() {
    int a=5, b=7, c=100, d, e, f;

    d = a/b*c;
    e = a*c/b;
    f = c/b*a;
    
    printf("d=%d, e=%d, f=%d
",d,e,f);

    return 0;
}

d=[5/7]*100

e=[5*100/7]

f=[100/7]*5

原因:计算结果取的是十进制整数

#include <stdio.h>
int main() {
    int x=1234;
    float f=123.456;
    double m=123.456;
    char ch='a';
    char a[]="Hello, world!"; 
    int y=3, z=4; 
    
    printf("%d %d
", y, z);
    printf("y=%d, z=%d
", y,z);
    printf("%8d,%2d
", x,x);
    printf("%f, %8f, %8.1f, %0.2f, %.2e
",f,f,f,f,f);
    printf("%lf
",m);
    printf("%3c
", ch);
    printf("%s
%15s
%10.5s
%2.5s
%.3s
",a,a,a,a,a);
    
    return 0;
}

%后数字为字段宽度

s按字符串输出

f按浮点数输出

d按十进制整数输出

#include <stdio.h>
int main() 
{
    double x,y;
    char c1,c2,c3;
    int a1,a2,a3;
    scanf("%d%d%d",&a1,&a2,&a3);
    printf("%d,%d,%d
",a1,a2,a3);
    scanf("%c%c%c",&c1,&c2,&c3);
    printf("%c%c%c
",c1,c2,c3);
    scanf("%lf,%lf",&x,&y);
    printf("%lf,%lf
",x,y);
    
    return 0;
} 

#include <stdio.h>
int main() {
    char x;
    
    x = getchar();
    
    if(x>='0'&&x<='9') 
        printf("%c是数字字符
", x);
    else if(x>=65&&x<=90||x>=97&&x<=122)
        printf("%c是英文字母
", x);
    else
        printf("%c是其它字符
", x);
    
    
    return 0;
} 

 

#include <stdio.h>
int main() {
    char ans1, ans2;
    
    printf("复习了没? (输入y或Y表示复习了,输入n或N表示没复习) :  ");
    ans1 = getchar(); 
    
    getchar();
    
    printf("
动手敲代码了没? (输入y或Y表示敲了,输入n或N表示木有敲) :  ");
    ans2 = getchar();
    
    if(ans1=='Y'||ans1=='y'&&ans2=='Y'||ans2=='y')
        printf("
罗马不是一天建成的:)
");    
    else
        printf("
罗马不是一天毁灭的。。。
");

    return 0;
} 

 

#include<stdio.h>
#include<math.h>
int main(){
    int n,sum;
    scanf("%d",&n);
    sum=1*(1-pow(2,n))/(1-2);
    printf("n = %d时,sum = %d
",n,sum);
    return 0;
}

原文地址:https://www.cnblogs.com/ruanfandd/p/13900512.html