Hello, and welcome back.
I want to continue our discussion on some of the functions
in the standard c library.
These are a lot of the functions we went through already in class,
but I wanted to remind you of.
So there are a lot of important various functions from c standard
library that we have already addressed throughout the course
with all of our examples and all the stuff that we've learned.
We talked a lot about string functions.
And some of them are very easy to use and some we're going to use a lot,
so you should be familiar with these.
To use any of these functions, you need to include the header
file string.h, everything is defined in string.h.
We talked about str cat.
Str cat concatenates two strings, two parameters it
takes, two character pointers.
And it will return a character pointer concatenated to the other one.
So placing a null character the end of the final string,
the function returns s1.
Str char this searches for a string that contains a character.
This will return a character star as well.
It's a good way to search.
We talked about this quite a bit with string cat.
Str compare is going to compare two strings.
This is the standard way you do it.
It takes two pointers and it returns an integer.
We did have some examples of this where basically if you return
zero the two strings are equal.
If we return greater than or less than, if you turn greater than
zero, that means s1 is usually greater in alphabetical size.
So if you pass in a string that contained a and then the second string
is b, it would return greater than zero because or it would turn less
than zero because a is less than b.
If you passed in b as the first parameter and c is the second
parameter some kind of string starting with b or c, it will
return greater than zero.
So you've got to understand those return values.
That's what's important str compare.
Str copy.
This is how you can assign a string to another string.
Remember, strings, pointers and character arrays, you cannot
assign the string directly using the assignment operator.
For integers and floats, you can just use the equals operators.
Can't do that with strings.
So you have to use str copy when you want to assign a string to a pointer,
character pointer or character array.
You can initialize a character array literal string constant
inside double quotes.
But after that, after the initialization if you want to change
it, you'll have to use str copy.
Str length.
Another important function to get the length of a string.
This returns a size t which is the integer representing the length.
All these string functions take in pointers or character
arrays as parameters.
So we need to understand how that works as well.
Other string function strncat.
Remember all the functions that instead add the end are
a little more safe because you don't have buffer overflows.
So in strncat the third parameter is added and this will say copy
or concatenate so many parameters.
Usually, the third parameter is going to be the size of the first
parameter because you don't want to copy anything larger than that.
These functions should be utilized over the other string
functions because they are safe for buffer overflow.
Strn compare.
Again, you're going to compare certain number of characters for s1 and s2.
Strn copy, here the third parameter end would probably be the str
length of or the size of s1.
Because you're going to copy s2 and s1, but you don't want to copy more
bytes than s1 can hold or store.
Str char.
This is where you can search the string for the last
occurrence of a character.
So you notice this looks very familiar or similar to the str
char, there's an extra r here.
So instead of returning the first occurrence, this will return the
last occurrence, but you used the same way as the other one.
Str str.
This is going to search for a string.
So this is a search function where you're trying to find essentially
a substring inside of a string.
Again, taking two character pointers.
Strtok.
This was the important function where you can it helps with parsing data.
Strtok takes the delimiter as the second parameter and it will
look for that delimiter character anywhere inside the string.
So often you use something like a space or a line feed for s2.
It will break the string into a number of different tokens.
Again, use when you want to parse data usually.
Every time you have to call strtok multiple number of times until
you with null if you're still looking for the next tok that you
want to find in a given string.
We also had some important character functions.
All the character functions are defined in ctype.h.
The character functions that we used were the is functions checking
whether or not a character is an alphanumeric or whether it's
blank or whether it's a control character a digit, there were a
number of different functions there.
So remember that ctype.h allows us to check for these different
characters what they actually are.
We also had some conversion functions in ctype.h, mainly our two upper.
Two lower takes a character and it will it will give the
lowercase equivalent of that c.
Remember we had a challenge where we converted lowercase to uppercase.
In that challenge, we actually added to our ASCII table, but we could have
called two upper on the character.
We could have set two up around the character as long as we included
ctype.h and that would converted a lowercase to an uppercase.
We had a lot of IO functions, input and output.
We concentrated on reading and writing files, but we also read input
from the keyboard, and we outputted data to the console or the screen.
So to use the most common IO functions, that's all in the
standardio.h header file.
This file has declarations for the different IO functions as well as
definitions for constants, like end of file, null standard in
standard output and standard error.
Those are all constant values.
There's also a constant for the file structure file.
Fclose is a function that we used to close our streams.
It closes the file identified by file pointer.
And it returns zero if it to close a successful, otherwise eof.
Feof, that's a function we didn't use explicitly but that will check
whether or not you're at the end of the file as opposed to comparing
to a constant you can do an feof.
Fflush.
This is a function that will flush all the data of the internal buffer.
Sometimes you need to do this when you want to start over, but
you're basically just getting rid of anything inside of a buffer.
We also have fgetc.
This is reading in individual characters, takes a file pointer.
We have fget position.
This is some of the position functions.
It gets the current file position for the file associated with
the file pointer and stores it in a f position type, which is
defined in standard io.h as well.
We had our fgets.
This was reading in strings.
Very nice, it reads characters from the indicated file,
but number of characters.
Fopen will open up a file in a specific mode: read, write or
append using a file pointer.
Fprintf will write data and formatted output, and it's similar to the
printf function it will take the arguments specified by the pointer
and then use formatted data to write.
So you can write integers, doubles using format specifiers and then
variables that you want to write.
We also had fputc, which will write characters to a file.
Fputs will write a string to a file.
It takes a buffer and a file pointer what you want to write
and the file you want to write to.
Fscanf will read data from a file.
It will grab the input, and it will use the same kind
of format as scanf will take format specifiers and variables.
But this is nice you want to read in data separated by space.
You can use fscanf and you can specify the type so read it directly into
ints, doubles, specific data types.
Fseek is going to move the position of where you're pointing to in a file.
So you don't have to start at the beginning or the end of a file.
You can go directly to the middle of the file.
Once you fseek that position, your next read or write will use that
position to write to or read to.
Fset position is similar to fseek.
It sets the current file position in the file associated with
the file pointer to the value pointed to by f position, fpos.
We also had ftell.
Ftell is going to return how many bytes from the beginning
you are based on the file pointer.
So it's the relative offsite in bytes from the current position,
not always the beginning, but the current position.
We use the ftell in one of our challenges to read a file in reverse.
Printf.
We use this probably more than any other function.
This was displaying output to the screen.
It uses format specifiers and a variable number of
arguments separated by commas.
Printf is very useful for debugging and displaying output, which you do
quite a bit inside of a c program.
Remove.
Remove is a function where you can delete a file.
It takes a file name.
The file pointer has to be closed.
You can't have it open.
Rename will rename one file to the other file, also has to be
closed the file cannot be opened.
Scanf will read input from usually default from the keyboard.
You can change it to read input from other streams if you want to.
We also had conversion functions that we discussed throughout the course.
Most of these functions are in the standard lib.h file, header file.
We had the atof function.
This is converting a string to a float, and you pass it in a string.
We have functions atoi.
This converting a string to an integer.
Atol is converting a string to a long, and you pass in the string
and it returns either the return method that you're converting to.
These are very useful.
You often use these to convert strings to some kind of number type.
Atoll converts it to a long long int.
But make sure you look at the standard lib header file for
more conversion functions.
We had our memory functions, our dynamic memory allocation functions.
These are really important.
Dynamic memory allocation is important because it's much more efficient.
You only utilize the memory that you need to.
You don't create static data.
It allows your program to be much more flexible.
All these functions are included inside standard lib.h.
We talked about calloc.
Calloc is a great function where you can allocate a certain size
and that will be multiplied by n.
So you can specify the data type as well and it will multiply it.
The nice thing about calloc, it initializes your data.
So it's really preferred over malloc.
We have our free function which will delete the memory.
All of this memory is going to be created on the heap.
So we'll be around a lot longer, but it's up to you as a
programmer to manage this memory.
You always want to free your memory after you're done using
it because memory is a scarce resource in other programs and
we want to use that memory.
So always call free after you do a calloc or malloc.
Malloc is a simpler version of creating memory.
It doesn't actually initialize any data.
It just takes the size as a parameter.
It returns a void star.
So you always need to cast it just like with calloc to whatever
type of memory you're pointing to.
If you're pointing to int or float, you have to cast that.
We also have realloc.
Realloc is awesome because you can actually add to existing memory.
And so this way your memory can be dynamic.
You can make it larger if you want to store more data.
Realloc takes a pointer in the size.
It's going to keep all the data that the previous memory was pointing to.
It will allocate new memory but it will keep it, so you can
add on to some existing memory.
Very, very useful.
In creating memory dynamically is is really going to be useful in all
your programs especially because pointers are very, very important
they're much more efficient.
But make sure you understand all these header files that we kind
of use throughout the course.
These are the main ones that are very, very important.
We'll talk about some new ones in the next couple lectures that we didn't
get to discuss as much as I liked.
But these are some important ones as well.
Thank you.