C算法--入门 2.1

 1 #include <stdio.h>
 2 
 3 
 4 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 5 /*输出数据1 换行2 输出数据2 ;计算机响8下*/
 6 int main() {
 7     int num1=1,num2=2;
 8     printf("%d\n\n%d",num1,num2); 
 9     printf("%c",7);
10     printf("%c",7);
11     printf("%c",7);
12     printf("%c",7);    
13     printf("%c",7);
14     printf("%c",7);
15     printf("%c",7);
16     printf("%c",7);
17     return 0;
18 }
转义字符
 1 #include <stdio.h>
 2 
 3 
 4 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 5 /*输出数据1 换行2 输出数据2 ;计算机响8下*/
 6 int main(){
 7     char str1[25]="h e l l o";
 8     char str2[25]="happy birthday !";
 9     printf("%s,%s",str1,str2);
10     return 0;
11 } 
字符数组
#include <stdbool.h>
/*若不写#include <stdbool.h> 需要用.cpp文件运行*/
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(){
    bool flag1=0,flag2=true;
    int a=1,b=1;
    printf("%d %d %d\n",flag1,flag2,a==b);
    return 0;
} 
布尔型
 1 #include <stdio.h>
 2 
 3 
 4 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 5 
 6 int main(){
 7     double r=12.56;
 8     int a =3, b=5;
 9     printf("%d\n",(int)r);
10     printf("%d\n",a/b);
11     printf("%.2f",(double)a/(double)b);
12     /*以两位小数输出*/
13     return 0;
14 } 
强制类型转换
 1 #include <stdio.h>
 2 #define pi 3.14
 3 
 4 /*define 标识符 常量*/ 
 5 int main(){
 6     double r=3;
 7     printf("%f\n",pi*r*r);
 8     return 0;
 9     
10 } 
符号常量
 1 #include <stdio.h>
 2 
 3 const double  pi=3.14;
 4     
 5 /*const 标识符 常量*/ 
 6 int main(){
 7     double r=3;
 8     printf("%f\n",2*pi*r);
 9     return 0;
10 }
const常量
 1 #include <stdio.h>
 2 #define ADD(a,b) ((a)+(b)) 
 3 
 4 /*define 标识符 任何语句或片段*/
 5 /*#define ADD(a,b) ((a)+(b)) */ 
 6 int main(){
 7     int num1=3,num2=5;
 8     printf("%d",ADD(num1,num2));
 9     return 0;
10 }
define定义语句片段

注:宏定义直接将对应部分替换

在scanf中除了 char数组整个输入的情况不加&之外,其他变量类型都需要加&(无需特地记忆,都加)

#include <stdio.h>
#define ADD(a,b) ((a)+(b)) 

/*123不足五位 前面自动用两个空格填充,凑足五位;1234567大于五位 直接输出*/
int main(){
    int a=123,b=1234567;
    printf("%5d\n",a);
    printf("%5d\n",b);
    return 0;
}
%md右对齐输出
1 #include <stdio.h>
2 
3 /*123用空格补齐*/
4 int main(){
5     int a =123,b=1234567;
6     printf("%05d\n",a);
7     printf("%05d\n",b);
8     return 0; 
9 }
用空格补齐
 1 #include <stdio.h>
 2 
 3 /*浮点数保留到小数点后几位*/
 4 int main() {
 5     double d1=12.3456;
 6     printf("%.0f\n",d1);
 7     printf("%.1f\n",d1);
 8     printf("%.2f\n",d1);
 9     printf("%.3f\n",d1);
10     printf("%.4f\n",d1);
11     return 0;
12 }
保留到小数点后几位
 1 #include <stdio.h>
 2 
 3 /*输入的第二个字符被接受但未被存储*/
 4 int main() {
 5     char c1,c2,c3;
 6     c1=getchar();
 7     getchar();
 8     c2=getchar();
 9     c3=getchar();
10     putchar(c1);
11     putchar(c2);
12     putchar(c3);
13     return 0;
14 }
getchar/putchar输入输出字符
1 #include <stdio.h>
2 
3 /*给long long 起个别名LL*/
4 typedef long long LL;
5 int main(){
6     LL a =123456789012345, b=234567890123456;
7     printf("%lld\n",a+b);
8     return 0;
9 } 
typedef定义别名

猜你喜欢

转载自www.cnblogs.com/Catherinezhilin/p/11131129.html
2.1