Program failed to run as expected

Hello friends,
The below-mentioned code failed to, run as expected. It’s a simple program to find the length of an array. The program compiles fine, but not the expected output is given back. Any idea guys ??

#include <stdio.h>

int main() {
  char array[10];
  int i,j,len=0;
  for(i=0; array[j] != '\0'; i++) {
      scanf("%c", &array[i]);
     }
  for(j=0;  array[j]!='\0'; j++) {
     printf("%c",array[j]);
     len++;
     }
   printf("Length of array : %d\n ",len);
}

Here is the output i got:

Length of array : 0

Hi,
first of all you are using array[j] on the first for loop instead of array[i], though it would still not work. A clever way to determine the array size is to initialise the array with ‘\0’ and use a predefined max array size or use a dynamic array while keeping track of its size.

A quick fix for you code could be:

int main() {
  char array[10] = {'\0'};
  int i,j,len=0;

  // break if i reaches size or the user gives a '\n' //
  for(i=0; i < 10 && array[i] != '\n'; i++) {
      scanf("%c", &array[i]);
     }
  for(j=0;  array[j]!='\0'; j++) {
     printf("%c",array[j]);
     len++;
     }
  // no need to use len. you can use i directly //
   printf("Length of array : %d\n ",len);
}
4 Likes

Thanks for the replay @thelatchbuster.

That’s a careless mistake by me.

Apart from that, I struggle to understand your code. Especially the first for loop. What the use of the array[i] != ‘\n’; statement in the condition?

In your program, you are looking for something which is not at all there. You declared an array of size 10 and started to check if ‘\0’ is there, which apparently not be there. When you compile your code, the compiler will allocate memory for array[10] and initialize it with random garbage value. In your program, you are comparing ‘\0’ with these random garbage values.
So it’s better if you initialize the array first, then append ‘\0’ to it.

#include <stdio.h>
int main() {
  char array[10];
  int i,j,len=0;
  for(i=0;  i<= sizeof(array); i++) {
      scanf("%c", &array[i]);
     }
  array[i] = '\0';

  for(j=0;  array[j]!='\0'; j++) {
     printf("%c",array[j]);
     len++;
     }
   printf("Length of array : %d\n ",len);
}
2 Likes

Hi!!

The for loop won’t break in your case if the user gives a ‘\n’. In your code, the condition
array[i] != ‘\n’ is checked first and then you are passing ‘\n’ to array[i]. You are checking the condition before ‘\n’ have reached array[i]. The correct way to do that is i < 10 && array[i-1] != ‘\n’

for(i=0; i < 10 && array[i-1] != '\n'; i++) {
      scanf("%c", &array[i]);
     }

Yeah that’s right. Though as I said if you use i instead of len then you have the size of the array without needing to check. After all he is just using a buffer not a string.

Completely agree with you, but i -1 will give you the correct number since there is ‘\n’ in front of ‘\0’.

The whole condition i < 10 && array[i-1] != ‘\n’; means break the for loop when i is less than 10 and somebody hit the enter key. Imagine you have an array of size 10, after passing 5 characters you may hit enter key and all the five characters will be stored in the array hence exiting the for the loop.

See the final code:

#include<stdio.h>
int main() {
  char array[10] = {'\0'};
  int i,j,len=0;

  // break if i reaches size or the user gives a '\n' //
  for(i=0; i < 10 && array[i-1] != '\n'; i++) {
      scanf("%c",&array[i]);
     }
  for(j=0;  array[j]!='\n'; j++) {
     printf("%c",array[j]);
     }
  // no need to use len. you can use i directly //
   printf("Number of elements : %d\n ",j-1);   // Or i-1
}
3 Likes

Thank you so much, Guys. Now I get it, where I was wrong.