int n = rand() % 26;
char c = (char)(n+65);
cout << c << "\n";
return 0;
Is it possible to do it in C programming? Thank you very much
Best regards.
Anna
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Ignoring the "cout << ..." line, the above is equally valid in C and
in C++. If you really didn't know that, you need to go back and learn
C from the beginning. I recommend Kernighan & Ritchie, "The C
Programming Language", 2nd Edition, affectionately known as K&R2.
BTW, your code is equally non-portable in both C and C++.
Apparently what you're trying to generate is not random characters,
but random uppercase letters. The above will do that only if your
system happens to be using an ASCII-based character set, where 'A' is
65 and the 26 uppercase letters have contiguous codes.
There are ways to generate random uppercase letters without depending
on the representation of the underlying character set, but I won't go
into that for now. Instead, let's say you're willing to assume ASCII,
and don't mind that your code won't work on non-ASCII systems (not all
code *has* to be 100% portable).
Your first line:
int n = rand() % 26;
is ok, but 26 is a "magic number". As long as you're assuming that
the letters are contiguous, you can compute the number of letters
rather than assuming that it's 26:
int n = rand() % ('Z' - 'A' + 1);
Your second line:
char c = (char)(n+65);
uses an unnecessary cast. Most casts are, in fact, unnecessary. A
cast specifies an implicit conversions; most useful conversions can be
done implicitly. And you use another "magic number", this one quite
unnecessary. I didn't remember off the top of my head that 65 happens
to be the ASCII code for 'A'; I had to check. You can make your code
easier to read by writing 'A' when you mean 'A':
char c = n + 'A';
Before calling rand(), you should call srand(), unless you want the
same sequence of pseudo-random numbers every time you run your
program.
In addition to K&R2, I recommend the comp.lang.c FAQ,
<http://www.c-faq.com/>. It's not a good way to learn C from scratch
(it's not intended to be), but it's an excellent resource for
answering the inevitable questions that will pop up as you're learning
the language. A lot of things in C are surprising and
counterintuitive; the FAQ will help you get past these obstacles.
--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
>Hello,
>I would like to know if there's a way to create random char in C
>programming. I've been searching all over the place and all the
>solution I found are only available for C++ for example:
>
> int n = rand() % 26;
> char c = (char)(n+65);
This code only works on systems using ASCII character encoding. There
are systems using other encodings, such as EBCDIC. It also only
generates upper case characters.
> cout << c << "\n";
This is the only line that is unique to C++. If you delete it or
replace it with a call to printf, you will have valid C code.
> return 0;
>
>Is it possible to do it in C programming? Thank you very much
>Best regards.
>Anna
Remove del for email
rand() exists in C as well - in fact, it is one of the many library
functions C++ inherited from C. The prototype is in <stdlib.h>.
DES
--
Dag-Erling Smørgrav - d...@des.no
It would be the same basic idea, but with some tweaks.
Generate a random number from 0-25, using rand().
Convert this to a letter. Note that the C++ example will only
work if the system uses the ASCII character set. To make
this more portable, use a string consisting of the alphabet
and use the number as a subscript, as in:
char c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[n];
And, to make it even "more portable", use #define for the string, and
use sizeof() to determine the number of letters in the alphabet, just
in case you are using some non-English language.
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsA...@gmail.com>
It is. The C++ code above looks like it's supposed to generate any
character between 'A' to 'Z' inclusive.
In C, you cannot assume that the character set you're using is ASCII.
You can't assume 'A' is represented by 65, 'B' by 66, etc. If what
you're after is generating a capital letter:
char *letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int num_of_letters = strlen(letters);
int n = rand() % num_of_letters;
printf("%c\n", letters[n]);
As a side note, the randomness of rand() varies from one implementation
to another. If the one you have is good enough, no problem. Otherwise,
you may want to search for other random number generation algorithms,
such as Mersenne Twister.
This is poor style; letters should be const. The only reason you won't
get a compiler warning or error here is that WG14 wimped out in the name
of "existing body of code" and special-cased the assignment of a string
literal to a char *.
DES
--
Dag-Erling Smørgrav - d...@des.no
Agreed.
> The only reason you won't
> get a compiler warning or error here is that WG14 wimped out in the name
> of "existing body of code" and special-cased the assignment of a string
> literal to a char *.
Not exactly. The reason you don't get a warning is that string
literals in C are not const; attempting to modify a string literal
invokes undefined behavior because there's a specific rule that says
so. The fact that you can assign of a string literal to a char* isn't
a special case, it's a consequence of other rules.
--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"