By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,147 Members | 1,347 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,147 IT Pros & Developers. It's quick & easy.

Steve Summit C notes , exercise 4

P: n/a
it is a very simple and it runs fine BUT still i want to have some
views on this:

-------------- PROGRAMME------------------------------
#include <stdio.h>

int main()
{
int i;

for(i=1; i <= 10; ++i)
{
if(i % 2 == 0)
printf("%d is even\n", i);
else
printf("%d is odd\n", i);
}

return 0;

}

---------------- OUTPUT ----------------------
[arch@voodo steve-summit]$ gcc -std=c99 -pedantic -Wall -Wextra
assign-3_ex-3.c
[arch@voodo steve-summit]$ ./a.out
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
[arch@voodo steve-summit]$

Mar 17 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
/*
Slightly shorter.
It's also trivial to make it branch-less.
(This one has a hidden branch in it).
*/
#include <stdio.h>

int main(void)
{
int i;

for (i = 1; i <= 10; ++i)
printf("%d is %s\n", i, i % 2 ? "odd" : "even");
return 0;
}
/*
C:\tmp>cl eo.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

eo.c
Microsoft (R) Incremental Linker Version 8.00.50727.42
Copyright (C) Microsoft Corporation. All rights reserved.

/out:eo.exe
eo.obj

C:\tmp>eo
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
*/

Mar 17 '07 #2

P: n/a
In article <11*********************@d57g2000hsg.googlegroups. com>,
user923005 <dc*****@connx.comwrote:
>/*
Slightly shorter.
It's also trivial to make it branch-less.
(This one has a hidden branch in it).
*/
#include <stdio.h>

int main(void)
{
int i;

for (i = 1; i <= 10; ++i)
printf("%d is %s\n", i, i % 2 ? "odd" : "even");
return 0;
}
Or this:

#include <stdio.h>
char *eo[] = {"even","odd"};

int main(void)
{
int i;
for (i = 1; i <= 10; ++i)
printf("%d is %s\n", i, eo[i % 2]);
return 0;
}
Mar 17 '07 #3

P: n/a
arnuld wrote:
it is a very simple and it runs fine BUT still i want to have some
views on this:

-------------- PROGRAMME------------------------------
#include <stdio.h>

int main()
{
int i;

for(i=1; i <= 10; ++i)
{
if(i % 2 == 0)
printf("%d is even\n", i);
else
printf("%d is odd\n", i);
You could replace these four lines with
printf("%d is %s\n",i, (i%2) ? "odd" : "even");
}

return 0;

}
Mar 17 '07 #4

P: n/a
"arnuld" <ge*********@gmail.comwrote in message
it is a very simple and it runs fine BUT still i want to have some
views on this:

-------------- PROGRAMME------------------------------
#include <stdio.h>

int main()
{
int i;

for(i=1; i <= 10; ++i)
{
if(i % 2 == 0)
printf("%d is even\n", i);
else
printf("%d is odd\n", i);
}

return 0;

}
It's fine.
The code works, it is readable, and there are no massive inefficiencies.
What could be wrong with it?

Mar 17 '07 #5

P: n/a
On 17 Mar 2007 00:50:18 -0700, "user923005" <dc*****@connx.comwrote:
>
/*
Slightly shorter.
It's also trivial to make it branch-less.
(This one has a hidden branch in it).
*/
Nice. Same, using C-99:

#include <stdio.h>

int main(void)
{
for (int i = 1; i <= 10; ++i)
printf("%2d is %s\n", i, i % 2 ? "odd" : "even");

return 0;
}
F.

--

E-mail: info<at>simple-line<dot>de
Mar 17 '07 #6

P: n/a
arnuld wrote:
it is a very simple and it runs fine BUT still i want to have some
views on this:

-------------- PROGRAMME------------------------------
#include <stdio.h>

int main()
{
int i;

for(i=1; i <= 10; ++i)
{
if(i % 2 == 0)
printf("%d is even\n", i);
else
printf("%d is odd\n", i);
}

return 0;

}
Well done. It is easily readable, which promotes maintainability, a key
attribute of successful commercial programs.

--
Thad
Mar 17 '07 #7

P: n/a
Malcolm McLean wrote:
"arnuld" <ge*********@gmail.comwrote in message
it is a very simple and it runs fine BUT still i want to have some
views on this:

-------------- PROGRAMME------------------------------
#include <stdio.h>

int main()
{
int i;

for(i=1; i <= 10; ++i)
{
if(i % 2 == 0)
printf("%d is even\n", i);
else
printf("%d is odd\n", i);
}

return 0;

}
It's fine.
The code works, it is readable, and there are no massive inefficiencies.
What could be wrong with it?
He's using hardcoded constants instead of configurable values. It's
irrelevant for such a trivial program, but it's an helpful habit to
get into early on.

Mar 17 '07 #8

P: n/a
G. Frege <nomail@invalidwrites:
On 17 Mar 2007 00:50:18 -0700, "user923005" <dc*****@connx.comwrote:
>/*
Slightly shorter.
It's also trivial to make it branch-less.
(This one has a hidden branch in it).
*/

Nice. Same, using C-99:

#include <stdio.h>

int main(void)
{
for (int i = 1; i <= 10; ++i)
printf("%2d is %s\n", i, i % 2 ? "odd" : "even");

return 0;
}
I'll point out that the original program is also valid C99 (as well as
valid C90). The only C99-specific feature in your version is
declaring an object in a for loop.

--
Keith Thompson (The_Other_Keith) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 17 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.