Odd bits of C

 

This web page has some odd bits and pieces about C - some of the bits that haven`t got their own page. There is a lot to C, so to cover it all in depth would take a lot of web pages.

As I use Linux exclusively as my desktop operating system, I only refer to Linux related bits and pieces. My current favourite Linux is SuSe Linux 11, so my C world revolves around that, and I`m doing everything at the command line.

 

From C to the final application

I started with this, because otherwise I would not have been able to test any code that I wrote.

SuSe Linux 11 includes the open source C compiler "gcc" - it isn`t installed by default, so you need to fight with YAST to install it, either at install time or later. In fact YAST completely failed to install gcc properly, so I ended up using the rpm command at the command line instead. The rpm`s were fine, it was YAST that was the problem.

The steps in the compilation process are, I think, as follows :-

The command for doing a compilation is


        gcc input-filename.c -o output-filename

and this does the whole process, right through to the executable. It is better to have the directory that contains <input-filename.c> as your current directory, rather than having to put in a path to it.

The .o file created by the assembler is deleted automatically, so doesn`t see the light of day at the end of the process. If you want to stop the compilation at the object file, use this command instead :-


        gcc input-filename.c -c

gcc has loads of other switches, many of them are listed in the man file, I haven`t looked at them yet.

Now for the C code stuff.

 

Comments

C scripts can be commented - comments go between two pairs of characters - /* and */, so for a single line comment, use


        /*  Comment goes in here  */

For multiple line comments, you can do something like


        /*  
        * first line of comment
        * next line of comment
        * etc
        */

 

Illegal characters in text strings

I have been unable to find a list of characters that cannot be used in text strings, in a C script. I found it relatively easy to find lists of forbidden characters for sed and perl. There must be several characters you can`t use in C.

There are plenty of texts and web pages that describe special characters using the escape character, ie, the backslash. However this isn`t the information I want.

The illegal characters that I have read about are

But I haven`t found out about any others.

From trying them out, it looks like   (   )   [   ]   {   }   ?   <   >   all work okay, as printf prints them out. According to some sources of information, the question mark should be escaped with a backslash, but in my case it works okay without the backslash.

It looks as if it should be possible to use a hex value instead of the normal alphanumeric type of character in text strings, and it works for all the characters I tried, except for the % sign - using the % as itself works ok, and gets printed out okay. However using the hex value of 25 doesn`t - when using hex, the hex value is preceeded by \x, so to print out a % symbol, you would use \x25 in the text string. However instead of the % symbol, you get a string of mostly numeric characters plus some rather odd characters.

Using hex strings for the \ and the " characters work okay, as well as many others.

I wondered if character encoding had anything to do with it - I am using Gedit to write my C scripts, and by default it uses UTF8 , which is good news for writing web pages, as that is the internet standard. However when I changed Gedit to use Western ( ISO-8859-15 ) - which is the only other option - it made no difference, using \x25 to insert a % symbol still didn`t work. So this is still a mystery at the moment.

There may not be a solution, because of the use of % within the printf function, to specify text formating.

 

Escaping characters in text strings

Just for reference, here is the list of characters that can be escaped.

As an aside, I wonder if you have to use both \r + \n to specify newlines if you are working with C in a MS Windows environment. The standard MS Windows way to specify a newline is to use CR + LF, instead of just the LF character as used in Unix / Linux.

And if so, does this affect the portability of C programmes between Unix / Linux environments and MS Windows environments ?

 

Writing tables with \t

The tab character can used to make basic tables, eg,


        #include <stdio.h>

        int main()
        {
        printf(" \n \n \t alpha \t bravo \t charlie ");
        printf(" \n \n \t delta \t echo \t foxtrot ");
        printf(" \n \n \t hotel \t india \t juliet ");
        printf(" \n \n ");
        return(0);
        }

This produces a table layout which looks like


                alpha   bravo   charlie

                delta   echo    foxtrot

                hotel   india   juliet

However there is a snag - there are 8 character spaces between tabs, and if one of the text strings going into the table is more than 8 characters in length, it carries over into the next tab space, and pushes all subsequent tabs on that row over to the right, so the table is no longer in alignment.


                alpha   bravobravo       charlie

                delta   echo    foxtrot  

                hotel   india   juliet

Double tabbing would allow text strings up to 16 characters, but only if you manually take out one of the next two tabs. So this would be a bit of a problem if you are generating the text strings going into the table from imported data from elsewhere.

 

Formatting different data types

Printf can be instructed to format the output data of variables in different ways, to suit the different data types.

It is done by putting a control string in the first part of the printf argument, and the name of the variable in the second part of the argument.

So if we had a function that produced an integer answer to some calculation, and the result was held in a variable called "dataout", we could print it out by :-


        #include <stdio.h>

        int main()
        {
        stuff;
        more stuff;
        printf(" \n \n \t The result is %d \n \n ", dataout );
        return(0);
        }

The argument can contain more than one control string, it can be any number, but there must be a corresponding variable name for every control string. The variable names are read in the same order as the control strings appear in the first part of the argument.


        #include <stdio.h>

        int main()
        {
        stuff;
        more stuff;
        printf(" \n \n \t Data in is %d, and data out is %d \n \n ", datain, dataout );
        return(0);
        }

The control strings are also known as conversion characters, and here is a list of the ones I have found so far.

 

Other ways to format print-outs

The control strings, or conversion characters, can also be used to control formatting in other ways. for example, using the script


        #include <stdio.h>

        int main()
        {
        stuff;
        more stuff;
        printf("%[number]s","text string \n \n");
        return(0);
        }

where [number] is an integer, tells printf to use a layout which is [number] spaces wide. If [number] is positive, then the text string is right justified within this width. If the number is negative, then the text string is left justified.

If the text string has got more characters than [number], the text string does not get truncated.

As far as I can see, if [number] is greater than the display window width, the layout gets wrapped onto the next line.

If the control string is not %s, but one of the other available ones, then [number] may not be an integer, and will control some other aspect of the argument to be printed out. In addition, there are also the options of using various flags along with [number], to give more options in the print out. However I haven`t got into them yet.

There are probably a lot more ways to do layout formatting, but I haven`t met them yet.

 

 

 

 

 

website design by ron-t

 

website hosting by freevirtualservers.com

 

© 2024   Ron Turner

 

+                                   +  

 

Link to the W3C website.   Link to the W3C website.