C definition and keywords
The C specification has become quite large. You can read the full specification for each version at http://www.iso-9899.info/wiki/The_Standard.
The C specification has become quite large. You can read the full specification for each version at http://www.iso-9899.info/wiki/The_Standard.
The following table provides a list of reserved keywords in C by category. These keywords cannot be redefined in your programs. Some of these have not been explained in this book:
Keys
1: Added to the C99 standard.
2: Added to the C11 standard. Many of these keywords facilitate quite advanced functions in computer programming.
The following table lists the precedence and associativity of C operators. Operators are listed from top to bottom, in descending precedence. The grouping operator, ()
, has the highest precedence. The sequence operator, (,)
, has the lowest precedence. There are four classes of operators: postfix
, prefix
, unary
, and binary
:
The following is a list of the compiler switches already encountered, with the addition of other useful switches and why you might want to use them:
There is a dizzying array of options switches for the GCC compiler. These can be found on the GNU website at https://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html.
We have a table of 256 ASCII characters. The table is reproduced here for convenience; it was generated from the program we created in Chapter 15, Working with Strings:
Here is the introduction to Bstrlib
taken from its document file:
The full documentation can be found at https://raw.githubusercontent.com/websnarf/bstrlib/master/bstrlib.txt. The documentation is thorough in providing motivation and seems to be complete in that it describes every function and its possible side effects, if any. If you decide to incorporate this library into your programs, I strongly suggest you read and study this document. In this brief introduction to Bstrlib, we will focus entirely on the C functions of the library, not the C++ functions.
The Bstrlib home page can be found at http://bstring.sourceforge.net...
Bstrlib is a set of programs that is meant to completely replace the C standard library string handling functions. It provides the following groups of functions:
To get the core functionality of Bstrlib, a program only needs to include one header file, bstrlib.h
, and one source file, bstrlib.c
, for compilation, along with the other program source files.
Unlike C strings, which are arrays of '\0'
-terminated characters, bstring
is a structure, defined as follows:
struct tagbstring {
int mlen; // lower bound of memory allocated for data.
int slen; // actual length of string
unsigned char* data...
These examples are very, very simple and are meant to give you a feel for using Bstrlib. The examples provided on the SourceForge
website are quite advanced string handling examples. They are extremely useful and well worth studying.
Our first bstrlib
example will be the Hello, world!
program, as follows:
#include <stdio.h>
#include "bstrlib.h"
int main( void ) {
bstring b = bfromcstr ("Hello, World!");
puts( (char*)b->data );
}
This program, bstr_hello.c
, creates bstring
from a C string and then prints it using puts()
. To compile this program, be sure that the bstrlib.h
and bstrlib.c
files are in the same directory as this program. Then, enter the following command:
cc bstrlib.c bstr_hello.c -o bstr_hello -Wall -Werror -std=c18.
In our next example, we will split a string into multiple strings based on a delimiter and then print them. We can do this with the C standard library, but it is rather complicated...
This is a very deep and broad topic. The purpose of this section is to provide a cursory introduction to the topic, as well as to provide some resources to learn much more about this topic.
In the early days of computers, there was 7-bit ASCII, but that wasn’t good enough for everyone, so someone came up with 16-bit Unicode. This was a good start, but it has its own problems. Finally, the guys who invented C got around to inventing UTF-8, which is backward-compatible with ASCII and dovetails into UTF-16 and UTF-32, so anyone around the world can write Hello, World!
in their own language using their own characters on just about any computer. An added benefit of UTF-8 is that it is easily converted into/from Unicode when needed. Unicode didn’t stop there; it evolved as well. Unicode and UTF-8 are different encodings, but they are still somewhat interrelated.
To give you an idea of what it is like to convert between Unicode and UTF-8, consider the following program:
#include <stdio.h>
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
int main(void) {
wchar_t ucs2[5] = {0};
if( !setlocale( LC_ALL , "en_AU.UTF-8" ) ) {
printf( "Unable to set locale to Australian English in UTF-8\n" );
exit( 1 );
}
// The UTF-8 representation of string "æ°´è°ƒæ*OE头"
// (four Chinese characters pronounced shui3 diao4 ge1 tou2) */
char utf8[] = "\xE6\xB0\xB4\xE8\xB0\x83\xE6\xAD\x8C\xE5\xA4\xB4" ;
mbstowcs( ucs2 , utf8 , sizeof(ucs2) / sizeof(*ucs2) );
printf( " UTF-8: " );
for( char *p = utf8 ; *p ; p++ )
printf( "%02X ", (unsigned)(unsigned char)*p );
printf( "\n" );
printf( "Unicode: " );
for( wchar_t *p = ucs2...
The C standard library offers quite a bit of functionality. The first thing to be aware of when using any part of this library is what’s in it. The following tables provide the header filenames and descriptions of the functions prototyped in each header file.
The following table shows the library files before C99:
The following table shows which files have been added to C99:
The following table shows which files have been added to C11:
If you have been compiling programs throughout this book, these files will already exist on your system. You need to find out where they are so that you can open them with an editor and examine exactly what is in them.
In a terminal/console with a Unix shell (such as csh
, tsh
, bash
, and so on), do the following:
hello.c
.cc -H hello.c
Ouch! Way too much information. What you are seeing is the full #include
stack of every single header file that is included in each header file. As you can see, some are included a lot of times.
You can also see that a lot of header files include other header files.
In a terminal/console with a Unix shell (such as csh, tsh, bash, and so on), do the following:
hello.c
.cc -H hello.c 2>&1 | grep '^\.\ '
This command, which looks like a lot of gobbledegook, is doing the following:
-H
option. The list of header files is sent to stderr
.2>&1
redirects stderr to stdout
.|
) to grep
, a regular expression parser.<period><space>
:'…'
is the search string. ^
indicates the beginning of a line.\.
is a period (this is important, as a dot (.
) alone has special meaning in grep).\
is a space (this is important, as a space alone has special meaning in grep).This one is the simplest of all if you have the locate
program on your system.
In your terminal/console, enter the following command:
locate <filename.h>
You might also get a lot of output from this, since your system might have many versions of these header files.
Method 2 is best because it tells you exactly which header file the compiler is using. Once you have found the function you want to know more about in one of these files, you can then use the Unix man
command to read about it on your system. To do so, enter the following into a terminal/console:
man 3 <function>
This tells man to look in section 3 for the given function. Section 3 is where C functions are described.
Alternatively, you could try the following:
man 7 <topic>
Section 7 is where general topics are described. There is a lot of information there.
Note
If you are new to man
, try entering man man
and it will tell you about itself.