printf, align & field width?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DaveC

    #1

    printf, align & field width?

    Hi.. I have a C exam in a few days.. Looking back at old exams I find a few
    things we have not been taught / cant remember being taught. Below is a
    question from last year.

    My question is how do you align values and define field widths in the
    printf function? I'm ok with the rest of the question.

    Thanks
    DaveC


    (a) Declare a C structure that can contain data describing a computer.
    The structure contains the following: the speed of the processor in GHz (a
    floating point number), the amount of RAM in Mbytes (an integer), the size
    of
    the hard disk in Gbytes (an integer), the type of processor (a string) and
    the
    name of the manufacturer (a string). [2 marks]
    (b) Declare a global variable of the above structure, and initialise it
    with
    reasonable values. (Do not use assignment statements.) [1 mark]
    (c) Write C code for a function print_computer , that has a single argument
    (a
    pointer to a structure describing the computer).
    The function prints the computer details on one line as follows:
    The manufacturer left aligned in a field width of 30,
    The processor type right aligned in a field width of 12,
    The processor speed right aligned in a field width of 8 (1 decimal place),
    The RAM size right aligned in a field width of 6,
    The hard disk size right aligned in a field width of 6. [3 marks]
  • Case -

    #2
    Re: printf, align & field width?

    DaveC wrote:
    [color=blue]
    > Hi.. I have a C exam in a few days.. Looking back at old exams I find a few
    > things we have not been taught / cant remember being taught. Below is a
    > question from last year.
    >
    > My question is how do you align values and define field widths in the
    > printf function? I'm ok with the rest of the question.[/color]

    Please don't bother us with a lot of text we don't need
    to anwer your question. The field with is a number (or
    two) between the % and the converion character (e.g., x
    for hex values). The details are in every C book or
    online help/manual.

    Good luck with your exam!

    Case

    Comment

    • Darrell Grainger

      #3
      Re: printf, align & field width?

      On Wed, 9 Jun 2004, DaveC wrote:
      [color=blue]
      > Hi.. I have a C exam in a few days.. Looking back at old exams I find a few
      > things we have not been taught / cant remember being taught. Below is a
      > question from last year.
      >
      > My question is how do you align values and define field widths in the
      > printf function? I'm ok with the rest of the question.[/color]

      Alignment, width and precision are part of the printf format specifiers.
      Most the time you see a text book using "%s" or "%d" but you can actually
      have more complex directives like "%-02.3f". I'll leave it to you to look
      up the details. They would be under the reference are for printf, fprintf
      or sprintf.
      [color=blue]
      > (a) Declare a C structure that can contain data describing a computer.
      > The structure contains the following: the speed of the processor in GHz (a
      > floating point number), the amount of RAM in Mbytes (an integer), the size
      > of
      > the hard disk in Gbytes (an integer), the type of processor (a string) and
      > the
      > name of the manufacturer (a string). [2 marks]
      > (b) Declare a global variable of the above structure, and initialise it
      > with
      > reasonable values. (Do not use assignment statements.) [1 mark]
      > (c) Write C code for a function print_computer , that has a single argument
      > (a
      > pointer to a structure describing the computer).
      > The function prints the computer details on one line as follows:
      > The manufacturer left aligned in a field width of 30,
      > The processor type right aligned in a field width of 12,
      > The processor speed right aligned in a field width of 8 (1 decimal place),
      > The RAM size right aligned in a field width of 6,
      > The hard disk size right aligned in a field width of 6. [3 marks]
      >[/color]

      --
      Send e-mail to: darrell at cs dot toronto dot edu
      Don't send e-mail to vice.president@ whitehouse.gov

      Comment

      • Malcolm

        #4
        Re: printf, align & field width?


        "DaveC" <bobason456@hot mail.com> wrote in message[color=blue]
        > My question is how do you align values and define field widths in > the[/color]
        printf function? I'm ok with the rest of the question.[color=blue]
        >
        > (c) Write C code for a function print_computer , that has a single >[/color]
        argument[color=blue]
        > (a
        > pointer to a structure describing the computer).
        > The function prints the computer details on one line as follows:
        > The manufacturer left aligned in a field width of 30,
        > The processor type right aligned in a field width of 12,
        > The processor speed right aligned in a field width of 8 (1 decimal >[/color]
        place),[color=blue]
        > The RAM size right aligned in a field width of 6,
        > The hard disk size right aligned in a field width of 6. [3 marks]
        >[/color]
        This question is a bit unfair, since the earlier questions were very
        fundamental C, whilst this is about little-used bits of the printf()
        function that you may easily not know.

        You can of course call sprintf() to do the conversions of numeric fields to
        text, and then hand-code the field alignments, if you can't figure out how
        to get printf() to do the whole lot in one go.


        Comment

        Working...