Click here to Skip to main content
15,886,052 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C++
#include<stdio.h>
#include<conio.h>
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.

[Edit by Jochen Arndt: Added code formatting]
Posted
Updated 3-Jun-15 21:44pm
v2
Comments
CPallini 4-Jun-15 4:24am    
I suggest you reading a tutorial on C arrays, see, for instance:
http://www.cplusplus.com/doc/tutorial/arrays/

If you want to print a string you must use the printf format character for strings which is '%s' and pass the pointer to the string:
C++
printf("%s",a);

You are printing a single character which is nor part of your string a:
C++
a[0] = 'l';
a[1] = 'k';
a[2] = 0; // The NULL termination character
//a[3] = ? Undefined because this is out of bound
 
Share this answer
 
Your char table has length of 3, and you are trying to access value outside the array boundary. The valid indexes are 0, 1, and 2, but because you are using char array the last value a[2] is used to store the end of string (NULL value).
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900