I am creating a simple c program and output of below program should be 2 but i am getting 50 dont know why (i am newbie to c) please let me know where i am missing
#include<stdio.h>
int main(int argc, char** argv) {
int a[4]={'1','2','2','\0'};
printf("The value of a is %d",a[1]);
return 0;
}
You initialised the array using ascii character codes.
'2'has integer value 50.Initialise the array as
if you want it to contain integers 1,2,2,0. Or
if you want an array of characters that can be treated as a string. (Note the use of the
%cformat specifier here.)