Home > Backend Development > C++ > In C language, what does conditional compilation mean?

In C language, what does conditional compilation mean?

WBOY
Release: 2023-09-05 17:29:05
forward
709 people have browsed it

In C language, what does conditional compilation mean?

In the C programming language, there are several instructions that control the selective compilation of program code. They are as follows −

  • #if
  • #else
  • #elif
  • #endif

# The general form of if is as follows −

#if constant_expression
   statement sequence
#endif
Copy after login

#else works similarly to the C keyword else.

#elif means "else if" and establishes an if else-if compilation chain.

Among other things, #if provides an alternative to "commenting out" code.

For example,

#if 0
   printf("#d", total);
#endif
Copy after login

Here, the compiler will ignore printf("#d", total);

#ifdef and #ifndef

#ifdef means "if defined" and ends with #endif.

#ifdef means "if not defined".

#undef

#undef deletes a previously defined definition.

#line

#line changes the contents of __LINE__, which contains the line number of the currently compiled code, and __FILE__, which is a source file containing the source file being compiled A string of names. Both of these are identifiers predefined in the compiler.

#pragma

#The pragma directive is an implementation-defined directive that allows various directives to be provided to the compiler.

Example

The following is a C programExample demonstrating #ifdef, #ifndef, #else and #endif-

Live Demo

# include <stdio.h>
# define a 10
void main(){
   #ifdef a
   printf("</p><p> Hello I am here..");
   #endif
   #ifndef a
   printf("</p><p> Not defined ");
   #else
   printf("</p><p> R u There ");
   #endif
}
Copy after login

Output

When the above program is executed, it produces the following results −

Hello I am here..
R u There
Copy after login

The above is the detailed content of In C language, what does conditional compilation mean?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template