printf, align & field width?

D

DaveC

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]
 
C

Case -

DaveC said:
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.

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
 
D

Darrell Grainger

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.

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.
(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]
 
M

Malcolm

DaveC said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top