Character in C and Java -


i have been trying 1 thing in c :

int main(){     char ch;    printf("enter character");    scanf(" %c",&ch);    printf("the entered character %c\n",ch);     return 0; } 

everything seems fine if put 2 characters @ input takes out first character. example if enter "as" give me "a" in ch. why , can tell me want give error if enter more 1 character. how can that? thank in advance.

and if possible same thing want in java.

as far c concerned: please modify code in following way, compile , run it, , you'll see self-explanatory answer: no matter how many characters type after 1 another, accumulated in stdin buffer, first character gets read program @ time (or @ pass), because of %c format specifier , target variable ch.

#include <stdio.h> int main(){    char ch;    printf("eneter character:\n");    while(scanf(" %c",&ch) != eof){       printf("the entered character %c\n",ch);    }    return 0; } 

to end program execution should enter eof (control-d).


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -