One further note: The UUID is a feature of certain filesystems
(and also Linux swap space). A partition without a filesystem will have no UUID, and some filesystems (such as FAT and NTFS) lack UUID values, although utilities like blkid return these filesystems' non-UUID serial numbers and label them as "UUIDs." The code that RupertPupkin presented does the same. Depending on your needs, that might be fine, but if your code assumes a full UUID, it could run into bugs if you use it on a filesystem that has a non-UUID serial number. IIRC, partitions that use ReiserFS prepared from Fedora lack UUIDs entirely.
If your disk uses the GUID Partition Table (GPT), every partition has associated with it a GUID, which is basically a UUID with some minor representational differences. This is true no matter what filesystem the partition holds, or even if it's entirely empty. Thus, if you know you'll be using GPT disks and if you need a unique identifier for every partition even if it's empty or if its filesystem might lack a UUID, using the partition's GUID might be a better option than using the filesystem's UUID. The drawback to this approach is that it works only on GPT disks; if the disks might be or definitely are Master Boot Record (MBR) disks, it won't work at all. Likewise if you're interested in filesystems held within an LVM configuration. I don't have any C code offhand that demonstrates how to get a GPT partition's GUID, but it's conceivable that something in libparted will help with the task, so you could look into that. GPT fdisk (gdisk) also contains code to do the job, but you'd need to use an awful lot of gdisk's code to get at the GUID values. Be aware that every partition on a GPT disk has two
GUIDs associated with it. One is unique to the partition in question, and the other is a partition type code. You presumably want the unique GUID, not the type code GUID, so be sure to use the correct one.