473,786 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

give me some tips

"No newline at the end of your output" is really a problem? I've never
learned that before.
So I desire to know some tips about writting a program perfectly.Can
anyone give me some tips which are hardly learned from textbook or
easily ignored?
Thank you very much.

Mar 12 '06 #1
28 3334
Yuri CHUANG wrote:

"No newline at the end of your output" is really a problem? I've never
learned that before.
So I desire to know some tips about writting a program perfectly.Can
anyone give me some tips which are hardly learned from textbook or
easily ignored?
Thank you very much.


http://c-faq.com/

--
pete
Mar 12 '06 #2
Well, your output will look much better if you have a practice of
putting \n at the end of a printf argument, etc.
Albert

Mar 12 '06 #3
On Sunday 12 March 2006 06:18, Albert opined (in
<11************ **********@v46g 2000cwv.googleg roups.com>):
Well, your output will look much better if you have a practice of
putting \n at the end of a printf argument, etc.
Albert


Please quote who and what you're replying to. Read and follow advice
from <http://cfaj.freeshell. org/google/>.

Back to your reply... "look much better" really has nothing to do with
this (as we all know, the beauty is in the eye of the be(er)holder).

What has *everything* to do with it is that if you do not terminate your
output with new line, or flush it with `fflush`, the compiler is not
required to provide /any/ output whatsoever. It's in the Standard, if
you're still wondering why.

--
BR, Vladimir

After any salary raise, you will have less money at the end of the
month than you did before.

Mar 12 '06 #4
Albert schrieb:
Well, your output will look much better if you have a practice of
putting \n at the end of a printf argument, etc.


Please, quote what you are replying to. If you are using
google to post, have a look at
http://www.clc-wiki.net/wiki/Introdu....c#googleposts
please.

Apart from that, your answer is not at all helpful as the OP
was not asking for aesthetic reasons but for the language
reasons; you could have pointed him or her to FAQ 12.4 or similar.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mar 12 '06 #5

"Yuri CHUANG" <yu*******@126. com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
So I desire to know some tips about writting a program perfectly.
There are two assumptions made in that statement:
1) that someone can always determine a method to solve every problem
2) that someone can always write a program which implements a solution
without errors

I'd start by learning how to solve some problems and write some programs
well. After solving and implementing
a few problems, you'll know whether you are better at "top-down" or
"bottoms-up" design. Each person has a
biological preference for each one (FYI, those who prefer "top-down" are N,
and "bottoms-up" are S, in Myers-Briggs personality Type Indicator).
Can
anyone give me some tips which are hardly learned from textbook or
easily ignored?


The following are my opinions for the C language are really are _specific_
to _my_ programming style. Others may not
have problems with the same areas of C that I do, so they probably have
their own list of issues. You'll need to develop
your own list of "problem areas" over time:

1) learn hexadecimal and the bit patterns for each value
2) don't forget your algebra
3) understand binary operations bitshifts, and, or, xor, one and two's
complement, etc...
4) use #define masks to extract bits from variables instead of using unions
5) include all the necessary include files, especially stdio.h and stdlib.h
6) avoid the use casts, except to ensure the type and precision of numerical
conversions and where you know it is necessary
7) avoid the use of type qualifiers (const, volatile, restrict), except
where necessary, because they can hide programming errors
8) avoid the use malloc(), except for dynamic data, because it leads to
memory allocation errors
8A) corollary: avoid the use pointers in main(), for the same reason
9) typedef structs and give them file scope (i.e., "global"), it can simply
certain problems
Rod Pemberton
Mar 12 '06 #6
Michael Mair <Mi**********@i nvalid.invalid> writes:
Albert schrieb:
Well, your output will look much better if you have a practice of
putting \n at the end of a printf argument, etc.


Please, quote what you are replying to. If you are using
google to post, have a look at
http://www.clc-wiki.net/wiki/Introdu....c#googleposts
please.

Apart from that, your answer is not at all helpful as the OP
was not asking for aesthetic reasons but for the language
reasons; you could have pointed him or her to FAQ 12.4 or similar.


Note that FAQ 12.4 recommends the use of fflush(stdout), which is a
good idea for prompts, but isn't guaranteed to help if the very last
thing your program writes to stdout doesn't end in a new-line.

What the standard says is (C99 7.19.12p2):

A text stream is an ordered sequence of characters composed into
_lines_, each line consisting of zero or more characters plus a
terminating new-line character. Whether the last line requires a
terminating new-line character is implementation-defined.

It's not clear what happens if the implementation requires a
terminating new-line and the program doesn't provide one; I suppose
it's undefined behavior. The most likely consequence, I suppose, is
that the last line doesn't appear.

