[英]Storing ASCII characters in an array in C
I am trying to make a random generator using ASCII characters and due to my minimal knowledge, I am having difficulty trying to identify the best way to go about making this random generator. 我正在尝试使用ASCII字符制作一个随机生成器,并且由于我的基本知识,我很难找到确定制作此随机生成器的最佳方法。
Currently, I have managed to make a simple array which currently holds 6 values. 目前,我设法制作了一个简单的数组,该数组目前包含6个值。
#include <stdio.h>
#include <conio.h>
int main()
{
int i;
int array[255] = {1, 2, 3 , 4, 5, 6 };
for(i = 0; i < 6; i++)
{
printf("%d", array[i]); /* Prints out all values declared in the array*/
}
return 0;
}
output: 123456
输出:123456
What I am currently stuck on now is trying to find out how to replace the numbers with ASCII characters. 我目前所坚持的是尝试找出如何用ASCII字符替换数字。 If anyone could help me figure this out, I would be very grateful.
如果有人能帮助我解决这个问题,我将不胜感激。
Use character literals in the array elements, and print them with %c
format. 在数组元素中使用字符文字,并以
%c
格式打印它们。
#include <stdio.h>
#include <conio.h>
int main()
{
int i;
int array[255] = {'1', '2', '3' , '4', '5', '6' };
for(i = 0; i < 6; i++)
{
printf("%c", array[i]); /* Prints out all values declared in the array*/
}
return 0;
}
You could also change the declaration of the array to char array[255]
, since it only holds characters. 您也可以将数组的声明更改为
char array[255]
,因为它仅包含字符。 For the purposes of printf()
it doesn't matter, because char
variables are automatically converted to int
when calling a variadic function like printf
(but I wouldn't be surprised if compilers print a warning about it, since it's unusual). 出于
printf()
的目的,这无关紧要,因为在调用诸如printf
类的可变函数时, char
变量会自动转换为int
(但是如果编译器打印出警告,这是很不寻常的,我不会感到惊讶)。
Your initial code is already pretty accurate. 您的初始代码已经非常准确。
Your only mistake is: printf("%d", array[i]);
您唯一的错误是:
printf("%d", array[i]);
The %d specifier will output an signed decimal integer , to output a character use the %c specifier. %d说明符将输出一个带符号的十进制整数 ,要输出字符,请使用%c说明符。 You can view all the documentation (including specifiers) for
printf
here . 您可以在此处查看有关
printf
所有文档(包括说明符)。
I also took the liberty of replacing the elements in the array with integers that result in alphanumeric output. 我还自由地用导致字母数字输出的整数替换数组中的元素。
#include <stdio.h>
#include <conio.h>
int main()
{
int i;
int array[255] = { 65, 66, 67 , 68, 69, 70 };
for(i = 0; i < 6; i++)
{
printf("%c", array[i]); // Outputs: ABCDEF
}
return 0;
}
Your next steps are to just randomise length and the elements of the array! 接下来的步骤是将长度和数组元素随机化! Good luck.
祝好运。
First of all the computer does not know anything about the characters - from its poit of view everything is just the number. 首先,计算机对字符一无所知-从它的观点来看,一切都只是数字。
char
type is just the integer value of the size (in bits) CHAR_BITS. char
类型只是CHAR_BITS大小的整数值(以位为单位)。 In most systems it is 8 bits. 在大多数系统中,它是8位。 Depending on your implementation and compile options
char
can be signed or unsigned. 根据您的实现和编译选项,可以对
char
进行签名或取消签名。
Lets consider the signed version. 让我们考虑签名版本。 On 32 bit system:
在32位系统上:
int
type is usually 32 bits wide, int
类型通常为32位宽,
short
is usually 16 bits wide short
通常是16位宽
char
is usually 8 bits wide char
通常为8位宽
so it can be called supershort :) 所以可以称为超短:)
typedef char supershort;
So there is no difference between them except the range of the values those types can accommodate. 因此,除了这些类型可以容纳的值的范围之外,它们之间没有其他区别。
Beginners are often confused by the char constants: 'a'
, 'b'
.... etc and it makes them think that char
is something special , but it is only for the humans convenience - it is much more difficult to remember the whole ASCII table. 初学者经常会被char常量弄糊涂:
'a'
, 'b'
....等,这使他们认为char
是很特别的东西,但这只是为了人类的方便-记住整个函数要困难得多ASCII表。
You can use them with any other number types: long x = 'a'
; 您可以将它们与任何其他数字类型一起使用:
long x = 'a'
; or even double y = '#'
甚至是
double y = '#'
Assuming : you can use char
type as any other integer type. 假设:您可以将
char
类型用作任何其他整数类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.