C Predefined Macros 🎯

beginner
18 min

C Predefined Macros 🎯

Welcome to our comprehensive guide on C Predefined Macros! This lesson is designed to help you understand one of the powerful features of the C programming language. By the end of this lesson, you'll be able to use predefined macros confidently in your code. Let's get started!

What are Predefined Macros? 📝

In C programming, macros are text replacements that allow you to define shortcuts for frequently used code segments. Predefined macros are provided by the C standard library and can be very useful in writing efficient, readable, and maintainable code.

Why Use Predefined Macros? 💡

  • Code Reusability: Macros help you write less code and reuse existing code segments, making your programs more efficient.
  • Readability: By defining meaningful names for complex code segments, macros make your code easier to read and understand.
  • Efficiency: Macros can sometimes be more efficient than functions, especially when they avoid function call overhead.

List of Predefined Macros 📝

Here are some commonly used predefined macros in C programming:

  1. NULL: Represents a null pointer.
  2. FALSE: Represents false boolean value.
  3. TRUE: Represents true boolean value.
  4. sizeof: Returns the size of a data type or variable in bytes.
  5. func: Provides the name of the current function.
  6. LINE: Provides the line number of the current line in the source code.
  7. FILE: Provides the name of the current source file.

Examples 💡

Example 1: Using sizeof to determine the size of a data type

c
#include <stdio.h> int main() { int myInt = 10; float myFloat = 3.14; printf("Size of an int: %ld bytes\n", sizeof(int)); printf("Size of a float: %ld bytes\n", sizeof(float)); return 0; }

Example 2: Using __func__, __LINE__, and __FILE__ for debugging

c
#include <stdio.h> void myFunction() { printf("Function: %s\n", __func__); printf("Line: %d\n", __LINE__); printf("File: %s\n", __FILE__); } int main() { myFunction(); return 0; }

Quiz 💡

Quick Quiz
Question 1 of 1

What does the `NULL` predefined macro represent?

By understanding and using predefined macros, you'll be well on your way to writing efficient and maintainable C code! Happy coding! 💻🚀