[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

printf, align & field width?

DaveC

4/22/2016 4:28:00 PM

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]
1 Answer

Malcolm

6/9/2004 11:57:00 PM

0


"DaveC" <bobason456@hotmail.com> wrote in message
> 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.
>
> (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]
>
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.