"[computername] says, 'Hello [username].'" with getenv()

MosaicFuneral 0 Tallied Votes 197 Views Share

Cheap, dirty code for Windows and *nix.

#include <stdio.h>
#include <stdlib.h>

#ifdef WIN32
  #define USER "USERNAME"
  #define HOST "COMPUTERNAME"
#elif defined __unix__
  #define USER "USER"
  #define HOST "HOST"
#else
  #error Sorry don't know the environment variable(of the target platform).
#endif

int main()
{
    printf("%s says, \"Hello %s.\"\n", getenv(HOST), getenv(USER));

    getchar();
    return(0);
}
athlon32 56 Junior Poster in Training

This is a good example :) 5 stars!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.