'Is printing array possible without any loop in the C Language?
For example, in Python, if we take a list as an array it directly prints the whole array for a single line of code. Is there any way, achieving the same thing in the C language?
Solution 1:[1]
Short answer
No
Short answer to almost ALL questions on the form "Can you do X as easy in C as Python?"
No
Long answer
It depends on what you mean. Internally, Python also work with loops. It's just hidden from you, and the same goes for all programming languages. If you want to process an array, you will in general need a loop at some level.
So if your question is if there are some predefined functions for printing arrays, the answer is no, provided that the array isn't a string. But it's not difficult to write a custom print function for this. Here is one example that prints it fairly pretty:
void print_int_array(int *arr, size_t size) {
if (size == 0) {
printf("[]");
} else {
putchar('[');
for (int i = 0; i < size - 1; i++)
printf("%d, ", arr[i]);
printf("%d]", arr[size - 1]);
}
}
It's also possible to use recursion, but recursion is basically just a loop in disguise. Here is a recursive version of the above:
void print_int_array_rec(int *arr, size_t size) {
if (size == 0) {
putchar(']');
} else {
printf("%d", *arr);
if (size == 1)
printf(", ");
print_int_array_rec(arr + 1, size - 1);
}
}
void print_int_array(int *arr, size_t size) {
putchar('[');
print_int_array_rec(arr, size);
}
But as I said, it's basically just a loop in disguise. At least in this simple case. All loops are typically converted to something like this in assembly:
loop_start:
// Code
if(<condition>) goto loop_start
Or with a nested loop:
loop_start:
// Code
loop2_start:
// Code
if(<condition>) goto loop2_start
// Code
if(<condition>) goto loop_start
With recursion, these jumps can become more complicated. If you're using complicated recursion the jumps can stop being properly nested. It could cause something like this:
loop_start:
// Code
loop2_start:
// Code
if(<condition>) goto loop_start
// Code
if(<condition>) goto loop2_start // Switched the gotos
Notice that I switched place on the gotos, so this "loop" is not properly nested. There is no inner and outer loop. This is what is called spaghetti code and the reason goto is frowned upon, because it makes the code very hard to follow. But it does not matter when it's assembly written by the compiler.
So does this snippet count as a loop, although a complicated one? TBH, I'm not sure. But what I DO know is that recursion and regular iteration are equally expressive which means that there is nothing you can compute recursively which you can not compute iteratively and vice versa. Read here for more information.
Some things become MUCH easier to write in a recursive style. For instance, the Ackermann function: How to rewrite Ackermann function in non-recursive style?
Solution 2:[2]
you can use recursion - no loops.
void printarray1(int *array, size_t size, size_t pos)
{
if(pos < size) {printarray1(array, size, pos + 1); printf("%d%s", array[size - pos - 1], pos ? " ": "");}
}
void printarray(int *array, size_t size)
{
printf("[");printarray1(array, size, 0);printf("]");
}
int main(void)
{
int array[] = {1,2,3,4,5,6,7,8,9,10};
printarray(array, sizeof(array)/sizeof(array[0]));
}
Solution 3:[3]
Ya, we can. If the array size is fixed, for example if the array's size is 6. Then you can print the values like printf(a[0]) to printf(a[5]). If you give the ''n'' value(here n is size of array) as input to user, then you don't know what value will the user give. So in that case you need loop to print the values in array.
Solution 4:[4]
Yes its possible to print an array without loop you have to just use goto statemenet to make a loop and if condition to check
my:
if(i<n)
{
scanf("%d",&arr[i]);
i++;
goto my;
}
else{
printf(" ");
}
j=0;
to:
if(j<n)
{
printf("%d ",arr[j]);
j++;
goto to;
}
else{
printf(" ");
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | 0___________ |
| Solution 3 | Harshan |
| Solution 4 | Sanjay Singh Negi |
