36. Declaring Multiple Namespaces

#include <iostream>
using namespace std;

namespace UART {
    int baudRate = 115200;
}

namespace SPI {
    int clockSpeed = 1000000;
}

int main() {
    cout << "UART Baud: " << UART::baudRate << "\n";
    cout << "SPI Clock: " << SPI::clockSpeed << "\n";
    return 0;
}

Explanation & Logic Summary

Two independent namespaces—UART and SPI—are declared to hold configuration variables.
The scope resolution operator (::) is used to access each variable explicitly, ensuring no naming conflicts.

Layman’s Terms

Think of namespaces like labeled folders. One folder holds UART settings, another holds SPI settings. You open each folder by using its name.

Firmware Relevance & Real-World Context

In real embedded systems, peripheral configurations (UART, SPI, I²C, etc.) are often grouped logically. Namespaces help keep these configurations organized, readable, and safe from symbol collisions in large firmware codebases.

 

 

 

 

 

Loading...

Input

Expected Output

UART Baud: 115200 SPI Clock: 1000000