|C - PROGRAMMING
C PROGRAMMING -Preprocessors- #define, #include
#define:
In the C Programming Language, the #define directive allows the definition of macros within your source code.
These macro definitions allow constant values to be declared for use throughout your code.
This macro defines constant value and can be any of the basic data types.
Syntax:
#define CNAME value Or #define CNAME (expression)
Syntax Explanation:
CNAME: CNAME is specified as a name of the constant. Most C programmers define their constant names in uppercase, but it is not a requirement of the C Language.
Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved. |C - PROGRAMMING
Value: value is represented as the value of the constant. Expression: Expression whose value is assigned to the constant. The expression must be enclosed in parentheses if it contains operators.
#include:
In the C Programming Language, the #include directive tells the preprocessor to insert the contents of another file into the source code at the point where the #include directive is found.
Include directives are typically used to include the C header files for
C functions that are held outsite of the current source file.
The source code of the file “file_name” is included in the main C program where “#include
Syntax:
#include
Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved. |C - PROGRAMMING
header_file: The name of the header file that you wish to
include.
A header file is a C file that typically ends in ".h" and contains declarations and macro definitions which can be shared between several source files.
Sample Code:
#include
#define NAME "www.wikitechy.com"
#define AGE 20 void main() { clrscr(); printf("%s is website", NAME); printf("\n"); printf(" %d years old", AGE); getch(); }
Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved. |C - PROGRAMMING
Code Explanation:
In C programming #include
which tells the compiler to insert the contents of stdio at that particular place. A header file is a C file, that typically ends in ".h".
In this example, #define NAME “www.wikitechy.com” specifies the
constant called NAME would contain the value of
“www.wikitechy.com”.
Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved. |C - PROGRAMMING
In this example, “#define AGE 20” specifies the constant
called AGE would contain the value of “20”. Sample Output:
Here in this output “www.wikitechy.com is website” represents the
constant called NAME would contain #define directive NAME.
Here in this output the statement “20 years old.” represents the
constant called AGE would contain #define directive AGE.
Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.