1 votes 1 votes int main() { int a[30]; printf("enter string:"); scanf("%s",a); printf("%c\n",a[2]); } take a string "123456" tell o/p Registered user 7 asked Jul 25, 2016 Registered user 7 443 views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
2 votes 2 votes In this program u r entering 123456 as input. But scanf is not storing the input value So, output of printf will be empty srestha answered Jul 25, 2016 srestha comment Share Follow See all 3 Comments See all 3 3 Comments reply Registered user 7 commented Jul 25, 2016 reply Follow Share instead of a[2] give a[0] now tel what is answer? 0 votes 0 votes vijaycs commented Jul 25, 2016 i edited by vijaycs Jul 25, 2016 reply Follow Share int a[30]; If size of int=4byte and char = 1 byte. Address allocated for variable a = 30*4= 120 byte. scanf("%s",a); Input string = "123456"; If starting address of a = 100; then value at address 100 = 1 value at address 101 = 2 value at address 102 = 3 value at address 103 = 4 value at address 104 = 5 value at address 105 = 6. value at address 106 = '\0' printf("%c\n",a[2]); Since data type stored in array is of integer type, but value printed should be only character a[0] = Value at address [ 100, 101, 102, 103] but, printf("%c\n",a[0]) will print only value at address 100.; a[1] = Value at address [ 104, 105, 106, 107], but printf("%c\n",a[1]) will print only value at address 104.; a[2] = Value at address [ 108, 109, 110, 111]. but printf("%c\n",a[2]) will print only value at address 108.; So ans should be garbage value or zero (if default value stored in array is zero). 6 votes 6 votes Kapil commented Jul 25, 2016 reply Follow Share nice explaination.... 1 votes 1 votes Please log in or register to add a comment.
0 votes 0 votes please make me correct , if i am wrong 3 rd statement , scanf, compilation problem because format specifier of string (%s) except only char values , but here no char pointer is defined , Learner_jai answered Jul 27, 2016 Learner_jai comment Share Follow See 1 comment See all 1 1 comment reply Arjun commented Jul 27, 2016 reply Follow Share No, format specifier won't cause a compilation error. Because this is inside a string and requires processing the string which happens at runtime. Some compilers just warns if it finds that the formats are not matching. 0 votes 0 votes Please log in or register to add a comment.