linux execute as user

C++
sudo -H -u otheruser bash -c 'echo "I am $USER, with uid $UID"' 

Source

Also in C++: