周期表的C编程 [英] C programming of periodic table
问题描述
我在使用此代码打印出数组中元素名称时遇到问题但是它无法正常工作
#include< stdio.h中>
#include< string.h>
int main()
{
int i;
char元素[20] = {氢,氦,锂,berrylium,硼,
碳,氮,氧,氟 ,氖,钠,镁,
铝,硅,磷,硫,氯,氩,
钾,钙};
for(i = 0; i< 20; i ++)
{
printf(%c\ n,(element [i]));
}
返回0;
}
你错过了一个明星(*
):
Quote:char element [20] = {Hydrogen,helium,lithium, berrylium,硼,
应该是
char * element [20] = {氢,氦,锂,berrylium ,硼,
您选择了错误的输出格式:
Quote:printf(%c\ n,(element [i]));
应该是
printf(%s \ n,(element [i]));
如果您将变量声明为
char 元素[ 20 ]
您正在分配一个包含20个元素的char类型数组。
如果你想分配一个字符串数组,你需要
char * element [] = { Hydrogen, helium,...};
< blockquote>尝试更改:
printf( %c \\\
,(element [i]));
要
printf( %s \ n,element [i]);
%c是单个字符,%s是空终止字符串
i have a problem printing out the names of element in an array i used this code but it's not working
#include <stdio.h> #include <string.h> int main() { int i; char element[20]= { "Hydrogen", "helium", "lithium", "berrylium", "boron", "carbon","Nitrogen","oxygen", "flourine","neon","sodium","magnesium", "aluminium","silicon","phosphorus","sulphur","chlorine","argon", "potassium","calcium"}; for(i=0; i<20; i++) { printf("%c\n", ( element[i])); } return 0; }
You missed a star (*
):
Quote:char element[20]= { "Hydrogen", "helium", "lithium", "berrylium", "boron",
Should be
char * element[20]= { "Hydrogen", "helium", "lithium", "berrylium", "boron",
You chose the wrong output format:
Quote:printf("%c\n", ( element[i]));
Should be
printf("%s\n", ( element[i]));
If you declare a variables as
char element[20]
You are allocating an array of char type with 20 elements.
If you want allocate a array of strings, you need
char* element[] = { "Hydrogen", "helium", ...};
Try changing:
printf("%c\n", ( element[i]));
To
printf("%s\n", element[i]);
"%c" is a single character, "%s" is a null terminated string
这篇关于周期表的C编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!