'proper way of writing to /sys or /proc filesystem in c

what is a proper way of writing to /proc or /sys filesystem in linux in c ?

Can I write as I would in any other file, or are there special considerations I have to be aware of?

For example, I want to emulate echo -n mem > /sys/power/state. Would the following code be the right way of doing it?

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

int main(int argc, char **argv) {

    FILE *f;

    f = fopen("/sys/power/state", "w");

    if(f == NULL) {
        printf("Error opening file: /sys/power/state\n");   
        exit(1);             
    }

    fprintf(f,"%s","mem");

    fclose(f);
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source