Click here to Skip to main content
15,885,910 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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;
}
Posted
Updated 25-Jan-13 2:27am
v3
Comments
Sergey Alexandrovich Kryukov 25-Jan-13 9:35am    
What you are doing has nothing to do with periodic table.
—SA

#include <stdio.h>
#include <string.h>
int main()

{
int i;
char element[20][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("%s\n", element[i]);
}
return 0;
}
 
Share this answer
 
Comments
Richard MacCutchan 22-Nov-15 8:09am    
Please do not post in old questions, it is most unlikely the OP still has any interest in this issue.
C++
#include <stdio.h>
#include <string.h>
int main()

{


int  i;
    char element[20][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("%s\n", ( element[i]));
}
getch();
return 0;
}
 
Share this answer
 
v2
Comments
H.Brydon 25-Jan-13 22:21pm    
Using "char [20][20]" is not as robust as "char*[20]". Go with the suggestions in solution #3.
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]));
 
Share this answer
 
Comments
fjdiewornncalwe 25-Jan-13 12:59pm    
+5.
CPallini 25-Jan-13 13:07pm    
Thank you.
H.Brydon 25-Jan-13 22:21pm    
+5 me too...
If you declare a variables as
C++
char element[20]

You are allocating an array of char type with 20 elements.

If you want allocate a array of strings, you need

C++
char* element[] = { "Hydrogen", "helium", ...};
 
Share this answer
 
Comments
fjdiewornncalwe 25-Jan-13 12:59pm    
+5.
Try changing:
C++
printf("%c\n", ( element[i]));

To
C++
printf("%s\n", element[i]);

"%c" is a single character, "%s" is a null terminated string
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900