Home > Net >  Format specifier to print the whole integer array?
Format specifier to print the whole integer array?

Time:03-30

Printing character arrays (strings) can be done with very minimal effort of: printf("%s",array_name). So why not have something similar to print the integer datatype arrays?

If we have the code below:

int main()
{
  char C[10]={'a','b','c'};
  //to print the string here, we just do this thing below
  printf("%s",C);
}

We can print the contents in the whole character array just by using one printf statement, I found that cool after having run loops to do the same for integer data type arrays. So according to me, this printf("%s", C) would be interpreted as %s -> want to print a string, just passing the array name C , here decays to a pointer and the compiler somehow just goes to that location, reads one byte, dereferences it and sees that it (printf) hasn't found NULL yet, and again reads one byte, and so on. This idea sounded very cool to me and I got to thinking as to why do we not have something similar to print integer datatype arrays? Like some format specifier %hey and passing the array name would print the contents of the integer array? Just in this case, it would need to read 4 bytes and dereference it and move on until it finds some stop. To put in code, it would be like replacing this below code

int main()
{
  int A[10]={1,2,3};
  //to print this array, we do this below
  for(i=0;i<9;i  )
  {
    printf("%d ", *(A i));
  }
}

with this: printf("%hey",A) to print the whole integer array.

CodePudding user response:

The difference is that strings can not contain the embedded character '\0'. This character serves as a terminating character of a string.

On the other hand, other integer arrays can contain values equal to 0.

For example how are you going to output this integer array

int a[] = { 0, 1, 2, 3, 4, 5 };

That is opposite to character arrays that contain strings other kinds of integer arrays (including also character arrays that do not contain strings) do not have a common adopted sentinel value.

CodePudding user response:

C does not have a generic way to pass an array. Instead, when an array is passed, it is converted to the address of the array's first element, so to a ... function like printf(some_format, array_name), the array size is lost. Passing only an array to printf(), even if the format was extended, code would need to pass size info also.

// Not doable in C - array size info lost
printf("some_extened_format", array_name);

Easy enough to write a helper function, maybe as:

void printf_int_array(const char *int_fmt, const char *separator_fmt,
    size_t n, const int a[n]) {
  for (size_t i = 0; i < n; i  ) {
    if (i > 0) {
      fputs(separator_fmt, stdout);
    }
    printf(int_fmt, a[i]);
  } 
}
     

We could use a macro to help us call it simply.

#define PRINT_INT_ARRAY_STD(A) \
    printf_int_array("%d", " ", sizeof(A)/sizeof((A)[0]), A)

// Usage
PRINT_INT_ARRAY_STD(some_int_array);

Consider there are a vast number of formats that one may want to print an array of int.


Details:

"Printing character arrays (strings) can be done with very minimal effort of: printf("%s",array_name)" is imprecise as is is loose with character array and string.

char C[10] = {'a','b','c'};
printf("%s", C);

That code does not print the array C[]. Array C has 10 elements. and only "abc" is printed. Code prints the string, not the array.

Instead, "%s" gets a pointer to the beginning of the array and prints until null character is encountered.

If the array C lacked a null character, printf("%s", C); would have been undefined behavior.

Arrays, either as char, int, ... do not certainly have a "end" value. A string does.

  • Related