473,665 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question about k&r2

Hi all,

I have been going through the k&r2 book and all the examples are done
with main() instead of int main(void) and k&r2 in the start of chapter
1 does not return 0 so I haven't yet either.

I did two compiles shown below.

$ gcc -std=c89 -Wall exercise_1-9.c -o exercise_1-9
exercise_1-9.c:5: warning: return type defaults to `int'
exercise_1-9.c: In function `main':
exercise_1-9.c:27: warning: control reaches end of non-void function

$ gcc -std=c99 -Wall exercise_1-9.c -o exercise_1-9
exercise_1-9.c:5: warning: return type defaults to `int'
Why doesn't k&r2 have the int return type on main and why doesn't c99
care that I reached the end of a non-void function without returning a
value.

Kind Regards,
Anthony Irwin
Mar 26 '07 #1
24 1796
Anthony Irwin said:
Why doesn't k&r2 have the int return type on main
They rely on the fact that, if a function doesn't explicitly return any
type, it is assumed to return int. This is, in my opinion, poor style.
and why doesn't c99
care that I reached the end of a non-void function without returning a
value.
It does, normally - but they (foolishly?) made a special case for main.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 26 '07 #2
Richard Heathfield wrote:
Anthony Irwin said:

>>Why doesn't k&r2 have the int return type on main


They rely on the fact that, if a function doesn't explicitly return any
type, it is assumed to return int. This is, in my opinion, poor style.

>>and why doesn't c99
care that I reached the end of a non-void function without returning a
value.


It does, normally - but they (foolishly?) made a special case for main.
Ok thanks for the clarification. I will continue my way through the
k&r2 book which is definately a good book the sample programs they get
you to write seem more real some how, its hard to explain.

Thanks to all who recommended it as I would not have bought it if it
wasn't recommended so much here. Funny how one assumes that a smaller
book won't be as good as a bigger one.

Another quick question is var1 = var2 = var3 = var4 = 0; considered
good style to save typing. I saw it in k&r2 and it was a new concept
for me and I have used it a few types already.

Kind Regards,
Anthony Irwin
Mar 26 '07 #3
Anthony Irwin said:
Another quick question is var1 = var2 = var3 = var4 = 0; considered
good style to save typing. I saw it in k&r2 and it was a new concept
for me and I have used it a few types already.
It's a handy shortcut. I would not recommend using it in too complicated
a way. For example, if you end up typing: a = b += c * a / d = e % f;
then you've probably gone too far.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 26 '07 #4
Anthony Irwin wrote:
>
.... snip ...
>
Another quick question is var1 = var2 = var3 = var4 = 0;
considered good style to save typing. I saw it in k&r2 and it
was a new concept for me and I have used it a few types already.
Opinions vary. I like it for various reasons. It emphasizes that
those items are initially equal. It may enable more efficient code
to be generated. It minimizes vertical space used.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 26 '07 #5
Anthony Irwin wrote:
Another quick question is var1 = var2 = var3 = var4 = 0; considered
good style to save typing.
Saving typing is a valid reason for writing code in a certain way,
but it is the weakest of all valid reasons.

The rule is that for the above c code statement,
the assigment of the zero value may be assigned to the vars
in any order. So, make sure to use that kind of code construct,
only when the order of the assignments doesn't matter.

If this is what you mean:
p -next = q;
p = q;
don't write it this way:
p = p -next = q;

--
pete
Mar 26 '07 #6

"Anthony Irwin" <no****@noemail here.nowhereha scritto nel messaggio
news:eu******** **@news-01.bur.connect. com.au...
Why doesn't k&r2 have the int return type on main and why doesn't c99 care
that I reached the end of a non-void function without returning a value.
C99 says that the closing brace of the main() function acts as return 0;},
or something like that (5.1.1.2.3.1).
Mar 26 '07 #7

pete wrote:
Anthony Irwin wrote:
Another quick question is var1 = var2 = var3 = var4 = 0; considered
good style to save typing.

Saving typing is a valid reason for writing code in a certain way,
but it is the weakest of all valid reasons.

The rule is that for the above c code statement,
the assigment of the zero value may be assigned to the vars
in any order. So, make sure to use that kind of code construct,
only when the order of the assignments doesn't matter.

If this is what you mean:
p -next = q;
p = q;
don't write it this way:
p = p -next = q;
What's wrong with the above line? I thought that the assignment
operator has right-left associativity.

Mar 26 '07 #8
CBFalconer wrote:
Anthony Irwin wrote:
... snip ...

Another quick question is var1 = var2 = var3 = var4 = 0;
considered good style to save typing. I saw it in k&r2 and it
was a new concept for me and I have used it a few types already.

Opinions vary. I like it for various reasons. It emphasizes that
those items are initially equal. It may enable more efficient code
to be generated. It minimizes vertical space used.
I'm in the opposite camp. I've never used it in code. I generally feel
that variables that need initialization should be done at declaration
time, and I try to use an individual line for most variable
declarations.


Brian
Mar 26 '07 #9
"CBFalconer " wrote
Anthony Irwin wrote:
>>
... snip ...
>>
Another quick question is var1 = var2 = var3 = var4 = 0;
considered good style to save typing. I saw it in k&r2 and it
was a new concept for me and I have used it a few types already.

Opinions vary. I like it for various reasons. It emphasizes that
those items are initially equal. It may enable more efficient code
to be generated. It minimizes vertical space used.
True, but most modern compilers can optimize away whitespace
and carriage returns.

--
Craig Franck
cr**********@ve rizon.net
Cortland, NY
Mar 27 '07 #10

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

Similar topics

5
7970
by: David | last post by:
this phrase has been mentioned many times in this NG and it seems like everyone know what it's. is it a book? a standard? or a guide of some sort? where can i read more about K&R2? thanks!
16
1676
by: TTroy | last post by:
I FOUND MAJOR ERRORS in K&R2 (making it almost useless for the herein mentioned topics). K&R2 Section 5.9 Pointers vs. Multidimension Arrays starts of like this... "Newcomers to C are somtimes confused about the difference between a two-dimensional array and an array of pointers..." then continues to explain int *b; to be...
16
2267
by: Josh Zenker | last post by:
This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to me. Is there a more elegant way to do this? #include <stdio.h> /* copies input to output, printing */ /* series of blanks as a single one */ int main() { int c;
5
1469
by: Clausfor | last post by:
Hello everybody, I cannot find where in the K&R2 it is stated that variables must be defined at the beginning of a block and not within other lines. In Section 1.2 I read: In C, all variables must be declared before they are used, usually at the beginning of the function before any executable statements. USUALLY does not mean ABSOLUTELY, correct? I know that in C99 it is possible, but I'm interested in C89.
2
2287
by: arnuld | last post by:
there is a solution on "clc-wiki" for exercise 1.17 of K&R2: http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_1:Exercise_17 i see this uses pointers whereas K&R2 have not discussed pointers yet. i have created a solution myself by modifying the example programme of section 1.19. i tried to find the source-code of K&R2 using Google. i found the home
8
4753
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. --------------------------------- PROGRAMME -------------------------------- /* K&R2 section 1.9 exercise 1.19
19
2395
by: arnuld | last post by:
this programme runs without any error but it does not do what i want it to do: ------------- PROGRAMME -------------- /* K&R2, section 1.6 Arrays; Exercise 1-13. STATEMENT: Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical
7
2162
by: somenath | last post by:
Hi All , As a process of my C language learning I was trying to learn how malloc can be implemented from K&R2 .But I am not able to understand few points . It would be very much helpful if some body give some inputs in the bellow mentioned points. Point 1) I page 186 it is said that "In malloc, the requested size in characters is rounded up to the proper number of header-sized units; the block that will be allocated contains one...
15
1807
by: arnuld | last post by:
STATEMENT: Write the function strindex(s, t), which returns the position of the rightmost occurence of t in s, or -1 if there is none. PURPOSE: this program prints the index of the rightmost match on the line. The match we have to find is a char array named pattern. We also print out the number of matches we have found. We will take the input from command-line. PROBLEM: Segmentation Fault
0
8348
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8863
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
8636
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
7376
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
6187
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
5660
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
4356
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1761
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.