经典编程900例(c语言)(第三篇)

例27:循环变量的变化造成的死循环

#include <stdio.h>

int main(int argc, char const *argv[])
{
	int i;
	int result = 0;
	int value = 1;

	// i自增
	for (i = 0; i < 100; i++)
	{
	  printf("%d ", i);
	  // value乘上减一后的i(i自减)
	  result = value * --i;   
	}

	printf("Result %d\n", result);

	// 程序一直打印0,因为i的值在每次循环时都是0

	return 0;
}

例28:int的打印宽度

#include <stdio.h>

int main(int argc, char const *argv[])
{
	int value = 5;

	// %1d打印宽度1(默认右对齐)
	printf ("%1d\n", value);

	// %1d打印宽度2
	printf ("%2d\n", value);

	// %1d打印宽度3
	printf ("%3d\n", value);

	// %1d打印宽度4
	printf ("%4d\n", value);

	return 0;
}

例29:int型的控制台打印输出

#include <stdio.h>

int main(int argc, char const *argv[])
{
	int age = 41;
	int height = 73;
	int weight = 165;

	printf("The user's age: %d weight: %d height: %d\n",age, weight, height);

	printf("%d plus %d equals %d\n", 1, 2, 1 + 2);

	return 0;
}

例30:while循环

#include <stdio.h>

int main(int argc, char const *argv[])
{
	// 初始化变量
	int counter = 1;

	// 循环条件
	while (counter <= 100)
	{
		printf("%d ", counter);

		// 循环变量的改变
		counter++;    
	}

	return 0;
}

例31:打印的对齐方式

#include <stdio.h>

int main(int argc, char const *argv[])
{
	int int_value = 5;
	float flt_value = 3.33;

	// 默认右对齐,'-'表示左对齐
	printf("Right justified %5d value\n", int_value);
	printf("Left  justified %-5d value\n", int_value);

	printf("Right justified %7.2f value\n", flt_value);
	printf("Left  justified %-7.2f value\n", flt_value);

	return 0;
}

例32:长整型的输出(看编译环境)

#include <stdio.h>

int main(int argc, char const *argv[])
{
	long int one_million = 1000000;

	printf ("One million is %ld\n", one_million);
	printf ("One million is %d\n", one_million);
	
	return 0;
}

例33:基础的计算输出

#include <stdio.h>

int main(int argc, char const *argv[])
{
	int seconds_in_an_hour;
	float average;
	
	// 一小时的秒数
	seconds_in_an_hour = 60 * 60;
	
	// 5, 10, 15, 20都平均值
	average = (5 + 10 + 15 + 20) / 4;
	
	printf("The number of seconds in an hour %d\n", seconds_in_an_hour);
	
	printf("The average of 5, 10, 15, and 20 is %f\n", average);
	
	// 48分钟的秒数
	printf("The number of seconds in 48 minutes is %d\n", seconds_in_an_hour - 12 * 60);

  return 0;
}

例34:取模(求余)运算

#include <stdio.h>

int main(int argc, char const *argv[])
{
	int remainder;
	int result;

	result = 10 / 3;
	remainder = 10 % 3;

	printf("10 Divided by 3 is %d Remainder %d\n", result, remainder);

	return 0;
}

例35:打印100以内的奇数和偶数

#include <stdio.h>
/**
 * 偶数能被2整除,奇数不能被2整除
 */
int main(int argc, char const *argv[])
{
	int counter;
	
	printf("\nEven values\n");
	for (counter = 1; counter <= 100; counter++)
	{
	  if (!(counter % 2))  // Even
	  printf("%d ", counter);
	}
	
	printf("\nOdd values\n");
	counter = 0;
	while (counter <= 100) 
	{
	  if (counter % 2) // Odd 
	    printf("%d ", counter);
	
	  counter++;
	}
	return 0;
}

例36:printf函数控制台打印

#include <stdio.h>

int main(int argc, char const *argv[])
{
	print ("This program does not link");

	return 0;
}

例37:没有stdio.h的输出

int main(int argc, char const *argv[])
{
	printf ("1001 C & C++ Tips");
	
	// gcc报错 error: 'printf' was not declared in this scope
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值