标签 C/C++ 下的文章

一
ifndef是 if not define 的缩写,一种宏定义。它是预处理功能中三种
(宏定义,文件包含和条件编译)中的第三种--条件编译

#ifndef

#define X

...

#endif


c语言在对程序进行编译时,会先根据预处理命令进行预处理,C语言编译系统包括预处理,

编译和链接等部分。

#ifndef X  //先测试是否被宏定义

#define X 

程序段1//如果X没有被宏定义过,定义X,并编译程序段1;

#else

程序段2 //如果X已经定义过了则编译程序段2 的语句,忽视程序段1.

#endif


ifndef和endif要一起使用,不能存在丢失。

假如你有一个C源文件,它包含了多个头文件,比如头文件A和头文件B,头文件B又包含了头文件A,
则最终该源文件包含了两次头文件A。
如果你在头文件A里定义了结构体或者类类型
那么问题来了,编译时会报大量的重复定义错误。
例如要编写头文件  eatline.h

//在头文件开头写上两行:

#ifndef EATLINE_H_      //一般是文件名的大写

#define EATLINE_H_      

//头文件结尾写上一行:

#endif

这样一个工程文件里同时包含两个test.h时,就不会出现重定义的错误了。
当第一次包含test.h时,由于没有定义_TEST_H,条件为真,
这样就会包含(执行)#ifndef _TEST_H和
#endif之间的代码,当第二次包含test.h时前面一次已经定义了_TEST_H,条件为假,
#ifndef _TEST_H和#endif之间的代码也就不会再次被包含,
这样就避免了重定义了.

--> 一ifndef是 if not define 的缩写,一种宏定义。它是预处理功能中三种(宏定义,文件包含和条件编译)中的第三种--条件编译#ifndef#define X...#endif二c语言在对程序进行编译时,会先根据预处理命令进行预处理,C语言编译系统包括预处理,编译和链接等部分。#ifndef X  //先测试是否被宏定义#define X 程序段1//如果X没有被宏定义过,定义X...

[label]进制转换[/label][alert]编写进制转换函数来接受两个参数,且第二个参数在 2~10 范围内,然后以第二个参数中指定的进制打印第一个参数的数值[/alert]

#include <stdio.h>
void to_base_n (int x, int base);//转换进制函数
int main()
{
    int number, b, count;
    printf("输入数字\n");
    while(scanf("%d",&number) == 1) //检测输入是否为数字
    {
        printf("输入 2~10 中的一个数字\n");
        while((count = scanf("%d",&b)) == 1 && (b < 2 || b > 10)) //检测输入是否为数字且在范围内
        {
            printf("输入需要在 2~10 范围内: ");
        }
        if (count != 1)
        break;
        printf("Base %d equivalent: ", b);
        to_base_n(number, b);
        putchar('\n');
        printf("输入一个数字 (q to quit):\n");
    }
    printf("Done!\n");
    return 0;
 }
 
void to_base_n (int x, int base)
{
    int r;
 
    r = x % base; //除以进制数求余
    if (x >= base)
    to_base_n(x / base, base); //递归
    putchar('0' + r);
    return;
}

--> [label]进制转换[/label][alert]编写进制转换函数来接受两个参数,且第二个参数在 2~10 范围内,然后以第二个参数中指定的进制打印第一个参数的数值[/alert]#include <stdio.h>void to_base_n (int x, int base);//转换进制函数 int main() { int number, b, count; ...

[label]前言[/label][alert] 混合使用 getchar() 和 scanf() 时,如果在调用 getchar() 之前,scanf() 在输入行留下一个换行符,会导致一些问题

编写程序时,要认真设计用户界面。事先预料一些用户可能会犯的错误,然后设计程序妥善处理这些错误情况[/alert]

#include <stdio.h>

char get_choice(void);       //菜单选择,获取获取用户输入
char get_first(void);        //处理换行符等,使其返回下一个非空白字符而不仅仅是下一个字符
int get_int(void);           //处理混合字符和数值输入
void count(void);

int main(void)
{
    int choice;
    void count(void);

    while ( (choice = get_choice()) != 'q')
    {
        switch (choice)
        {
            case 'a' : printf("Buy low, sell high.\n");
            break;
            case 'b' : putchar('\a'); /* ANSI */
            break;
            case 'c' : count();
            break;
            default : printf("Program error!\n");
            break;
        }
    }
    printf("Bye.\n");

    return 0;
}

void count(void)
{
    int n,i;

    printf("Count how far? Enter an integer:\n");
    n = get_int();
    for (i = 1; i <= n; i++)
    printf("%d\n", i);
    while ( getchar() != '\n')
    continue;
}

char get_choice(void)
{
    int ch;

    printf("Enter the letter of your choice:\n");
    printf("a. advice b. bell\n");
    printf("c. count q. quit\n");
    ch = get_first();
    while ( (ch < 'a' || ch > 'c') && ch != 'q')
    {
        printf("Please respond with a, b, c, or q.\n");
        ch = get_first();
    }

    return ch;
}

char get_first(void)
{
    int ch;

    ch = getchar();
    while (getchar() != '\n')
    continue;

    return ch;
}

int get_int(void)
{
    int input;
    char ch;

    while (scanf("%d", &input) != 1)
    {
        while ((ch = getchar()) != '\n')
        putchar(ch);                                //处理错误输出
        printf(" is not an integer.\nPlease enter an ");
        printf("integer value, such as 25, -178, or 3: ");
    }

    return input;
}

--> [label]前言[/label][alert] 混合使用 getchar() 和 scanf() 时,如果在调用 getchar() 之前,scanf() 在输入行留下一个换行符,会导致一些问题编写程序时,要认真设计用户界面。事先预料一些用户可能会犯的错误,然后设计程序妥善处理这些错误情况[/alert]#include <stdio.h>char get_choic...