Home > Backend Development > C++ > body text

Here are a few title options, keeping in mind the question structure and the article\'s focus: **Option 1 (Direct and focused):** * **Is Recursion in C Undefined Behavior?** **Option 2 (Emphasize

Susan Sarandon
Release: 2024-10-26 04:50:30
Original
881 people have browsed it

Here are a few title options, keeping in mind the question structure and the article's focus:

**Option 1 (Direct and focused):**

* **Is Recursion in C   Undefined Behavior?**

**Option 2 (Emphasizes the nuance):**

* **Recursion and Undefined Behavior

Recursion and Undefined Behavior in C

Infinite loops without side effects are explicitly considered undefined behavior (UB) in the C 11 standard. Consider the following program:

<code class="cpp">int main() {
   while (true) {}
}</code>
Copy after login

This program is UB because it does not perform any meaningful actions and can continue running indefinitely.

Is Recursion Also UB?

Now, let's consider a recursive program:

<code class="cpp">void foo() {
   foo();
}

int main() {
   foo();
}</code>
Copy after login

Is this program also UB?

Standard Citations

The standard states that (1.10p24):

The implementation may assume that any thread will eventually do one of the following:

  • terminate
  • make a call to a library I/O function
  • access or modify a volatile object, or
  • perform a synchronization operation or an atomic operation.

This applies to both the infinite loop and the recursion.

Despite this, the recursion can still result in undefined behavior if it exceeds the implementation limit of nested recursive function calls. This has always been the case.

The above is the detailed content of Here are a few title options, keeping in mind the question structure and the article\'s focus: **Option 1 (Direct and focused):** * **Is Recursion in C Undefined Behavior?** **Option 2 (Emphasize. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!