Thread: Nested "for" loops

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    Nested "for" loops

    Is there a limit on how many for loops I can nest or would it complicate the program?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    There's bound to be a limit but it's probably something totally ridiculous like 256 or more.

    Why would it complicate your program? You do what it takes to solve the problem.

  3. #3
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    at least 15 levels in C89 and at least 127 levels in C99. Most compilers will support any amount, until you are limited by your own resources, I'm guessing.

    Though if you've got that many nested for loops, you're probably doing something wrong.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Phenax View Post
    15 levels in C89 and 127 levels in C99

    Though if you've got that many nested for loops, you're probably doing something wrong.
    127 ... ok... noted for future... not that I'd even consider doing that.

  5. #5
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Quote Originally Posted by CommonTater View Post
    127 ... ok... noted for future... not that I'd even consider doing that.
    I would imagine some automatic code generators may hit that, if they are really bad or have a bug. But I couldn't imagine any human coding that.

    Also, I edited it for clarification: That's a minimum limit. So you're safe up to 127 levels, but after that, it's not guaranteed to work on all compilers. The "Transition limits" of C as is follows:

    Quote Originally Posted by Draft Section 5.2.4.1
    Implementations should avoid imposing fixed translation limits whenever possible.

    Both the translation and execution environments constrain the implementation of
    language translators and libraries. The following summarizes the language-related
    environmental limits on a conforming implementation; the library-related limits are
    discussed in clause 7.

    — 127 nesting levels of blocks
    — 63 nesting levels of conditional inclusion
    — 12 pointer, array, and function declarators (in any combinations) modifying an
    arithmetic, structure, union, or incomplete type in a declaration
    — 63 nesting levels of parenthesized declarators within a full declarator
    — 63 nesting levels of parenthesized expressions within a full expression
    — 63 significant initial characters in an internal identifier or a macro name (each
    universal character name or extended source character is considered a single
    character)
    — 31 significant initial characters in an external identifier (each universal character name
    specifying a short identifier of 0000FFFF or less is considered 6 characters, each
    universal character name specifying a short identifier of 00010000 or more is
    considered 10 characters, and each extended source character is considered the same
    number of characters as the corresponding universal character name, if any)14)
    — 4095 external identifiers in one translation unit
    — 511 identifiers with block scope declared in one block
    — 4095 macro identifiers simultaneously defined in one preprocessing translation unit
    — 127 parameters in one function definition
    — 127 arguments in one function call
    — 127 parameters in one macro definition
    — 127 arguments in one macro invocation
    — 4095 characters in a logical source line
    — 4095 characters in a character string literal or wide string literal (after concatenation)
    — 65535 bytes in an object (in a hosted environment only)
    — 15 nesting levels for #included files
    — 1023 case labels for a switch statement (excluding those for any nested switch
    statements)
    — 1023 members in a single structure or union
    — 1023 enumeration constants in a single enumeration
    — 63 levels of nested structure or union definitions in a single struct-declaration-list
    Last edited by Phenax; 04-07-2011 at 04:34 PM.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    Thumbs up

    Quote Originally Posted by CommonTater View Post
    ...
    Why would it complicate your program? You do what it takes to solve the problem.
    thanks! I was just looking at the programming homework I had and thought of something crazy. But I meant complicating the other user to read.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to exit from nested loops?
    By frktons in forum C Programming
    Replies: 40
    Last Post: 07-19-2010, 02:57 PM
  2. Nested For Loops
    By smitsky in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2004, 01:58 PM
  3. Evaluation of nested loops
    By Mister C in forum C Programming
    Replies: 2
    Last Post: 08-13-2004, 01:47 PM
  4. nested for loops and bank account interest help!!
    By webvigator2k in forum C++ Programming
    Replies: 1
    Last Post: 04-07-2003, 08:03 PM
  5. Nested for loops
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2002, 10:25 AM