I don't know of any implementations that actually require the trailing
new-line, but on systems I use, if a program's stdout is directed to a
terminal and there's no trailing new-line, the last line might be
overwritten by the next shell prompt. Even if it isn't, having the
prompt appear in the same line immediately following the program's
output is ugly:

prompt% ./hello
hello, worldprompt %

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 12 '06 #7

"Rod Pemberton" <do*********@so rry.bitbucket.c mm> wrote in message
news:dv******** ***@news3.infoa ve.net...

"Yuri CHUANG" <yu*******@126. com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
So I desire to know some tips about writting a program perfectly.
There are two assumptions made in that statement:
1) that someone can always determine a method to solve every problem
2) that someone can always write a program which implements a solution
without errors

I'd start by learning how to solve some problems and write some programs
well. After solving and implementing
a few problems, you'll know whether you are better at "top-down" or
"bottoms-up" design. Each person has a
biological preference for each one (FYI, those who prefer "top-down" are

N, and "bottoms-up" are S, in Myers-Briggs personality Type Indicator).
Can
anyone give me some tips which are hardly learned from textbook or
easily ignored?
The following are my opinions for the C language are really are _specific_
to _my_ programming style. Others may not
have problems with the same areas of C that I do, so they probably have
their own list of issues. You'll need to develop
your own list of "problem areas" over time:

1) learn hexadecimal and the bit patterns for each value
2) don't forget your algebra
3) understand binary operations bitshifts, and, or, xor, one and two's
complement, etc...
4) use #define masks to extract bits from variables instead of using

unions 5) include all the necessary include files, especially stdio.h and stdlib.h 6) avoid the use casts, except to ensure the type and precision of numerical conversions and where you know it is necessary
7) avoid the use of type qualifiers (const, volatile, restrict), except
where necessary, because they can hide programming errors
8) avoid the use malloc(), except for dynamic data, because it leads to
memory allocation errors
8A) corollary: avoid the use pointers in main(), for the same reason
9) typedef structs and give them file scope (i.e., "global"), it can simply certain problems


I'd also add:
10) use unsigned types whenever possible

Rod Pemberton
Mar 12 '06 #8
Rod Pemberton schrieb:
"Yuri CHUANG" <yu*******@126. com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
So I desire to know some tips about writting a program perfectly.
There are two assumptions made in that statement:
1) that someone can always determine a method to solve every problem
2) that someone can always write a program which implements a solution
without errors

I'd start by learning how to solve some problems and write some programs
well. After solving and implementing
a few problems, you'll know whether you are better at "top-down" or
"bottoms-up" design. Each person has a
biological preference for each one (FYI, those who prefer "top-down" are N,
and "bottoms-up" are S, in Myers-Briggs personality Type Indicator).


Hmmm, and what am I if I prefer one approach for some kinds of problems
and the other for other kinds of problems and the
midlevel-to-up-and-down and the come-from-top-and-bottom for yet
other classes of problems?
Can
anyone give me some tips which are hardly learned from textbook or
easily ignored?


The following are my opinions for the C language are really are _specific_
to _my_ programming style. Others may not
have problems with the same areas of C that I do, so they probably have
their own list of issues. You'll need to develop
your own list of "problem areas" over time:

1) learn hexadecimal and the bit patterns for each value
2) don't forget your algebra
3) understand binary operations bitshifts, and, or, xor, one and two's
complement, etc...


If you need them, learn something about floating point numbers
and their shortcomings so you are not in for nasty surprises.
4) use #define masks to extract bits from variables instead of using unions
and use functions to wrap the semantics whenever the bit thing
is only an implementation decision...
5) include all the necessary include files, especially stdio.h and stdlib.h
6) avoid the use casts, except to ensure the type and precision of numerical
conversions and where you know it is necessary
7) avoid the use of type qualifiers (const, volatile, restrict), except
where necessary, because they can hide programming errors
How can const lead to errors unless you violate (6)?
8) avoid the use malloc(), except for dynamic data, because it leads to
memory allocation errors
.... it _can_ lead ...
8A) corollary: avoid the use pointers in main(), for the same reason
Avoid implementing any functionality in main(); main() essentially
just should call functions and assign to temps...
This step usually makes it easier to structure the program and the
tasks.
9) typedef structs and give them file scope (i.e., "global"), it can simply
certain problems


Are you talking about the type definition or about the instance?

@OP: As Rod said, everyone has their own list.
A look at a good textbook and the C FAQ before you start out
implementing something may make you aware of potential problems.

