Category: Embedded C Challenges

  • Weak Symbols vs Strong Symbols in C | Master Embedded Interview Preparation (2026)

    Weak Symbols vs Strong Symbols : When working with C programming, especially in embedded systems or system-level development, you will often encounter the concepts of weak symbols and strong symbols. These terms come into play during the linking stage of compilation. Understanding them is crucial for writing flexible, modular, and maintainable code.

    In this article, we’ll explore the difference between weak symbols and strong symbols in C, how they work, and where they are used.

    What is a Weak symbol and Strong Symbols in C ?

    In C programming, symbols represent functions or variables that the linker resolves during compilation. Strong symbols are the default definitions of global variables or functions, and only one strong symbol with the same name can exist across a program. If multiple strong symbols are present, the linker throws a multiple-definition error.

    On the other hand, weak symbols are declared using the __attribute__((weak)) attribute. They act as fallback definitions, meaning if a strong symbol with the same name is available, it overrides the weak symbol. If no strong definition exists, the weak symbol is used.

    This mechanism is particularly useful in embedded systems, libraries, and modular applications, where weak symbols provide default implementations (such as interrupt handlers or library functions) that can be optionally overridden by the developer.

    Key difference:

    • Strong symbols = Default and unique definitions.
    • Weak symbols = Flexible fallback definitions, overridden by strong symbols if available.

    What are Symbols in C?

    In simple terms, a symbol is the name that represents a function or variable in your program. For example:

    int myVar = 10;   // 'myVar' is a symbol
    void myFunction() {} // 'myFunction' is a symbol
    

    When you compile your code, the compiler generates object files with these symbols. The linker then resolves them to produce the final executable.

    Strong symbols in C

    A strong symbol is the default type of symbol generated by the compiler when you define a global variable or function.

    👉 Characteristics of strong symbols:

    • Created when you define a function or variable without the weak attribute.
    • Only one definition of a strong symbol can exist across the program.
    • If two strong symbols with the same name are defined in different files, the linker will throw an error (multiple definition error).

    Example of Strong Symbol

    int data = 100;  // Strong symbol by default
    
    void printData() {
        printf("%d\n", data);
    }
    

    Here, data and printData are strong symbols.

    Weak Symbols in C

    A weak symbol is a symbol that tells the linker:
    “Use this symbol if no strong definition is found, otherwise ignore me.”

    👉 Characteristics of weak symbols:

    • Declared using the __attribute__((weak)) keyword.
    • Can be overridden by a strong symbol with the same name.
    • If no strong symbol is provided, the weak symbol is used as a fallback.

    Example of Weak Symbol

    __attribute__((weak)) void myFunction() {
        printf("Default weak function\n");
    }
    

    If another file defines a strong version of myFunction, the weak version will be ignored by the linker.

    Weak vs Strong Symbols in C

    FeatureStrong SymbolWeak Symbol
    Default behaviorYesNo (explicitly defined)
    Multiple definitionsNot allowed (linker error)Allowed, but overridden by strong
    Use caseNormal variables/functionsFallbacks, optional overrides
    Example useint var = 10;__attribute__((weak)) void func()

    Why Use Weak Symbols?

    Weak symbols are very useful in system programming and embedded systems:

    1. Fallback Implementation
      • Provide a default implementation of a function.
      • Example: weak logging functions can be overridden by user-defined strong ones.
    2. Flexibility in Libraries
      • Library authors often define weak symbols so developers can override them with custom implementations.
    3. Bootloaders & Firmware
      • In embedded systems, weak symbols are used for interrupt handlers or startup code, which can later be overridden by user-defined handlers.

    Practical Example – Weak vs Strong

    // weak_function.c
    #include <stdio.h>
    __attribute__((weak)) void hello() {
        printf("Hello from weak function!\n");
    }
    
    // strong_function.c
    #include <stdio.h>
    void hello() {
        printf("Hello from strong function!\n");
    }
    
    // main.c
    int main() {
        hello();
        return 0;
    }
    

    👉 If you compile all three files together:

    • Output will be: “Hello from strong function!”
    • The weak symbol is ignored because a strong definition exists.

    👉 If you compile without strong_function.c:

    • Output will be: “Hello from weak function!”

    This demonstrates the priority of strong symbols over weak symbols.

    Weak Symbols vs Strong Symbols

    In C programming, symbols are names that represent functions or variables during the compilation and linking stages. The linker decides how these symbols are resolved. Based on their definition, they can be classified as strong symbols or weak symbols.

    Strong Symbols in C

    • Definition: Default symbols created when you define a global variable or function normally.
    • Characteristics:
      • Only one strong symbol with the same name can exist.
      • If multiple strong symbols are defined → linker error.
      • Used for actual, unique implementations.
    • Example: int count = 0; // Strong symbol void myFunction() { } // Strong symbol

    Weak Symbols in C

    • Definition: Symbols explicitly marked as weak using __attribute__((weak)).
    • Characteristics:
      • Can be overridden by a strong symbol with the same name.
      • If no strong symbol exists, the weak symbol is used.
      • Provide default/fallback implementations.
    • Example: __attribute__((weak)) void handler() { printf("Default weak handler\n"); }

    Key Differences Between Weak and Strong Symbols

    AspectStrong SymbolWeak Symbol
    DefinitionDefault function/variable definitionDefined using __attribute__((weak))
    MultiplicityOnly one allowedMultiple allowed (overridden if strong exists)
    PriorityAlways preferred by linkerUsed only if strong symbol not found
    Error HandlingMultiple definitions → Linker errorNo error, just overridden
    Use CaseUnique implementation of codeFallbacks, library defaults, embedded handlers

    Example Demonstrating Weak vs Strong

    // weak.c
    #include <stdio.h>
    __attribute__((weak)) void hello() {
        printf("Hello from weak function\n");
    }
    
    // strong.c
    #include <stdio.h>
    void hello() {
        printf("Hello from strong function\n");
    }
    
    // main.c
    int main() {
        hello();
        return 0;
    }
    

    👉 If all files are compiled → “Hello from strong function”
    👉 If strong.c is not compiled → “Hello from weak function

    Advantages of Weak and Strong Symbols

    Advantages of Strong Symbols

    • Ensure uniqueness of functions and variables across the program.
    • Prevent accidental multiple definitions through linker checks.
    • Provide clear ownership of implementation (no ambiguity).

    Advantages of Weak Symbols

    • Allow default implementations that can be overridden.
    • Useful for library development – provides flexibility to end-users.
    • Helpful in embedded systems for fallback code like interrupt handlers.
    • Enable code modularity and easy customization without changing base code.

    Disadvantages of Weak and Strong Symbols

    Disadvantages of Strong Symbols

    • Multiple definitions lead to linker errors.
    • Not flexible – you cannot provide optional overrides.

    Disadvantages of Weak Symbols

    • Can cause unintended overrides if strong symbols are accidentally defined.
    • Harder to debug, since weak definitions may silently get replaced.
    • May lead to inconsistent behavior if not properly documented.

    Real-Time Applications of Weak and Strong Symbols

    1. Embedded Systems (Microcontrollers & Firmware)
      • Startup code often defines weak interrupt handlers.
      • Developers can override them with strong custom handlers.
    2. Operating Systems and Libraries
      • System libraries provide weak functions as placeholders, allowing developers to replace them with optimized implementations.
    3. Bootloaders
      • Weak symbols act as default functions for initialization routines, overridden later by board-specific implementations.
    4. Driver Development
      • Hardware drivers may use weak functions to allow custom extensions without modifying the core code.
    5. Logging and Debugging Systems
      • A weak logging function may be defined in a library, which can be overridden by a strong symbol to implement device-specific logging.

    Key Takeaways of weak and strong symbols in C

    • Strong symbols are the default; they must be unique across your program.
    • Weak symbols act as placeholders or fallback implementations.
    • If both exist, the strong symbol overrides the weak symbol.
    • Useful in embedded systems, libraries, and modular applications where flexibility and default behavior are important.

    FAQ of Weak and Strong symbols in C

    1.What is a weak symbol in C programming?

    Ans: A weak symbol in C is a function or variable marked with __attribute__((weak)). It provides a default implementation that can be overridden by a strong symbol if available.

    2.What is a strong symbol in C programming?

    Ans: A strong symbol is the default definition of a global function or variable in C. Only one strong symbol with the same name can exist; otherwise, the linker reports a multiple-definition error.

    3.How does the linker resolve weak and strong symbols in C?

    Ans: The linker always gives priority to strong symbols. If both weak and strong symbols of the same name exist, the strong one overrides the weak one. If no strong symbol is found, the weak symbol is used.

    4.Why are weak symbols used in embedded systems?

    Ans: Weak symbols are widely used in embedded systems to provide default implementations of functions, such as interrupt handlers or startup code, which can later be overridden with strong custom implementations.

    5.Weak vs strong symbols in embedded C examples

    Ans: For example, in an embedded project, a library might provide a weak function for logging, while the developer can define a strong logging function to customize it. If the strong symbol is defined, it overrides the weak one.

    6.Advantages and disadvantages of weak symbols in C

    Ans:
    Advantages: Provide fallback functions, flexibility in libraries, easy overrides, useful in firmware and drivers.
    Disadvantages: Can be accidentally overridden, harder to debug, and may cause inconsistent behavior if not documented.

    7.Real-time applications of weak symbols in embedded systems

    Ans:
    a) Interrupt handlers in microcontrollers
    b) Default startup code in bootloaders
    c) System libraries providing optional implementations
    d) Logging/debugging placeholders overridden by user code

    8.How to override weak functions in C

    Ans: To override a weak function, simply define a strong function with the same name in your program. The linker will ignore the weak definition and use the strong one.

    9.Weak attribute in GCC explained

    Ans: In GCC, the __attribute__((weak)) keyword is used to declare a symbol as weak. This tells the linker to treat it as a fallback definition, which can be replaced if a strong definition exists.

    1.What is a weak symbol in C?

    A weak symbol in C is a function or variable marked with __attribute__((weak)). It can be overridden by a strong definition if available.

    2.What is a strong symbol in C?

    A strong symbol is the default type of global function or variable in C. Only one strong symbol of the same name can exist; otherwise, the linker throws a multiple definition error.

    3.What happens if both weak and strong symbols exist in C?

    The strong symbol takes precedence, and the weak symbol is ignored by the linker.

    4.Why are weak symbols used in embedded systems?

    Weak symbols allow developers to provide default implementations (e.g., interrupt handlers) which can later be overridden by user-defined strong implementations.

    5.Can two strong symbols exist with the same name?

    No, multiple strong symbols with the same name cause a linker error.

    Weak Symbols vs Strong Symbols
    Weak Symbols vs Strong Symbols
  • Reverse a Hex Number in C | Master Embedded Coding (2026)

    Reverse a Hex Number in C : If you are learning C programming, working with hexadecimal numbers is an important skill. One common task is to reverse a hex number – that means flipping its digits from last to first.Master the art of reversing a hexadecimal number with this beginner-friendly guide covering C++, Python, and Java. Learn what hex numbers are, how reversal works, and explore simple, step-by-step programs for each language. Perfect for students and programmers looking to practice string manipulation, number system basics, and improve problem-solving skills with real coding examples.
    In this tutorial, we will explain what a hex number is, how reversing works, and how to write C code to reverse a hexadecimal number step-by-step.

    What is a Hex Number?

    A hexadecimal number (or hex number) is a number system with base 16.
    It uses digits 0-9 and letters A-F:

    Hex: 0, 1, 2, ..., 9, A, B, C, D, E, F
    Decimal: 0, 1, 2, ..., 9, 10, 11, 12, 13, 14, 15
    

    Example:

    • Hex 1A3F in decimal is 6719.

    What Does “Reverse a Hex Number” Mean?

    Reversing a hex number means reading its hexadecimal digits from right to left.

    Example:

    Original: 1A3F  
    Reversed: F3A1
    

    Steps to Reverse a Hex Number in C

    We can reverse a hex number by:

    1. Reading the number as a string (so letters and numbers are preserved).
    2. Reversing the string by swapping characters.
    3. Printing the reversed hex number.

    1.C Program to Reverse a Hex Number

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char hex[100]; // To store hex number as string
        int length, i;
        char temp;
    
        // Step 1: Input hex number
        printf("Enter a hexadecimal number: ");
        scanf("%s", hex);
    
        // Step 2: Find length
        length = strlen(hex);
    
        // Step 3: Reverse the string
        for (i = 0; i < length / 2; i++) {
            temp = hex[i];
            hex[i] = hex[length - i - 1];
            hex[length - i - 1] = temp;
        }
    
        // Step 4: Output the reversed hex number
        printf("Reversed hexadecimal number: %s\n", hex);
    
        return 0;
    }
    

    Example Output

    Enter a hexadecimal number: 1A3F
    Reversed hexadecimal number: F3A1
    

    How Reverse a Hex Number Code Works

    • We store the hex number as a string to preserve both numbers and letters.
    • strlen() helps us find how many characters are in the input.
    • A loop swaps characters from start and end until the middle is reached.
    • Finally, we print the reversed result.

    Key Points of Reverse a Hex Number

    • Use char[] to store hex numbers with letters.
    • Always handle input as a string, not as an integer, when reversing.
    • Works for both uppercase (A-F) and lowercase (a-f) hex letters.

    2. Reverse a Hex Number in C++

    #include <iostream>
    #include <string>
    #include <algorithm>
    using namespace std;
    
    int main() {
        string hexNum;
    
        // Step 1: Input hex number
        cout << "Enter a hexadecimal number: ";
        cin >> hexNum;
    
        // Step 2: Reverse the string
        reverse(hexNum.begin(), hexNum.end());
    
        // Step 3: Output reversed hex
        cout << "Reversed hexadecimal number: " << hexNum << endl;
    
        return 0;
    }
    

    Explanation:

    • We use string to store the hex value.
    • The reverse() function from <algorithm> quickly reverses the characters.
    • Works for both uppercase and lowercase letters.

    3. Reverse a Hex Number in Python

    # Step 1: Input hex number
    hex_num = input("Enter a hexadecimal number: ")
    
    # Step 2: Reverse the string using slicing
    reversed_hex = hex_num[::-1]
    
    # Step 3: Output result
    print("Reversed hexadecimal number:", reversed_hex)
    

    Explanation:

    • Python treats strings as sequences, so slicing [::-1] reverses the string easily.
    • This method is the shortest and beginner-friendly.

    4. Reverse a Hex Number in Java

    import java.util.Scanner;
    
    public class ReverseHex {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
    
            // Step 1: Input hex number
            System.out.print("Enter a hexadecimal number: ");
            String hexNum = sc.next();
    
            // Step 2: Reverse using StringBuilder
            String reversedHex = new StringBuilder(hexNum).reverse().toString();
    
            // Step 3: Output result
            System.out.println("Reversed hexadecimal number: " + reversedHex);
    
            sc.close();
        }
    }
    

    Explanation:

    • Java’s StringBuilder class has a built-in reverse() method.
    • Using toString() converts the reversed object back into a string.

    Example Run for Reverse a Hex Number in all Languages

    Enter a hexadecimal number: 1A3F
    Reversed hexadecimal number: F3A1
    

    Key Takeaways

    • Always store hex numbers as strings when reversing.
    • C++ uses reverse() from <algorithm>.
    • Python uses slicing [::-1].
    • Java uses StringBuilder.reverse().

    FAQ : Reverse a Hex Number in C

    Q1: What does it mean to reverse a hex number in C?

    Ans: Reversing a hex number means rearranging its hexadecimal digits in reverse order. For example, reversing 0x1A3F gives 0xF3A1. This is often used in embedded systems and low-level programming.

    Q2: How can I reverse a hex number in C?

    Ans: You can reverse a hex number by extracting each digit using bitwise operations or string manipulation, and then reconstructing the number in reverse order.

    Q3: Can I reverse a hex number using arrays or strings?

    Ans: Yes! You can convert the hex number to a string, reverse the string, and convert it back to a number. This method is simple and beginner-friendly.

    Q4: Is reversing a hex number different in C++ or Python?

    Ans: The concept is the same across languages, but the implementation differs. In C++, you can use std::string for easy manipulation. In Python, you can use slicing to reverse the string representation of a hex number.

    Q5: Why is reversing a hex number important in embedded systems?

    Ans: Reversing hex numbers is useful in memory management, communication protocols, endianness handling, and debugging, where the byte order matters.

    Q6: Are there any common mistakes to avoid when reversing hex numbers in C?

    Ans: Common mistakes include ignoring leading zeros, not handling negative numbers correctly, and mixing up bitwise operations with string manipulation methods.