Steve Summit C notes , exercise 4

A

arnuld

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]$
 
U

user923005

/*
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
*/
 
K

Kenny McCormack

/*
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;
}
 
M

Martin Ambuhl

arnuld said:
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");
 
M

Malcolm McLean

arnuld said:
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?
 
G

G. Frege

/*
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.
 
S

santosh

Malcolm said:
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.
 
T

Thad Smith

arnuld said:
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.
 
K

Keith Thompson

G. Frege said:
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.
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top