473,511 Members | 14,052 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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]
Nov 14 '05 #1
3 3935
DaveC wrote:
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

Nov 14 '05 #2
On Wed, 9 Jun 2004, DaveC wrote:
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]


--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #3

"DaveC" <bo********@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.
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
6792
by: Phil Powell | last post by:
Code: <!-- script src="/js/val_header.js"></script --> <style> <!-- BODY { scrollbar-3d-light-color:#eeeedd; scrollbar-arrow-color:#000000; scrollbar-base-color:#000000;...
3
2676
by: Stanislav Traykov | last post by:
Hello, I have a question about text positioning in a table field. In particular, I want to create a template which works with images of unknown size. There is a <table> field, in which I need...
14
6619
by: jlemay | last post by:
if you look at http://www.emrose.net/contact1.html the last field is a textarea field and I can't make it align with the other text fields. it's pretty close, but this comes from trial and...
4
17149
by: sushant | last post by:
hi why do we use '&' operator in scanf like scanf("%d", &x); but why not in printf() like printf("%d" , x); thnx in advance sushant
5
24383
by: Bilgehan.Balban | last post by:
Hi, I use %#08x to print unsigned integers in hexadecimal format. According to C ref. man. Harbison & Steele, #08 stands for "pad the number with up to 8 zeroes to complete it to 8 digit...
4
3702
by: chandanlinster | last post by:
hello everybody, as i was going through the "printf" man page, i came across this statement. printf("%*d", width, num); what does "*" mean?
6
18026
by: MikeC | last post by:
Folks, Can the width specifier be used in a printf() text string? If I execute... printf("%4s", ".........."); it prints ten dots. If I execute
6
3533
shane3341436
by: shane3341436 | last post by:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta...
43
366
by: Jrdman | last post by:
someone has an idea on how the printf function is programmed ?
0
7144
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7427
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7085
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
449
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.