Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6343813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:31:09+00:00 2026-05-24T20:31:09+00:00

Consider the two following programs: program one int main() { printf( hello\n ); }

  • 0

Consider the two following programs:

program one

int main()
{
   printf( "hello\n" );
}

program two

int main()
{
   srand( 0 );
   if( rand() ) {
      printf( "hello\n" );
   } else {
      printf( "hello\n" );
   }
}

Do they have the same observable behavior or not? According to C++ Standard (1.9/6) the observable behavior includes:

  • reads and writes to volatile data
  • library I/O functions

Now srand() and rand() are likely not I/O functions (although I have no idea whether a given implementation uses some hardware noise source), but they modify program internal state. Do they manipulate volatile data? I don’t know. Calls to printf() are clearly I/O operations and sequences thereof are identical in both programs.

Do the two programs above have the same observable behavior? How do I know if two given programs have the same observable behavior?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:31:11+00:00Added an answer on May 24, 2026 at 8:31 pm

    Do the two programs above have the same observable behavior?

    As you say, that depends on whether srand() and rand() have observable side effects. They shouldn’t; there can be no external noise source, since the sequence is required to be repeatable for a given seed, and there is no other reason to perform I/O or access volatile data.

    If the compiler is able to determine that they don’t (e.g. if they are defined inline in a header, or the linker is smart enough to perform extra optimisations), then it would be able to omit them; otherwise, it must assume that they do, and include them. In many implementations, these functions will be in a precompiled library, and the linker won’t be that smart, so you will end up calling the functions; however, a decent compiler should notice that both branches of the if are identical and omit the test.

    (UPDATE: as noted in the comments, the call to rand() can also only be omitted if the compiler can determine that no future observable behaviour can depend on its side effects).

    How do I know if two given programs have the same observable behavior?

    In general, that’s a very difficult problem, and there will be some programs where it’s impossible to tell (for reasons similar to the Halting Problem). For programs as simple as this, you can simply list the observable operations and compare them; if the behaviour depends on the program’s input in a non-trivial way, then that soon gets very difficult to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following two classes (one is a Main with main() method): The VO
Please consider the following fork() / SIGCHLD pseudo-code. // main program excerpt for (;;)
Consider the following two statements out of a very simple assembly language program: DATA1
Consider the following two ways of writing a loop in Java to see if
Consider the following two alternatives: console.log("double"); console.log('single'); The former uses double quotes around the
Consider two web pages with the following in their body respectively: <body> <script> document.writeln('<textarea></textarea>')
Consider the following example. It consists of two header files, declaring two different namespaces:
Consider the following code public static void method(String[] srgs){ try{ }catch(){ System.out.println(Hello World +
Let's consider the two following lines in C# (using framework .NET 3.5) Regex regex
Consider the following program #include <iostream> #include<cstdlib> using namespace std; class E { public:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.