例11:浮点数的科学计数法
#include <stdio.h>
int main(int argc, char const *argv[])
{
float value = 1.23456;
// 输出宽度12保留1位小数的科学计数法表示
printf ("%12.1e\n", value);
// 输出宽度12保留3位小数的科学计数法表示
printf ("%12.3e\n", value);
// 输出宽度12保留3位小数的科学计数法表示
printf ("%12.5e\n", value);
return 0;
}
例12:浮点数计算科学计数法
#include <stdio.h>
int main(int argc, char const *argv[])
{
// π
float pi = 3.14159;
// 半径
float radius = 2.0031;
// 根据半径计算周长=2*π*r
printf("The circle's area is %e\n", 2 * pi * radius); // The circle's area is 1.258584e+001
printf("The circle's area is %E\n", 2 * pi * radius); // The circle's area is 1.258584E+001
return 0;
}
例13:控制台输出函数
#include <stdio.h>
int main(int argc, char const *argv[])
{
// 调用函数(需包含stdio.h)
printf("1001 C & C++ Tips!"); // 1001 C & C++ Tips!
return 0;
}
例14:浮点数输出
#include <stdio.h>
int main(int argc, char const *argv[])
{
float price = 525.75;
float sales_tax = 0.06;
// %f打印浮点数
printf("The item cost is %f\n", price); // The item cost is 525.750000
printf("Sales tax on the item is %f\n", price * sales_tax);// Sales tax on the item is 31.544999
return 0;
}
例15:浮点数的输出控制
#include <stdio.h>
int main(int argc, char const *argv[])
{
float value = 1.23456;
// 输出宽度8保留1位小数
printf ("%8.1f\n", value);
// 输出宽度8保留3位小数
printf ("%8.3f\n", value);
// 输出宽度8保留5位小数
printf ("%8.5f\n", value);
return 0;
}
例16:%g在printf中的特点
#include <stdio.h>
int main(int argc, char const *argv[])
{
// %g根据数值的大小,自动选f格式或e格式(选择输出时占宽度较小的一种)
printf("Displaying 0.1234 yields %g\n", 0.1234);
printf("Displaying 0.00001234 yields %g\n", 0.00001234);
return 0;
}
例17:两个循环变量
#include <stdio.h>
int main(int argc, char const *argv[])
{
int i, j;
// i从1到100,j从100-200,每次+1,依次输出
for (i = 0, j = 100; i <= 100; i++, j++)
printf("i = %d j = %d\n", i, j);
return 0;
}
例18:循环变量的步长控制
#include <stdio.h>
int main(int argc, char const *argv[])
{
int counter;
// 从-100开始,小于等于100就循环,每次+5
for (counter = -100; counter <= 100; counter += 5)
printf("%d ", counter);
printf("\nStarting second loop\n");
// 从100开始,大于等于-100就循环,每次-25
for (counter = 100; counter >= -100; counter -= 25)
printf("%d ", counter);
return 0;
}
例19:循环条件的判断
#include <stdio.h>
int main(int argc, char const *argv[])
{
int counter;
for (counter = 5; counter >= 1; counter--)
printf("%d ", counter);
printf("\nStarting second loop\n");
for (counter = 10; counter >= 1; counter--)
printf("%d ", counter);
printf("\nStarting third loop\n");
// 从0开始,大于等于1就循环,每次-1,不打印
for (counter = 0; counter >= 1; counter--)
printf("%d ", counter);
return 0;
}
例20:char和float的for循环
#include <stdio.h>
int main(int argc, char const *argv[])
{
char letter;
float percent;
// 从A开始,小于等于Z就循环,每次+1
for (letter = 'A'; letter <= 'Z'; letter++) //ABCDEFGHIJKLMNOPQRSTUVWXYZ
putchar(letter);
// 从z开始,大于等于a就循环,每次-1
for (letter = 'z'; letter >= 'a'; letter--) // zyxwvutsrqponmlkjihgfedcba
putchar(letter);
putchar('\n'); // 换行
// 从0.0开始,小于1.0就循环,每次+0.1
for (percent = 0.0; percent < 1.0; percent += 0.1) // 0.0<换行>
printf("%3.1f\n", percent);
return 0;
}
例21:for循环
#include <stdio.h>
int main(int argc, char const *argv[])
{
int counter;
for (counter = 1; counter <= 5; counter++)
printf("%d ", counter);
printf("\nStarting second loop\n");
for (counter = 1; counter <= 10; counter++)
printf("%d ", counter);
printf("\nStarting third loop\n");
for (counter = 100; counter <= 5; counter++)
printf("%d ", counter);
return 0;
}
例22:输出格式控制
#include <stdio.h>
int main(int argc, char const *argv[])
{
int int_value = 5;
// -表示左对齐输出(默认右对齐),+表示带正负号,3表示宽度
printf("Left justifed with sign %-+3d##\n", int_value); // Left justifed with sign +5 ##
return 0;
}
例23:goto语句的使用
#include <stdio.h>
/*
* 这段代码可以实现循环,一个没有for和while的循环
* (学过汇编的同学可能会很熟悉)
*/
int main(int argc, char const *argv[])
{
int count = 1;
// 定义标记label,冒号后接语句
label:
printf("%d ", count++);
// 如果count小于等于100就跳到label标记处运行
if (count <= 100)
goto label;
return 0;
}
例24:一段难看的代码
#include <stdio.h>
int main(int argc, char const *argv[]){{printf("1001 C & C++ Tips!");}return 0;}
例25:一段好多注释的代码
/* Program: COMMENT.C*/
/* Written by: Kris Jamsa*/
/* Date written: 06-30-93*/
/* Purpose: Illustrates the use of comments in a C program.*/
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf ("1001 C & C++ Tips!"); /* Display a message*/
return 0;
}
例26:又一段好多注释的代码
/* Program: COMMENT.C
Written by: Kris Jamsa
Date written: 06-30-93
Purpose: Illustrates the use of comments in a C program. */
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf ("1001 C & C++ Tips!"); /* Display a message */
return 0;
}