c - Looping through string - exit on null not zero -
i've written small program highlight problem i'm having in c. expected in following code, display loop print 0, exit on null; however, loop exits on 0. on earth missing / don't understand? appreciated.
#include <stdio.h> int main (void) { int = 0; int iarray[] = {1, 2, 0, 4, 5, '\0'}; int carray[] = {1, 2, 0, 4, 5, '\0'}; for(i=0; iarray[i] != '\0'; i++) { printf("\nelement: %d", iarray[i]); } for(i=0; carray[i] != '\0'; i++) { printf("\nelement: %d", iarray[i]); } return 0; } thanks, dan
you're missing quotes, seem want create string of digits. digit '1' not same character value 1.
also, arrays mysteriously int, when should char.
try:
const char iarray[] = { '1', '2', '0', '4', '5', '\0'}; which of course can simpler written as:
const char iarray[] = "12045";
Comments
Post a Comment