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

本文通过多个实例展示了C语言中各种实用的编程技巧,包括不同进制的转换、控制台输出控制、位运算、变量地址获取等,帮助读者深入理解C语言的特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

例38:注释代码

#include <stdio.h>

int main(int argc, char const *argv[])
{
	// printf ("This line does not appear");

	/* This is a comment

	printf ("This line does not appear either");

	*/
	return 0;
}

例39:打印八进制、十六进制

#include <stdio.h>

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

   // %o输出八进制
   printf("The decimal value %d in octal is %o\n", value, value);

   // %x输出十六进制 小写字母
   printf("The decimal value %d in hexadecimal is %x\n", value, value);

   // %X输出十六进制 大写字母
   printf("The decimal value %d in hexadecimal is %X\n", value, value);
	return 0;
}

例40:打印奇数和偶数的另一种思路(第二篇例35)

#include <stdio.h>

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

    printf("\nEven values\n");
    for (counter = 1; counter <= 100; counter++)
    {
      if (counter % 2)  // Odd
        continue;

      printf("%d ", counter);
    }

    printf("\nOdd values\n");
    counter = 0;
    while (counter <= 100) 
    {
      counter++;

      if (! (counter % 2)) // Even 
        continue;

      printf("%d ", counter);
    }
    
    return 0;
}

例41:没有换行的控制台打印

#include <stdio.h>

int main(int argc, char const *argv[])
{
	printf ("This is line one.");
	printf ("This is the second line.");

	return 0;
}

例42:数值溢出问题

#include <stdio.h>

int main(int argc, char const *argv[])
{
    // short占两个字节,这里是能正确存的最大值和最小值
    short positive = 32767;
    short negative = -32768;

    // %hd表示短整型打印
    printf("%hd + 1 is %hd\n", positive, positive+1);
    printf("%hd - 1 is %hd\n", negative, negative-1);

    return 0;
}

例43:前置自减和后置自减的区别

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int value = 1;
   
    // 后置
    printf("Using postfix %d\n", value--); 
    printf("Value after decrement %d\n", value);

    value = 1;
    // 前置
    printf("Using prefix %d\n", --value); 
    printf("Value after decrement %d\n", value);
    
    return 0;
}

例44:浮点型的精确位数

#include <stdio.h>

int main(int argc, char const *argv[])
{
    float accurate = 0.123456790987654321;
    double more_accurate = 0.1234567890987654321;

    // 打印宽度21, 保留19位小数
    printf("Value of float\t %21.19f\n", accurate);         // Value of float   0.1234567910432815600
    printf("Value of double\t %21.19f\n", more_accurate);   // Value of double  0.1234567890987654400

    return 0;
}

例45:前置自增和后置自增的区别

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int value = 1;
   
    // 后置
    printf("Using postfix %d\n", value++); 
    printf("Value after increment %d\n", value);

    value = 1;
    // 前置
    printf("Using prefix %d\n", ++value); 
    printf("Value after increment %d\n", value);

    return 0;
}

例46:变量地址的打印

#include <stdio.h>

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

    // %p打印地址值
    printf("The address of the variable value is %p\n",  &value);

    return 0;
}

例47:_rotr循环右移函数和_rotl循环左移函数

#include <stdio.h>
#include <stdlib.h>
/**
 * 循环右移
 * 原数:00001111
 * 循环右移4位:11110000
 */
int main(int argc, char const *argv[])
{
    unsigned value = 1;

    // _rotr(value,1)表示value右循环移动1(需包含stdlib.h)
    printf("%u rotated right once is %u\n", value, _rotr(value, 1));

    value = 5;
    printf("%u rotated right twice is %u\n", value, _rotr(value, 2));

    value = 65534;
    // _rotl(value,2)表示value左循环移动2
    printf("%u rotated left twice is %u\n", value, _rotl(value, 2));

    return 0;
}

例48:左移和右移运算

#include <stdio.h>

int main(int argc, char const *argv[])
{
    unsigned u_val = 1;
    signed int value = -1;

    // 按位左移2位
    printf("%u (unsigned) shifted left 2 times is %u\n", u_val, u_val << 2);  // 1 (unsigned) shifted left 2 times is 4

    // 按位右移2位
    printf("%u (unsigned) shifted right 2 times is %u\n", u_val, u_val >> 2); // 1 (unsigned) shifted right 2 times is 0

    u_val = 65535;
    printf("%u (unsigned) shifted left 2 times is %u\n", u_val, u_val << 2);  // 65535 (unsigned) shifted left 2 times is 262140
    printf("%u (unsigned) shifted right 2 times is %u\n", u_val, u_val >> 2); //65535 (unsigned) shifted right 2 times is 16383
    printf("%d (signed) shifted left 2 times is %d\n", value, value << 2);    // -1 (signed) shifted left 2 times is -4
    printf("%d (signed) shifted right 2 times is %d\n", value, value >> 2);   //-1 (signed) shifted right 2 times is -1

    return 0;
}

例49:带有进制表示的输出

#include <stdio.h>

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

    // %#o表示输出带有进制符号的八进制(0开头)
    printf("The decimal value %d in octal is %#o\n", value, value);

    // %#x表示输出带有进制符号的十六进制(0x开头)
    printf("The decimal value %d in hexadecimal is %#x\n", value, value);
    printf("The decimal value %d in hexadecimal is %#X\n", value, value);

    return 0;
}

例50:带有正负号的打印输出

 #include <stdio.h>

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

    float neg_flt = -100.23;
    float pos_flt = 100.23;

    printf("The integer values are %+d and %+d\n", neg_int, pos_int);   // The integer values are -5 and +5
    printf("The floating point values are %+f %+f\n", neg_flt, pos_flt);// The floating point values are -100.230003 +100.230003

    return 0;
}

例51:语法错误

#include <stdio.h>

int main(int argc, char const *argv[])
{
    printf("1001 C & C++ Tips!);
        
    return 0;
}

例52:两行代码,打印两行

#include <stdio.h>

int main(int argc, char const *argv[])
{
    printf ("This is line one.\n");
    printf ("This is the second line.");

    return 0;
}

例53:不是语法错误,也不是逻辑错误

#include <stdio.h>

int Main(int argc, char const *argv[])
{
    printf ("This program does not compile.");

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值