Different output in C

Why is the output different in C?

1
2
3
4
5
6
7
8
<#include
#include
void main()
{ clrscr();
char a[3]="lk";
printf("%c",a[3]);
getch();
}>


Instead of getting lk as output, I get the output as angstrom(A^0). I don't understand how I get this.Please help me. Thanks.
why are you trying to force a string into a char?
Why are you trying to print a value outside the bounds of your array? You have an array size of 3 so trying to access array[3] would be undefined, the largest allowable index is 2.

damn i missed that :)
I think you both missed the point. The OP es expecting "lk" as output, which means he should be outputting a as a C-string, not a char.

@OP: %c prints only a single character.
What I believe you want is to print a as a C-string.
 
  printf ("%s", a);
lk


BTW, main should always return an int.

Last edited on
Topic archived. No new replies allowed.