You are here:

C/Printing Strings !

Advertisement


Question
Hi,

How are you?
I have a 2D character string that I declare as:

char mytable[3][4]={"how","are","you"};

Why cannot I print out individual words from that?
I want the program to show the first word but

printf("%s", mytable[1]);//won't work
printf("%s", mytable[1][1]);//won't work

How do you print out individual words?
Thanks  

Answer
HI dear Eric !

Nice to meet you back after a long gap !

I didnt find any bug with your code , perhaps a small Grammatical issue  grammar in the sense compatible with C langage , ie array index starts from 0 instead of 1 , Sure your programme genarate the output what you have expected if you used index as 0 instead of 1 ,

Presenting you a simple code , hope it would help you out understanding  the grammar well !

#include<stdio.h>
void main()
{
  char array[3][4] = {"how" , "are" , "you"};
  int i;

  for( i = 0 ; i < 3 ; i++)
     printf("%s " , array[i]);



}

Output :
how are you

If you need more assistance regarding arrays and strings , please get back to me !

Thanks and Regards !
Prince M. Premnath

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise


I'm sure that I can solve any doubts in Turbo C ,Graphics Programing ,Mouse, Hardware Programming ,File System ,Interrupts, BIOS handling , TSR Programming , General Concepts in C Language, handling inline Assembly statements

Experience

Research over 6+ Years

Organizations
CG-VAK Softwares and Exports Limited

Education/Credentials
Masters in Computer Applications

©2012 About.com, a part of The New York Times Company. All rights reserved.