Hello, and welcome back.
In this lecture, I want to talk about some of the utility functions that are
provided by the c standard library.
And these do exactly what you would think just some utilities,
some generic general functions to do various different things.
To use the utility functions, they're usually in standard lib.h.
So you include that header file.
Some of the functions I'm going to describe just useful functions.
One is the absolute value abs.
Returns the absolute value of an argument.
Absolute value is converting it to a positive.
So if you pass in a negative or so forth, it will return
you the positive value.
There's function called exit.
I've used this a bit in our main function.
It will take an integer, usually a return value and usually a negative
number means there's an error a positive number or zero means
no error or one means no error.
This will terminate the program though.
It will just jump right out.
If you're in a main function, you do a return, you can
return from the main function.
But exit is usually to close the program.
There are some constants as well defined such as exit success and
exit failure that you can use if you don't want to use numbers.
Other related routines in the library that you might want to
refer to are important at exit.
And you will see this in quite a bit c program, especially abort.
Abort is when there's some kind of error like if you can't
malloc, you may call abort, and you can set it to signal.
The good thing with abort is it will generate a core
file depending on the signal.
Core file can be helped to debug for further information.
But exit abort and atexit very useful functions, usually invoked
when you have a fatal error.
There's a get environment function which will allow you
to read environment variables.
Environment variables are usually associated with your environment.
They're a variable that you can use it's kind of like a
global variable that you can use anywhere on the operating system.
So if it's Linux, you have an environment variable for your path.
There's environment variables in windows to specify paths or where
different programs are located or just generic global variables.
You can set an environment variable and then you can read it using
this function get environment.
And it will read in that environment variable and you
can use it in your program.
So again, things like path anythings that are related with your shell
or related with your environment.
Get environment is very useful.
There's a qsort function.
This will sort the data array pointed to by the void pointer array.
So there's an array that's taken as a first parameter
and it's going to sort it.
There's going to be n elements in the array, each size bites in the length.
And the n and the size are of type size t.
So there's four variables that go to this function.
But the qsort's nice because you can sort an array.
The fourth argument is a pointer to the function that
you want to use for sorting.
So it can become more complicated.
But this function is very useful when you need to compare
two elements in an array.
You can pass in the pointers to the elements that you
want to compare against.
We have a random function.
We use this in one of our challenges rand.
This will generate a random number.
You can specify a seed if you want in another function or it will just
generate a random number between a minimum value of 3276, and minimum
value zero random, maximum value 3276.
So, rand max is defined in standard lib.h.
You'll often use this with the sran function, which provides a seed.
A seed can provide a certain range.
It will make the numbers maybe more random too.
So it seeds the random number generator to an unsigned int.
System is a function that you can invoke to execute system calls.
I used it in the class to do a pause.
But anything you can run from the console, like changing directories,
changing permissions if you're on Linux, executing other programs,
you can do all this through the system.
And so this will actually run a command in your shell,
whether it's the dos prompt or you're using a shell on Linux.
It will run that command.
So for example you could, say system make dir and it will create a
directory of whatever you specified in the argument to make dir.
It's all inside double quotes, so it takes a string.
System is very, very useful as well.
The assert library is useful for debugging.
We didn't get a chance to talk a lot about assert, but I did
briefly want to mention it.
It's a library that's in assert.h and it's one to help with debugging.
So assert is saying something is true.
So you can put assert inside your code to make sure that
something is true before you move on to the next line of code.
There's an assert macro that you're going to use and it takes an
argument such as an inner expression.
If the expression evaluates to false, the assert macro writes an
error message to standard error stream and calls the abort function.
So your programmer terminate.
So you can put something in your code that basically
says something like this.
You can say z is equal to x times x minus x times y.
And then you can assert that z is greater than or equal to zero.
If that returns false, your program is going to exit.
Otherwise, it will execute the next line of code.
So it's like making true assumptions throughout your code.
You want to make sure that certain values are what they are
before you move on to the next.
This is essentially making sure that you're not like dividing by zero
there you didn't have a zero value when you did this multiplication.
You'll often see assert inside of c programs for debugging.
Other useful header files that you may want to look into: time.h.
Time.h has all sorts of different functions and macros for supporting
different dates and times.
Usually, returning a time t struct.
But you can get the date.
You can get the time.
Errno.h or errornumber.h.
This defines macros for reporting of errors.
Often used, again, for better debugging and
reporting specific errors.
So a lot of functions you use in standard libraries.
If they fail, they may return a number for a specific error, and it's more
important when you use some socket functions other things because there
could be many different errors.
Sometimes it's useful to know what's going on as opposed to
just say there's an error is not very helpful to say a specific
type of error is much useful.
So you can get error numbers using this with to help
with further debugging.
Locale.h.
There's a bunch of functions and macros in this header
for formatting different currencies and monetary units.
Signal.h.
This is useful for usually for program termination
generating different signals.
Signals isn't a topic that we cover in a lot of detail but this could be
useful when you call abort specifying specific signals while the program
crashed or program terminated.
And you can use these if you understand signals you can use
them in your core file as well.
Stdarg.h.
This defines different facilities that enable a variable number of arguments
to be passed to a function, varargs.
We use vargs and printf, but we didn't create our own functions of arg.
It's a more advanced topic.
But these are basic header files that you may find useful for getting
times, error numbers, and so forth.
These are good utility functions.
So I hope this was helpful.
Thank you.