Click here to Skip to main content
11,414,561 members (66,561 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C programming
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
Edited 25-Jan-13 3:27am
v3
Comments
Sergey Alexandrovich Kryukov at 25-Jan-13 9:35am
   
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 at 25-Jan-13 12:59pm
   
+5.
CPallini at 25-Jan-13 13:07pm
   
Thank you.
H.Brydon at 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 at 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 at 25-Jan-13 22:21pm
   
Using "char [20][20]" is not as robust as "char*[20]". Go with the suggestions in solution #3.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,160
1 OriginalGriff 7,487
2 Maciej Los 3,710
3 Abhinav S 3,298
4 Peter Leow 3,084


Advertise | Privacy | Mobile
Web02 | 2.8.150427.4 | Last Updated 25 Jan 2013
Copyright © CodeProject, 1999-2015
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