Reader small image

You're reading from  Learn C Programming. - Second Edition

Product typeBook
Published inAug 2022
PublisherPackt
ISBN-139781801078450
Edition2nd Edition
Right arrow
Author (1)
Jeff Szuhay
Jeff Szuhay
author image
Jeff Szuhay

Jeff Szuhay is the principal developer at QuarterTil2 which specializes in graphics-rich software chronographs for desktop environments. In his software career of over 35 years, he has engaged in a full range of development activities from systems analysis and systems performance tuning to application design, from initial development through full testing and final delivery. Throughout that time, he has taught computer applications and programming languages at various educational levels from elementary school students to university students, as well as developed and presented professional, on-site training.
Read more about Jeff Szuhay

Right arrow

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.

C keywords

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.

Table of operators and their precedence

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:

Summary of useful GCC and Clang compiler options

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.

ASCII character set

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:

The Better String Library (Bstrlib)

Here is the introduction to Bstrlib taken from its document file:

“The bstring library is an attempt to provide improved string processing functionality to the C and C++ language. At the heart of the bstring library (Bstrlib for short) is the management of “bstring”s which are a significant improvement over ‘\0’ terminated char buffers.”

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...

A quick introduction to Bstrlib

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:

  • Core C files (one source file and header)
  • Base Unicode support, if needed (two source files and headers)
  • Extra utility functions (one source file and header)
  • A unit/regression test for Bstrlib (one source file)
  • A set of dummy functions to abort the use of unsafe C string functions (one source file and header)

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...

A few simple examples

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...

Unicode and UTF-8

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.

A brief history

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.

Where we are today

Unicode now replaces older character encodings,...

A UTF to Unicode example

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

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.

Method 1

In a terminal/console with a Unix shell (such as csh, tsh, bash, and so on), do the following:

  1. Create a simple program – for example, hello.c.
  2. Add the header file you want to find and save it.
  3. In a bash command shell, execute the following:
    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.

Method 2

In a terminal/console with a Unix shell (such as csh, tsh, bash, and so on), do the following:

  1. Create a simple program – for example, hello.c.
  2. Add the header file you want to find, and save it.
  3. In a bash command shell, execute the following:
    cc -H hello.c 2>&1 | grep '^\.\ '

This command, which looks like a lot of gobbledegook, is doing the following:

  1. It invokes the compiler with the -H option. The list of header files is sent to stderr.
  2. 2>&1 redirects stderr to stdout.
  3. stdout is then redirected via a pipe (|) to grep, a regular expression parser.
  4. grep is told to search the beginning of each line for <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).
  5. You will now only see one or two...

Method 3

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.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learn C Programming. - Second Edition
Published in: Aug 2022Publisher: PacktISBN-13: 9781801078450
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Jeff Szuhay

Jeff Szuhay is the principal developer at QuarterTil2 which specializes in graphics-rich software chronographs for desktop environments. In his software career of over 35 years, he has engaged in a full range of development activities from systems analysis and systems performance tuning to application design, from initial development through full testing and final delivery. Throughout that time, he has taught computer applications and programming languages at various educational levels from elementary school students to university students, as well as developed and presented professional, on-site training.
Read more about Jeff Szuhay