LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-26-2016, 09:48 PM   #1
iceman81
Member
 
Registered: Jan 2013
Location: New Jersey
Distribution: CentOS, Arch
Posts: 181

Rep: Reputation: Disabled
C++ unexpected output


hey guys, doing some exercises after a section on scopes.

the question was

Is the following program legal? if so, what values are printed?

Code:
int i = 100, sum = 0;
for (int = 0; i != 0; ++i)
sum += i;
std::cout << i << " " << sum << std::endl;
so i fired up emacs and put it in...
Code:
#include <iostream>
int main()
{
  int i = 100, sum = 0;
  for (int i = 0; i <= 10; ++i)
    sum += i;
  std::cout << i <<  " " << sum << std::endl;
  return 0;
}
now i anticipated "int i = 0" to remove 100 and put 0.

Then i expected it to print

1 1
2 3
3 6
...
10 55 but i got 100 55. Now as im typing this i am facepalming, because i think the i=0 is only defined in the for loop, and the std::cout line is calling the block? scope variable?

is that right? man this stuff is tricky lol
 
Old 07-26-2016, 10:06 PM   #2
notKlaatu
Senior Member
 
Registered: Sep 2010
Location: Lawrence, New Zealand
Distribution: Slackware
Posts: 1,077

Rep: Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732
As you typed it, the 'for' loop ends with the semi-colon.

To see exactly what is going on, try this minor variation, using braces to define the for loop:

Code:
#include <iostream>
int main()
{
  int i = 100, sum = 0;
  for (int i = 0; i <= 10; ++i) {
    std::cout << "for i=" << i << '\n';
    std::cout << "for sum=" << sum << '\n';
    sum += i;
  }
  std::cout << "Final int i=" << i <<  " " << sum << std::endl;
  return 0;
}
Oh, and yes the 'int i' in the for loop exists only in the for loop.

Last edited by notKlaatu; 07-26-2016 at 10:10 PM. Reason: forgot to answer the question about the scope of the 'for' int
 
1 members found this post helpful.
Old 07-26-2016, 10:08 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
You did miss the 'i' in the first code block for the 'for' loop, but I assume that is a typo

Because your 'for' loop has not braces ({}) to scope the loop it is related that the 'for' loop has only a single line of code, which is the sum itself of 'sum'.
Therefore, std:cout is outside the loop and the program will only return a single line with the 'main' value of 'i' which is 100 as 'i' used for the addition to 'sum' is internal (scoped) to the loop.
 
1 members found this post helpful.
Old 07-26-2016, 10:20 PM   #4
iceman81
Member
 
Registered: Jan 2013
Location: New Jersey
Distribution: CentOS, Arch
Posts: 181

Original Poster
Rep: Reputation: Disabled
thanks for clarifying, totally missed the braces part of it. Didn't realize i was doing a one line statement.

the printout from that code is more what i was expecting Thanks again!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
unexpected output in perl doughyi8u Programming 4 12-26-2013 10:02 PM
Unexpected output from my program hida4all Linux - Newbie 1 09-08-2010 04:53 AM
[SOLVED] syslinux unexpected console output lryoung Linux - Kernel 1 07-24-2010 03:55 AM
Unexpected output from grep zer0x333 Linux - General 12 12-04-2007 10:42 AM
Unexpected output from 'ls' when using glob expressions psiakr3w Linux - General 7 07-22-2004 03:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration