27. 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;
}

 

Solution Explanation

  • Two separate namespaces are declared: UART and SPI.
  • Their variables are accessed with UART:: and SPI::.

 

Layman’s Terms

Think of two folders: one has UART settings, the other SPI settings. You open each by prefixing with the folder name.

 

Significance for Embedded Developers

This mirrors real embedded projects, where UART, SPI, I²C configs, etc., are grouped separately to avoid mix-ups.

Loading...

Input

Expected Output

UART Baud: 115200 SPI Clock: 1000000