Home >Backend Development >C#.Net Tutorial >Usage of #define in c language

Usage of #define in c language

angryTom
angryTomOriginal
2020-03-09 17:16:5315527browse

Usage of #define in c language

Usage of #define in C language

In C language, you can use #define to define an identifier to represent a constant.

Its characteristic is: the defined identifier does not occupy memory, it is just a temporary symbol, and this symbol will no longer exist after precompilation.

Precompilation is also called preprocessing. Precompilation is not compilation, but processing before compilation. This operation is automatically completed by the system before formal compilation. The format of

#define is:

#define  标识符  常量

Recommended learning: c language video tutorial

In order to mark the To distinguish identifiers from variable names, it is customary to use all uppercase letters for identifiers. The most commonly used place for macro definitions is in arrays to specify the length of the array.

Write a program below:

# include <stdio.h>
# define NUM 5
int main(void)
{
    int i, j = NUM;
    int a[NUM] = {0};
    printf("请输入%d个数:", j);
    for (i=0; i<NUM; ++i)
    {
        scanf("%d", &a[i] );
    }
    for (i=0; i<NUM; ++i)
    {
        printf("%d\x20", a[i]);
    }
    printf("\n");
    return 0;
}

The output result is:

请输入5个数:5 4 3 2 1
5 4 3 2 1

NUM is a defined macro, which represents the following constant (not variable). In addition, macros enclosed in double quotes in the program will not be replaced by macros during preprocessing. Because in C language, a string is represented by double quotes.

For more C language tutorials, please pay attention to PHP Chinese website!

The above is the detailed content of Usage of #define in c language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn