Level of Nested for loop in c+

I have written a c program with 256 nested for loop. But, I have heard that it is not possible to run this program in C. But, I have also heard that we can run a C++ program with 256 nested for loop. I have converted this C Program in to C++ by adding iostream header and I have changed printf to cout statement.

After adding suitable c header files, Can I able to use string functions: strlen, strcat; file functions fopen, fclose, fgets; memory functions: free and malloc in same way as we use in C.
Why do you need that many levels of nesting? What are you trying to do?
Any series of nested loops can be converted into a recursive function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#define N 256

void useful_function(int variables[N]);

void nesting(int current_level, int variables[N], int limits[N]){
    if (!current_level){
        useful_function(variables);
        return;
    }

    int *i = variables + current_level - 1;
    int n = limits[current_level - 1];
    for (*i = 0; *i < n; ++*i){
        nesting(current_level - 1, variables, limits);
    }
}

int main(){
    int variables[N];
    int limits[N];
    //Note: limits[0] is the limit for the deepest level of nesting.
    limits[0] = 10;
    limits[1] = 42;
    //etc.
    nesting(N, variables, limits);
    return 0;
}


As far as I know, there's nothing in C or C++ that forbids nesting loops arbitrarily deep (until you run out of stack space for all the variables). And yes, you can use standard C functions from C++.
Last edited on
> I have written a c program with 256 nested for loop.

This is fundamentally wrong, unless you were doing it for purposes of testing the implementation of a compiler. Consider using a different algorithm (an elegant option would be to write it recursively).


> I have also heard that we can run a C++ program with 256 nested for loop.

The standard does not guarantee it.

Nesting levels of compound statements, iteration control structures, and selection control structures [256].

The bracketed number following each quantity is recommended as the minimum for that quantity. However, these quantities are only guidelines and do not determine compliance.



> I have heard that it is not possible to run this program in C.

You may be able to run such a program in C. Unlike C++, C does impose minimum requirements on implementations; IIRC, C99 requires that an implementation shall be able to translate a program with 127 nesting levels of blocks. This is the lower limit; an implementation may allow deeper levels of nesting, and IIRC the C99 standard suggests that if possible, the implementation should not impose fixed limits.


> After adding suitable c header files, Can I able to use string functions: strlen, strcat;
> file functions fopen, fclose, fgets; memory functions: free and malloc in same way as we use in C.

Yes, C compatibility headers are available. Almost in the same way. For example:
1
2
int* p = malloc( 100 * sizof(int) ) ;
double* q = p ;

is valid C, but not valid C++.

Ideally include the standard C++ headers <cstring>, <cstdio> etc. and then use std::strlen, std::fopen etc.

For details, see: http://en.cppreference.com/w/cpp/header
Topic archived. No new replies allowed.