C - Converting from char / string to int using atoi

13. March 2012 08:00

 

This function should probably be avoid since it does not have any error handling and the strtol should be used instead. However it is a quick and dirrty way of converting strings to int's

 

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


int main(int argc, char **argv) {
        char *str = "12345";
        int val = atoi(str);

        printf("%s -> %d\n", str, val);

        return 0;
}

E-mail Kick it! DZone it! del.icio.us Permalink


C - Unsetting an environment variable

12. March 2012 08:00

 

A short example to in C to clear an enviroment variable using the libc function unsetenv

 

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

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

	if (unsetenv("HOME") < 0) {
		perror("unsetenv");
		exit(EXIT_FAILURE);
	}

	system("env | grep HOME");
	return 0;
}

E-mail Kick it! DZone it! del.icio.us Permalink


C - Using setenv

11. March 2012 21:39

 

A short example of how to us setenv in a c program to set an enviroment variable which is then made avilable for any child processes to use.

 

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

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

	if (setenv("VARNAME", "NEWVALUE", 1) < 0) {
		perror("setenv");
		exit(EXIT_FAILURE);
	}

	system("env | grep VARNAME");
	return 0;
}

E-mail Kick it! DZone it! del.icio.us Permalink


C - getenv

10. March 2012 08:00

 

A C example to use getenv to get and print an enviroment variable.

 

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

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

	char *var = getenv("HOME");
	
	if (var == NULL)
		printf("Not Set");
	else
		printf("var = %s\n", var);

	return 0;
}

E-mail Kick it! DZone it! del.icio.us Permalink


C - Clear the enviroment

9. March 2012 21:30

 

A short example to show how to clear all enviroment variables from a C program using the libc function clear env.

 

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

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

	clearenv();

	system("env");
	return 0;
}

E-mail Kick it! DZone it! del.icio.us Permalink