TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C Macros

C Macros with programming examples for beginners and professionals covering concepts, control statements, Object-like Macros, Function-like Macros, C Predefined Macros, C predefined macros example, c array, c pointers, c structures, c union, c strings and more.

<< Back to C

C Macros

A macro is a segment of code which is replaced by the value of macro. Macro is defined by #define directive. There are two types of macros:

  1. Object-like Macros
  2. Function-like Macros

Object-like Macros

The object-like macro is an identifier that is replaced by value. It is widely used to represent numeric constants. For example:

#define PI 3.14

Here, PI is the macro name which will be replaced by the value 3.14.

Function-like Macros

The function-like macro looks like function call. For example:

#define MIN(a,b) ((a)<(b)?(a):(b))  

Here, MIN is the macro name.

Visit #define to see the full example of object-like and function-like macros.


C Predefined Macros

ANSI C defines many predefined macros that can be used in c program.

No.MacroDescription
1_DATE_represents current date in "MMM DD YYYY" format.
2_TIME_represents current time in "HH:MM:SS" format.
3_FILE_represents current file name.
4_LINE_represents current line number.
5_STDC_It is defined as 1 when compiler complies with the ANSI standard.

C predefined macros example

File: simple.c

#include
 int main(){  
   printf("File :%s\n", __FILE__ );  
   printf("Date :%s\n", __DATE__ );  
   printf("Time :%s\n", __TIME__ );  
   printf("Line :%d\n", __LINE__ );  
   printf("STDC :%d\n", __STDC__ );    
   return 0;
 }  

Output:

File :simple.c
Date :Dec 6 2015
Time :12:28:46
Line :6
STDC :1
Next TopicC #include


Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf