print alphabet

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

I have seen code used like this before but my code won't compile. I want
to print the letters a-z but this doesn't work. Should I be using enum?

B

#include <stdio.h>

int main()
{
char alpha[a - z];
int i;
for (i = 0; i < 27; i++) {
printf("%c\n", d);
return 0;
}
return 0;
}
 
S

Shao Miller

    I have seen code used like this before but my code won't compile. I want
to print the letters a-z but this doesn't work. Should I be using enum?

B

#include <stdio.h>

int main()
{
    char alpha[a - z];
    int i;
    for (i = 0; i < 27; i++) {
        printf("%c\n", d);
        return 0;
    }
    return 0;

}

'a' and 'z' look like undeclared identifiers. You might try:

char alpha[] = "abcdefghijklmnopqrstuvwxyz";

and change:

for (i = 0; i < 27; i++) {

to:

for (i = 0; i < sizeof alpha - 1 /* exclude nul-terminator */; i++) {

Using this code, please do not attempt to modify any of the elements
of the 'alpha' array.
 
S

Shao Miller

    I have seen code used like this before but my code won't compile. I want
to print the letters a-z but this doesn't work. Should I be using enum?

#include <stdio.h>
int main()
{
    char alpha[a - z];
    int i;
    for (i = 0; i < 27; i++) {
        printf("%c\n", d);
        return 0;
    }
    return 0;


'a' and 'z' look like undeclared identifiers.  You might try:

char alpha[] = "abcdefghijklmnopqrstuvwxyz";

and change:

for (i = 0; i < 27; i++) {

to:

for (i = 0; i < sizeof alpha - 1 /* exclude nul-terminator */; i++) {

Using this code, please do not attempt to modify any of the elements
of the 'alpha' array.


Oh and 'd' appears to be an undefined identifier, too. :) Did you
mean 'alpha'?
 
S

Shao Miller

    I have seen code used like this before but my code won't compile. I want
to print the letters a-z but this doesn't work. Should I be using enum?

#include <stdio.h>
int main()
{
    char alpha[a - z];
    int i;
    for (i = 0; i < 27; i++) {
        printf("%c\n", d);
        return 0;
    }
    return 0;


'a' and 'z' look like undeclared identifiers.  You might try:

char alpha[] = "abcdefghijklmnopqrstuvwxyz";

and change:

for (i = 0; i < 27; i++) {

to:

for (i = 0; i < sizeof alpha - 1 /* exclude nul-terminator */; i++) {

Using this code, please do not attempt to modify any of the elements
of the 'alpha' array.

Please allow me to clarify on "please do not attempt": If you do,
you'll wreck your alphabet. :) Otherwise, it's perfectly _safe_ to
modify, and that was not the intent of the suggestion.

Google might have dropped another post where I ask if you meant
'alpha' where you typed 'd'. Did you? If not, 'd' is an undeclared
identifier, too.
 
S

Shao Miller

And remove the inner 'return 0;', too. You should not return from the
'main' function after the first 'char' element is printed if you wish
to print the alphabet.
 
J

John Kelly

Google might have dropped another post

Did you know there are free nntp servers, like the one I post with? I
can't tolerate a web interface to news.
 
S

Shao Miller

Did you know there are free nntp servers, like the one I post with?  I
can't tolerate a web interface to news.
Yes. :) I suppose it's about time to install one. :) Thanks for the
stimulus, John.
 
K

Keith Thompson

Vincenzo Mercuri said:
#include <stdio.h>
int main(void) {
for(int i = 'a'; i <= 'z' && putchar(i); i++);
putchar('\n');
return 0;
}

That's not portable; it assumes the values of 'a' through 'z' are
contiguous.

puts("abcdefghijklmnopqrstuvwxyz"); /* yes, that's cheating */
 
K

Keith Thompson

Bill Cunningham said:
I have seen code used like this before but my code won't compile. I want
to print the letters a-z but this doesn't work. Should I be using enum?

B

#include <stdio.h>

int main()
{
char alpha[a - z];
int i;
for (i = 0; i < 27; i++) {
printf("%c\n", d);
return 0;
}
return 0;
}


How many times have we advised you *not to guess*?

If a piece of random code you've written doesn't compile, does
it occur to you read the error messages and try to fix it before
posting here?
 
S

Seebs

How many times have we advised you *not to guess*?

There are two key responses to this:

1. Enough that, at this point, it really doesn't matter whether he's a troll
or someone with some kind of severe brain injury; either way, it seems
unlikely that any benefit comes from posting responses to him.
2. I think the problem is more subtle than that.

Guessing is, ultimately, a big part of how experienced and successful
programmers design, debug, and test. The problem is that the guesses must
be *educated* guesses -- you have to look at the available information and
come up with theories about it, then test those. Bill's problem is that
his guesses are not merely uninformed by circumstances, but actually,
with amazing consistency, *contrary* to circumstances.

I'd guess someone like this could be a real asset to a programming team.
Present him with ANY problem, no matter how complicated, ask him what to
try next, and avoid it. I am mostly joking here, but honestly, look at his
track record; could you get things that consistently and that totally
wrong if you TRIED? I'm not sure I could.

-s
 
S

Shao Miller

Keith said:
That's not portable; it assumes the values of 'a' through 'z' are
contiguous.
Just to add a little here: The C99 C Standard draft with filename
'n1256.pdf' does give us guarantees for the characters '0' through '9'
(5.2.1,p3).
puts("abcdefghijklmnopqrstuvwxyz"); /* yes, that's cheating */
This gave me a huge laugh. :) A bonus here is that the single
appearance of the string literal means that it's unlikely to be stored
twice; once for itself and once inside the 'alpha' array. ;)
 
J

jacob navia

Bill Cunningham a écrit :
I have seen code used like this before but my code won't compile. I want
to print the letters a-z but this doesn't work. Should I be using enum?

B

#include <stdio.h>

int main()
{
char alpha[a - z]; (1)
int i;
for (i = 0; i < 27; i++) {
printf("%c\n", d); (2)
return 0; (3)
}
return 0;
}



I think that this troll is breaking a record here...
4 serious bugs for only 4 lines of code (not counting
braces and declarations)

In line (1)
o undeclared identifier a
o undeclared identifier b

In line (2)
o undeclared identifier d

In line (3)
o return 0 will stop the loop atthe first iteration.


To answer his question:

YES BILL, you should be using an enum

enums are nice. Try for instance as an exercise, to enumerate
the bugs, i.e. calculate your BPL (Bugs Per Line).


But you are making progress. You have at least 2 lines without any
bug at all:

for (i=0; i<27;i++)

and

return 0;
 
M

Morris Keesan

But you are making progress. You have at least 2 lines without any
bug at all:

for (i=0; i<27;i++)

Nope. If he wants to print the English alphabet ('a' through 'z'), this
loop executes one time too many.
and

return 0;

but he does have two other lines which are okay:
#include <stdio.h>
and

int i;

and although "int main(void)" would be better, "int main()" isn't actually
a bug.
 
S

Shao Miller

jacob said:
... ... ...
But you are making progress. You have at least 2 lines without any
bug at all:

for (i=0; i<27;i++)
It's none of my business to discuss Bill's intentions nor his coding
learning.

I will offer that the line above does have a bug. There are 26 letters
in the English alphabet. In the array, they'd be numbered 0 through 25.
The controlling expression in the 'for' statement will allow for use
of a 27th element (whose index is 26), which is beyond the number of
letters in the English alphabet.
 
N

Nick Keighley

    I have seen code used like this before but my code won't compile. I want
to print the letters a-z but this doesn't work.

Should I be using enum?
why?

#include <stdio.h>

int main()
{
    char alpha[a - z];
    int i;
    for (i = 0; i < 27; i++) {
        printf("%c\n", d);
        return 0;
    }
    return 0;
}


it doesn't even compile you idiot
 
N

Nick Keighley

It's none of my business to discuss Bill's intentions nor his coding
learning.

I will offer that the line above does have a bug.  There are 26 letters
in the English alphabet.  In the array, they'd be numbered 0 through 25..
  The controlling expression in the 'for' statement will allow for use
of a 27th element (whose index is 26), which is beyond the number of
letters in the English alphabet.

if you review his posting history you might understand some of the
attitude displayed
 
O

osmium

Bill Cunningham said:
I have seen code used like this before but my code won't compile. I
want to print the letters a-z but this doesn't work. Should I be using
enum?

B

#include <stdio.h>

int main()
{
char alpha[a - z];
int i;
for (i = 0; i < 27; i++) {
printf("%c\n", d);
return 0;
}
return 0;
}


Why don't you try to do one thing at a time? Your mention of enum sounds to
me like you think there is a better way of writing the code, if you just
knew what that way was. So my suggestion is, decide whether to:

o - fix the code you posted
o - write a new program to print the alphabet, perhaps using enum.

Fiddling with two problems at once is very confusing. I think it would be
more productive to work on the first bullet. You could start by compiling
the code to see if there are some error message. Then try to figure out
what the *first* message means. This is a kind of time-honored system.

Remember that "code used like this" has no standing in programming. "Like
this" is OK for playing horseshoes but not in programming.
 
L

Lew Pitcher

Bill said:
I have seen code used like this before but my code won't compile. I
want
to print the letters a-z but this doesn't work. Should I be using enum?

B

#include<stdio.h>

int main()
{
char alpha[a - z];
int i;
for (i = 0; i< 27; i++) {
printf("%c\n", d);
return 0;
}
return 0;
}


or


not, for reasons explained elsewhere
#include <stdio.h>
int main(void) {
for(int i = 'a'; i <= 'z' && putchar(i); i++);
putchar('\n');
return 0;
}

/* In the spirit of Bill's attempt */

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char alpha[] = "abcdefghijklmnopqrstuvwxyz";
int i;

for (i = 0; alpha != 0; i++)
printf("%c\n",alpha);

return EXIT_SUCCESS;
}
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top