用C打印数组元素

时间:2021-12-14 21:28:07

Well, it is a basic program which I got an array of chars, like :

好吧,这是一个基本程序,我得到了一系列字符,如:

char alphabet[5] = {'A', 'B2', 'C', 'D4', 'E'};

char alphabet [5] = {'A','B2','C','D4','E'};

and I want to print elements of that array, inside of a for-loop :

我想在for循环中打印该数组的元素:

int remainder = my_number % 5; printf("%c\n", alphabet[remainder]);

int remainder = my_number%5; printf(“%c \ n”,字母[余数]);

thats it's where I can't make it run as expected the print out comes A, 2, C, 4 or E. How I can make it print B2 and D4 ?

多数民众赞成在那里,我不能让它按预期运行,打印输出A,2,C,4或E.我怎么能打印B2和D4?

I've heard about using enums but I din't understand how I could use, I get confuse on the idea of elements have value and how I could get the elements out using they value, e.g: enum letters {'a', 'b', 'c'}; a = 0, b = 1 and c = 2.

我听说过使用枚举但是我不明白我是如何使用的,我对元素有价值的想法感到困惑,以及如何使用它们来获取元素值,例如:枚举字母{'a',' b','c'}; a = 0,b = 1且c = 2。

(maybe just my syntax it's not correct, but please if you can help me ...)

(也许只是我的语法,这是不正确的,但如果你可以帮助我...)

I'll open my code for easier explanation.

我将打开我的代码以便于解释。

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

const char *notes[13] = {"C", "C2", "D", "D2", "E", "F", "F2", "G", "G2", "A", "A2", "B", "c"}; // The 2's is for the "#"

int main(int argc, char const *argv[]) // Just using argv[1] to say how much numbers I want to print 
{
  int now, before_1 = 1, before_2 = 1, rem, steps; // "rem" stands for remainder
  char note;

  if (atoi(argv[1]) <= 2)
    for (steps = 0; steps < atoi(argv[1]); steps++)
      printf("1\n");
  else
    for (steps = 0; steps < atoi(argv[1]); steps++) // Main loop for Fibonacci's Numbers
    {
      now = before_1 + before_2;
      rem = now % 13;
      note = notes[rem];
      printf("Note : %c <=> Rem : %d. Num : %d\n", note, rem, now);

      before_2 = before_1; // Change values of variables
      before_1 = now;
    }

  return 0;
}

The idea is get the numbers of Fibonacci's Serie and transform them into all 13 notes possible (include the sharp ones), but as I've said the output is note going too good.

这个想法是得到斐波纳契系列的数量,并将它们转换成所有可能的13个音符(包括尖锐的音符),但正如我所说的那样,输出效果太好了。

4 个解决方案

#1


3  

In the original version of this question, the problem is twofold.

在这个问题的原始版本中,问题是双重的。

First, alphabet is defined as an array of characters, i.e. char alphabet[]. According to the initial question, it should rather be declared as an array of strings, i.e. char *alphabet[].

首先,字母表定义为字符数组,即char alphabet []。根据最初的问题,它应该被声明为一个字符串数组,即char * alphabet []。

Second, which in some way follows from the first issue, the initialization values are characters, whereas they should be defined as strings. The definition of F2 as a character, hence 'F2', will not represent a literal string "F2".

其次,从第一个问题开始,在某种程度上,初始化值是字符,而它们应该被定义为字符串。 F2作为字符的定义,因此'F2',不代表文字字符串“F2”。

char *alphabet[] = {"A", "B2", "C", "D4", "E"};

Finally, in order to print strings rather than characters, the printf format identifier has to be changed from %c to %s.

最后,为了打印字符串而不是字符,printf格式标识符必须从%c更改为%s。

Summing up, you can print the elements of a string array as follows:

总结一下,您可以打印字符串数组的元素,如下所示:

char *alphabet[5] = {"A", "B2", "C", "D4", "E"};
for(int i = 0; i < 5; i++){
    printf("%s} ", alphabet[i]);
}

If you don't know the size of the array from the beginning, you can also write:

如果从一开始就不知道数组的大小,你也可以写:

char *alphabet[] = {"A", "B2", "C", "D4", "E"};
for(int i = 0; i < sizeof(alphabet)/sizeof(alphabet[0]); i++){
    printf("%s ", alphabet[i]);
}

Hope it helps!

希望能帮助到你!

#2


1  

char is used for single characters. If you want multiple characters you need to use strings.

char用于单个字符。如果你想要多个字符,你需要使用字符串。

char *strings[] = { "A", "B2", "C", "D4", "E"};

int remainder = my_number % 5;
printf("%s\n", strings[remainder]);

#3


-1  

As odin19 pointed out, B2 and D4 are not characters in C. In order to print them you can use double quotes in the elements of the array and print them as strings using %s in your printf.

正如odin19指出的那样,B2和D4不是C中的字符。为了打印它们,您可以在数组元素中使用双引号,并在printf中使用%s将它们打印为字符串。

#4


-1  

Single quotes are used to indicate a char. So 'B2' would be treated a char. Instead you want char*, and thus double quotes.

单引号用于表示char。因此'B2'将被视为char。相反,你想要char *,因此需要双引号。

#1


3  

In the original version of this question, the problem is twofold.

在这个问题的原始版本中,问题是双重的。

First, alphabet is defined as an array of characters, i.e. char alphabet[]. According to the initial question, it should rather be declared as an array of strings, i.e. char *alphabet[].

首先,字母表定义为字符数组,即char alphabet []。根据最初的问题,它应该被声明为一个字符串数组,即char * alphabet []。

Second, which in some way follows from the first issue, the initialization values are characters, whereas they should be defined as strings. The definition of F2 as a character, hence 'F2', will not represent a literal string "F2".

其次,从第一个问题开始,在某种程度上,初始化值是字符,而它们应该被定义为字符串。 F2作为字符的定义,因此'F2',不代表文字字符串“F2”。

char *alphabet[] = {"A", "B2", "C", "D4", "E"};

Finally, in order to print strings rather than characters, the printf format identifier has to be changed from %c to %s.

最后,为了打印字符串而不是字符,printf格式标识符必须从%c更改为%s。

Summing up, you can print the elements of a string array as follows:

总结一下,您可以打印字符串数组的元素,如下所示:

char *alphabet[5] = {"A", "B2", "C", "D4", "E"};
for(int i = 0; i < 5; i++){
    printf("%s} ", alphabet[i]);
}

If you don't know the size of the array from the beginning, you can also write:

如果从一开始就不知道数组的大小,你也可以写:

char *alphabet[] = {"A", "B2", "C", "D4", "E"};
for(int i = 0; i < sizeof(alphabet)/sizeof(alphabet[0]); i++){
    printf("%s ", alphabet[i]);
}

Hope it helps!

希望能帮助到你!

#2


1  

char is used for single characters. If you want multiple characters you need to use strings.

char用于单个字符。如果你想要多个字符,你需要使用字符串。

char *strings[] = { "A", "B2", "C", "D4", "E"};

int remainder = my_number % 5;
printf("%s\n", strings[remainder]);

#3


-1  

As odin19 pointed out, B2 and D4 are not characters in C. In order to print them you can use double quotes in the elements of the array and print them as strings using %s in your printf.

正如odin19指出的那样,B2和D4不是C中的字符。为了打印它们,您可以在数组元素中使用双引号,并在printf中使用%s将它们打印为字符串。

#4


-1  

Single quotes are used to indicate a char. So 'B2' would be treated a char. Instead you want char*, and thus double quotes.

单引号用于表示char。因此'B2'将被视为char。相反,你想要char *,因此需要双引号。