Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5846105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:31:47+00:00 2026-05-22T12:31:47+00:00

This question is exactly like this question: How to get MAC address of your

  • 0

This question is exactly like this question: How to get MAC address of your machine using a C program?

“I am working on Ubuntu. How can I get MAC address of my machine or an interface say eth0 using C program.”


Now, I don’t usually touch C…but in this case I have to. Since I don’t really know what is going on in the following code, which was taken from the answer linked to above, I need some help.

#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <netdb.h>
#include <stdio.h>

int main()
{
  struct ifreq s;
  int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);

  strcpy(s.ifr_name, "eth0");
  if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
    int i;
    for (i = 0; i < 6; ++i)
      printf(" %02x", (unsigned char) s.ifr_addr.sa_data[i]);
    puts("\n");
    return 0;
  }
  return 1;
}

Instead of a function that prints the MAC Address I need a function that returns it as a string. You know, like this:

const char * gettaStringFromNativeCode(void) 
{
    return "This is a string";
}

This is to be used with Mozilla Chromeless, which uses Firefox’s new JCTYPES like this.

Basically, I’m looking to do something like this (borrowing from C#):

// Using "string" here because its pseudo-code and I don't know what i'm doing. :-)
string getMAC()
{
  struct ifreq s;
  int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);

  strcpy(s.ifr_name, "eth0");
  var macAddress = string.Empty; // yah, this is actually C#
  if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
    int i;
    for (i = 0; i < 6; ++i)
       // yah, this is a bit of C# too.
      macAddress += string.Format(" %02x", (unsigned char) s.ifr_addr.sa_data[i]) );
  }
  return macAddress;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:31:48+00:00Added an answer on May 22, 2026 at 12:31 pm
    #include <sys/socket.h>
    #include <sys/ioctl.h>
    #include <linux/if.h>
    #include <netdb.h>
    #include <stdio.h>
    
    char *getmac(char *iface)
    {
    #define MAC_STRING_LENGTH 13
      char *ret = malloc(MAC_STRING_LENGTH);
      struct ifreq s;
      int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
    
      strcpy(s.ifr_name, iface);
      if (fd >= 0 && ret && 0 == ioctl(fd, SIOCGIFHWADDR, &s))
      {
        int i;
        for (i = 0; i < 6; ++i)
          snprintf(ret+i*2,MAC_STRING_LENGTH-i*2,"%02x",(unsigned char) s.ifr_addr.sa_data[i]);
      }
      else
      {
        perror("malloc/socket/ioctl failed");
        exit(1);
      }
      return(ret);
    }
    
    int main()
    {
      char *mac = getmac("eth0");
      printf("%s\n",mac);
      free(mac);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, This question is not exactly a programming question but this is what can
Computers recognize different file systems. My question is, where exactly does this information get
I postet this question a few days ago but I didn't explain exactly what
This is probably a stupid question, but what exactly is a non-blocking web server?
I'm trying to understand what exactly the above (in my question's Title) means? This
I would like to apologize if the duplicate of this question exist. i tried
I'm not exactly sure how to word this question so I'll just use an
So, if this question has been asked before, I'm sorry. I'm not exactly sure
This question is kind of the next level of F# Set using custom class
I had previously asked this question, trying to get started with this code: The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.