You are here:

C/help plz

Advertisement


Question
hi
i have a question about the work of getchar()
this is the code that i write but in the printing of the word it prints some signs that i dont know what they are

#include<stdio.h>
void main(){
int i=0;
char c,word[10];
while(c!='
'){
c=getchar();
word[i]=c;
i++;
}
printf("%s
",word);
}

can you help me that what should i do?or give me the right code?

bita

Answer
Hai Dear Bita !

 Your code having simple issue , ie you failed to terminate the string using the null character that's why your program displaying some garbage value !!

#include<stdio.h>

void main()
{
  int i = 0;

  char ch , word[80];
  while( (ch = getchar()) != '\n')
     word[i++] = ch;
  word[i] = '\0'; /* Terminating the string using null value */
  printf(" The string is :");
  puts(word);
}


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.