Topic : C Lessons
Author : Christopher Sawtell
Page : << Previous 4  Next >>
Go to page :


     for the number to go negative. Also if the word in memory is
going
                 to be used as a bit pattern or a mask and not a number the use
of
                 unsigned is strongly urged. If it is possible for the sign bit
in
                 the bit pattern to be set and the program calls for the bit
pattern
                 to be shifted to the right, then you should be aware that the
sign
                 bit will be extended if the variable is not declared unsigned.
                 The default for the "int" types is always "signed", and, as
discussed
                 above that of the "char" is machine dependent.

  This completes the discussion on the allocation of data types, except to
  say that we can, of course, allocate arrays of the simple types simply by
  adding a pair of square brackets enclosing a number which is the size of
  the array after the variable's name:

  char client_surname[31];

  This declaration reserves storage for a string of 30 characters plus the
  NULL character of value zero which terminates the string.

  Structures.

         Data elements which are logically connected, for example - to use the
         example alluded to above - the dimensions and other details about a
sea
         going ship, can be collected together as a single data unit called a
         struct. One possible way of laying out the struct in the source code
is:

struct ship          /* The word "ship" is known as the

structure's "tag". */
{
  char name[30];
  double displacement;                           /* in grammes */
  float length_of_water_line;                    /* in meters */
  unsigned short int number_of_passengers;
  unsigned short int number_of_crew;
  };


     Note very well that the above fragment of program text does NOT
                 allocate any storage, it merely provides a named template to
the
                 compiler so that it knows how much storage is needed for the
                 structure. The actual allocation of memory is done either like
this:

struct ship cunarder;

                 Or by putting the name of the struct variable between the "}"
and
                 the ";" on the last line of the definition. Personally I don't
                 use this method as I find that the letters of the name tend to
get
                 "lost" in the - shall we say - amorphous mass of characters
which
                 make up the definition itself.

     The individual members of the struct can have values assigned to
                 them in this fashion:

  cunarder.displacement = 97500000000.0;
  cunarder.length_of_water_line = 750.0
  cunarder.number_of_passengers = 3575;
  cunarder.number_of_crew = 4592;


     These are a couple of files called demo1.c & demo1a.c which contain
                 small 'C' programs for you to compile. So, please cut them out
of the
                 news posting file and do so.


----------------------------------------------------------------------
#ident demo1.c  /* If your compiler complains about this line,

chop it out */
#include <stdio.h>

struct ship
{
  char name[31];
  double displacement;                              /* in grammes */
  float length_of_water_line;                       /* in meters */
  unsigned short int number_of_passengers;
  unsigned short int number_of_crew;
  };

char *format = "\
Name of Vessel: %-30s\n\
  Displacement: %13.3f\n\
    Water Line: %5.1f\n\
    Passengers: %4d\n\
          Crew: %4d\n\n";

main()
{
  struct ship cunarder;

  cunarder.name = "Queen Mary";                  /* This is the bad line. */
  cunarder.displacement = 97500000000.0;
  cunarder.length_of_water_line = 750.0
  cunarder.number_of_passengers = 3575;
  cunarder.number_of_crew = 4592;

  printf ( format,
           cunarder.name,
                 cunarder.displacement,
           cunarder.length_of_water_line,
           cunarder.number_of_passengers,
           cunarder.number_of_crew
           );
  }


----------------------------------------------------------------------

                 Why is the compiler complaining at line 21?
     Well C is a small language and doesn't have the ability to allocate
     strings to variables within the program text at run-time. This
                 program shows the the correct way to copy the string "Queen
Mary",
                 using a library routine, into the structure.


----------------------------------------------------------------------
#ident demo1a.c  /* If your compiler complains about this line,

chop it out */
#include <stdio.h>

/*
** This is the template which is used by the compiler so that
** it 'knows' how to put your data into a named area of memory.
*/

struct ship
{
  char name[31];
  double displacement;                              /* in grammes */
  float length_of_water_line;                       /* in meters */
  unsigned short int number_of_passengers;
  unsigned short int number_of_crew;
  };

/*
** This character string tells the printf() function how it is to output
** the data onto the screen. Note the use of the \ character at the end
** of each line. It is the 'continue the string on the next line' flag
** or escape character. It MUST be the last character on the line.
** This technique allows you to produce nicely formatted reports with all the
** ':' characters under each other, without having to count the characters
** in each character field.
*/

char *format = "\n\
Name of Vessel: %-30s\n\
  Displacement: %13.1f grammes\n\
    Water Line: %5.1f metres\n\
    Passengers: %4d\n\
          Crew: %4d\n\n";

main()
{
  struct ship cunarder;

  strcpy ( cunarder.name, "Queen Mary" );           /* The corrected line */
  cunarder.displacement = 97500000000.0;
  cunarder.length_of_water_line = 750.0;
  cunarder.number_of_passengers = 3575;
  cunarder.number_of_crew = 4592;

  printf ( format,
           cunarder.name,
                        


Page : << Previous 4  Next >>