Author : James Rogers
Page : << Previous 8 Next >>
that I am qualified to write about it yet. I will revisit it once I have covered everything else. Or maybe someone else could tell us how to use these in everyday programming.
The examples from this tutorial:
Example 1:
#include <stdio.h>
#include <stdlib.h>
/*
This program is written to demonstrate the <stdlib.h> library.
This program will demonstrate integer math
Written by James M. Rogers
17 March 1999
Released to the Public Domain on this date.
*/
main(){
int a, b;
long int c, d;
div_t x;
ldiv_t y;
a = 10;
b = 3;
x = div(a, b);
printf(" div (%d, %d) returns a quotient of %d and a remainder of %d\n", a, b, x.quot, x.rem);
c = 1000000;
d = 337;
y = ldiv(c, d);
printf(" div (%d, %d) returns a quotient of %d and a remainder of %d\n", c, d, y.quot, y.rem);
a = -100;
b = abs(a);
printf("The absolute value of %d is %d\n", a, b);
}
Example 2:
#include <stdio.h>
#include <stdlib.h>
/*
This program is written to demonstrate the <stdlib.h> library.
This program will demonstrate string to number conversions
Written by James M. Rogers
17 March 1999
Released to the Public Domain on this date.
*/
main(){
double a;
int b;
long int c;
unsigned long int d;
a = atof("1345");
printf("%f\n", a);
b = atoi("-345");
printf("%d\n", b);
c = atol("1234567890");
printf("%d\n", c);
a = strtod("9876543210.0123456789", (char **)NULL);
printf("%f\n", a);
c = strtol("157", (char **)NULL, 10);
printf("%d\n", c);
d = strtoul("47586", (char **)NULL, 10);
printf("%d\n", d);
}
Example 3:
#include <stdio.h>
#include <stdlib.h>
/*
This program is written to demonstrate the <stdlib.h> library.
This program will demonstrate seaching and sorting and random numbers
Written by James M. Rogers
17 March 1999
Released to the Public Domain on this date.
*/
#define ELEMENTS 50000
/* this is the compare program that we call with qsort to sort the array */
static int compar(const void *a, const void *b){
return (strcmp( (char *)a, (char *)b));
}
main(){
int x, i;
char *string;
struct sort_test_t {
char s[16];
} ;
struct sort_test_t sort_test[ELEMENTS];
/* initalize the pseudo-random sequence with the time */
/* if you remove the following command then the program */
/* will generate the same series of "random" numbers each */
/* time the program is ran */
srand(time());
/* inititalize the array */
for (i=0;i<ELEMENTS;i++) {
/* produce a random variable */
x=1+(100000.0*rand()/(RAND_MAX+1.0));
/* load the variable into the string as an array */
sprintf(sort_test[i].s,"%d\000", x);
}
/* sort the array */
qsort(sort_test, ELEMENTS, sizeof(sort_test[0]), &compar);
/* seach the array */
if (string = bsearch("1000", sort_test, ELEMENTS, sizeof(sort_test[0]), &compar)) {
printf("The string 1000 is present.\n");
} else {
printf("The string 1000 is not present!\n");
}
/* output the sorted array */
for (i=0;i<ELEMENTS;i++) {
printf("%d %s\n", i, sort_test[i].s);
}
}
Example 4:
#include <stdio.h>
#include <stdlib.h>
/*
This program is written to demonstrate the <stdlib.h> library.
This program will demonstrate memory allocation using malloc.
Written by James M. Rogers
21 March 1999
Released to the Public Domain on this date.
*/
struct element{
struct element *next;
int value;
}element_size;
struct element *initialize () {
struct element *add;
if(add=(struct element *)malloc(sizeof(element_size))){
add->next = (struct element *)NULL;
return add;
} else {
return (struct element *)NULL;
}
}
struct element *push (struct element *top, int value) {
struct element *add;
if (add=(struct element *)malloc(sizeof(element_size))){
add->next=top;
add->value=value;
top=add;
return top;
} else {
printf("Failed to push a value onto the stack, I am quiting.\n");
exit (1);
}
}
struct element *pop (struct element *top, int *value) {
struct element *remove;
fflush(stdout);
*value=top->value;
remove=top;
top=top->next;
free(remove);
return top;
}
main(){
struct element *stack;
int x;
if ( !(stack = initialize())){
printf("Failed to initialize the stack, I am quiting.\n");
exit (1);
}
stack = push(stack, 1);
stack = push(stack, 2);
stack = push(stack, 4);
stack = push(stack, 8);
stack = push(stack, 16);
stack = push(stack, 32);
stack = push(stack, 64);
stack = push(stack, 128);
stack = push(stack, 256);
while (stack = pop(stack, &x)){
printf("Return value is \t%d\n", x);
}
}
Example 5:
#include <stdio.h>
#include <stdlib.h>
/*
This program is written to demonstrate the <stdlib.h> library.
This program will demonstrate memory allocation using calloc and realloc.
Written by James M. Rogers
21 March 1999
Released to the Public Domain on this date.
*/
/*
Read in lines from a file, reallocating memory as needed, then freeing when done.
*/
#define SIZE 1024
main(int argv, char *argc[]){
char *file;
char line[SIZE];
FILE *stream;
unsigned int mem_size, file_size, line_size;
mem_size=SIZE;
file_size=0;
if((file=calloc(1,SIZE))==(char *)NULL){
printf("Cannot allocate memory.\n");
exit (1);
}
if((stream=fopen(argc[1], "r")) == (FILE *)NULL){
printf("Cannot open file.");
exit (1);
}
while(fgets(line, SIZE+1, stream) != (char *)NULL) {
printf("%s",line);
line_size=strlen(line);
file_size += line_size;
while(file_size>mem_size) {
mem_size += SIZE;
if ((file=realloc(file, mem_size)) == (char *)NULL){
printf("Cannot allocate memory.\n");
free(file);
fclose(stream);
exit (1);
}
}
strcat(file,line);
printf("allocated memory:%d \t file size:%d \t size of line:%d\n", mem_size, file_size, line_size);
}
printf("%s",file);
fclose(stream);
free(file);
exit (0);
}
Example 6:
#include <stdio.h>
#include <stdlib.h>
/*
This program is written to demonstrate the <stdlib.h> library.
This program will demonstrate memory allocation using calloc and realloc.
Written by James M. Rogers
21 March 1999
Released to the Public Domain on this date.
*/
/*
Read in lines from a file, reallocating memory as needed, then freeing when done.
*/
#define SIZE 1024
main(int argv, char *argc[]){
char *file;
char line[SIZE];
FILE *stream;
unsigned int mem_size, file_size, line_size;
mem_size=SIZE;
file_size=0;
if((file=calloc(1,SIZE))==(char *)NULL){
printf("Cannot allocate memory.\n");
exit (1);
}
if((stream=fopen(argc[1], "r")) == (FILE *)NULL){
printf("Cannot open file.");
exit (1);
}
while(fgets(line, SIZE+1, stream) != (char *)NULL) {
printf("%s",line);
line_size=strlen(line);
file_size += line_size;
while(file_size>mem_size) {
mem_size += SIZE;
if ((file=realloc(file, mem_size)) == (char *)NULL){
printf("Cannot allocate memory.\n");
free(file);
fclose(stream);
exit (1);
}
}
strcat(file,line);
printf("allocated memory:%d \t file size:%d \t size of line:%d\n", mem_size, file_size, line_size);
}
printf("%s",file);
fclose(stream);
free(file);
exit (0);
}
Example 7:
#include <stdio.h>
#include <stdlib.h>
/*
This program is written to demonstrate the <stdlib.h> library.
This program will demonstrate environmental functions of the stdlib library.
Written by James M. Rogers
22 March 1999
Released to the Public Domain on this date.
*/
#define OVERWRITE 1
#define NO_OVERWRITE 0
void shutdown_1 () {
printf("\nShutting down!\n\n");
}
void shutdown_2 () {
printf("\nReally Shutting down!\n\n");
}
void shutdown_3 () {
printf("\nReally Really Shutting down!\n\n");
}
void shutdown_4 () {
printf("\nReally Really Really Shutting down!\n\n");
}
main(){
atexit(shutdown_4);
printf("I am displaying the environmental variable TESTING : ");
printf("%s\n", getenv("TESTING"));
printf("The following setenv will only set TESTING if TESTING is unset : ");
if ( setenv("TESTING", "no_overwite", NO_OVERWRITE) ) {
abort();
} else {
printf("%s\n", getenv("TESTING"));
}
atexit(shutdown_3);
printf("I am unsetting TESTING.\n");
unsetenv("TESTING");
printf("The following setenv will only set TESTING if TESTING is unset : ");
if ( setenv("TESTING", "no_overwite", NO_OVERWRITE) ) {
abort();
} else {
printf("%s\n", getenv("TESTING"));
}
atexit(shutdown_2);
printf("The following setenv will always set TESTING : ");
if ( setenv("TESTING", "overwrite", OVERWRITE) ) {
abort();
} else {
printf("%s\n", getenv("TESTING"));
}
atexit(shutdown_1);
exit (0);
}
Example 8:
#include <stdio.h>
#include <stdlib.h>
/*
This program is written to demonstrate the <stdlib.h> library.
This program will demonstrate seaching and sorting and random numbers
Written by James M. Rogers
17 March 1999
Released to the Public Domain on this date.
*/
#define ELEMENTS 50000
/* this is the compare program that we call with qsort to sort the array */
static int compar(const void *a, const void *b){
return (strcmp( (char *)a, (char *)b));
}
main(){
int x, i;
char
Page : << Previous 8 Next >>