A couple of things which are not strictly C which I would add:
i) Use your compiler in the strictest possible mode, i.e.
a) use only standard C if you can get away with it
b) turn the warning level to maximum
ii) If sensible, use a lint-tool.
iii) If you get warnings or error messages, understand them. Do
not try to make them "go away" with tricks but by making sure
the (potential) problem they point out does not occur or by
changing the code so the problem cannot occur.
If you decide that the warning is not critical and your compiler
offers a #pragma to switch off a warning for a code section,
then switch it off for the smallest possible code section and
write a comment explaining why you switched it off.
iv) If you get beyond one file or beyond a thousand lines of code
or if you know beforehand that you might, then _plan_ahead_:
a) Use make or similar tools to automate the build process
b) Test every function or at least every module thoroughly and
separately -- it is much easier to find bugs this way.
If you design the test before writing the code, this may even
save you some time because you know which pitfalls to avoid.
c) Make tests that test the interaction of different software
components/modules/functions.
v) Do never ever take shortcuts in order to "optimise".
If your code is too slow and you have found out by measuring
which part it is that slows you down, then think about a better
way of implementing it. Keep the slow function/group of functions
so that you test whether the optimised code does the same. And
measure whether the optimised part is actually faster/more memory
efficient.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mar 12 '06 #9
Keith Thompson schrieb:
Michael Mair <Mi**********@i nvalid.invalid> writes:
Albert schrieb:
Well, your output will look much better if you have a practice of
putting \n at the end of a printf argument, etc.


Please, quote what you are replying to. If you are using
google to post, have a look at
http://www.clc-wiki.net/wiki/Introdu....c#googleposts
please.

Apart from that, your answer is not at all helpful as the OP
was not asking for aesthetic reasons but for the language
reasons; you could have pointed him or her to FAQ 12.4 or similar.


Note that FAQ 12.4 recommends the use of fflush(stdout), which is a
good idea for prompts, but isn't guaranteed to help if the very last
thing your program writes to stdout doesn't end in a new-line.

What the standard says is (C99 7.19.12p2):

<snip: explanation>

I admit I was too lazy to search for the appropriate FAQ;
I just assumed that somewhere around there, there should be
an appropriate FAQ entry. Looking there, I don't find one...
Considering that this is something corrected fairly often,
I am surprised that one cannot find that in the FAQ.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mar 12 '06 #10

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

Similar topics

3
1865
by: arthur-e | last post by:
A new Machine was set up by our IS department - it came with Office Pro 2003. Access97, Word97 and Excel 97 were added. I'm trying to update an Access 97database to 2003 but get weird compiling errors - Date, Format and Left give errors. I've registered the missing (or those ocx's) that were indicated as missing- iincluding - mscomct2.ocx - and the date picker showed up but
7
2155
by: MLH | last post by:
?2944*24 gives me an overflow error, entered in the immediate window. Anybody else?
0
1044
by: mike | last post by:
Recently I posted a message about deploying an application over a network. I had the .net framework installed on the pcs, the local intranet set to full trust, and I had copied the bin directory to the production location. However, I didn't have any trouble running the program on any pcs that had visual studio 2003 installed on it. So, there was no problem with any development machines. However, some non-development machines would run the...
44
4197
by: Christoph Zwerschke | last post by:
In Python, it is possible to multiply a string with a number: >>> "hello"*3 'hellohellohello' However, you can't multiply a string with another string: >>> 'hello'*'world' Traceback (most recent call last): File "<interactive input>", line 1, in ?
2
1571
by: Lauren Wilson | last post by:
Hi Folks, We have a user who is suddenly getting the following error message when they start our very mature application. Other users of the same version are NOT getting this error: "Error 3075 - Function is not available in expressions in query expression 'Trim(!)' " I believe that this function comes from the DAO 3.6 library.
1
2103
by: Ellen12c | last post by:
On the last post I asked about a code to make a vertical scroll bar and ends up I didn't need one. I was told by a big group of people on this forum that I needed to do away with all the flash and the backgrounds to make the web site more conservative. I received post to let members know so they can go and give me tips and more help to make the web sit better. So I'm finished the new web site it took me 2 days to make it....
5
1222
by: Shanthagomathi | last post by:
can u pl give me some java1.5 tips which should be useful for projects..it can be general also Thanks
10
2149
by: sara | last post by:
Hi - I have a report that is 14 columnar sub-reports (Line up: Position- holders in each of our 14 locations - Manager, Assistant Manager, Receiving, Office, etc). I output directly to PDF (using ConvertReportToPDF from this site - Stephan Lebans) The formatting - Outlines on some fields - do not appear on subreports
0
1234
by: zzzmail.01 | last post by:
I find some useful tips about C/C++ and want to share with you. Sorry if it bothers you. Tips for better Coding Style: http://tmh-coding-tips.blogspot.com/2008/09/cc-guidelines-for-better-coding-style.html Tips for better Memory Management: http://tmh-coding-tips.blogspot.com/2008/09/cc-tips-for-better-memory-management.html Tips for Performance Enhancements
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8989
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.