Click here to Skip to main content
12,690,140 members (28,091 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C
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 25-Jan-13 3:12am
greato7182
Updated 25-Jan-13 3:27am
v3
Comments
   
What you are doing has nothing to do with periodic table.
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

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]));
  Permalink  
Comments
Marcus Kramer 25-Jan-13 12:59pm
   
+5.
CPallini 25-Jan-13 13:07pm
   
Thank you.
H.Brydon 25-Jan-13 22:21pm
   
+5 me too...
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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", ...};
  Permalink  
Comments
Marcus Kramer 25-Jan-13 12:59pm
   
+5.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Try changing:
printf("%c\n", ( element[i]));

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

"%c" is a single character, "%s" is a null terminated string
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

#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;
}
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

#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;
}
  Permalink  
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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170117.1 | Last Updated 22 Nov 2015
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100