LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 11-11-2014, 03:51 PM   #1
moisespedro
Senior Member
 
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223

Rep: Reputation: 195Reputation: 195
Simple C program, can't understand the output.


Hi, I am reading a C book and it asked the reader to print the following output:
Code:
1
12
123
.
.
.
12345678910
Since it is an example, the book has the code ready:

Code:
#include <stdio.h>
main() {
        int i, j;

        for(i=1;i<=10;i++) {
                for(j=1;j<=i;j++)
                        printf("%d",j);
                putchar('\n');
        }
}
It works, yes indeed but I didn't understand how. I mean, in my head it would print j once, sum 1 to j, check if it is less than i and then print it again.

In my head the output would be:
Code:
1
2
3
4
5
6
7
8
9
10
What am I missing?
 
Old 11-11-2014, 03:56 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Why do you think it would just print j once? j is inside a loop from 1 to i.

So when i is 1, j will loop from 1 to 1, and print 1
Then it will print a new line
Then when i is 2, j will loop from 1 to 2, and print 12
Then it will print a new line
And so on through 10.

Maybe what you're missing is the fact that in C, when you have a for loop or if statement, if there are no {}, it will include everything up to the next ; as part of the block.

So:
Code:
for(j=1;j<=i;j++)
   printf("%d",j);
Code:
for(j=1;j<=i;j++) printf("%d",j);
Code:
for(j=1;j<=i;j++) {
   printf("%d",j);
}
are all equivalent.

Last edited by suicidaleggroll; 11-11-2014 at 03:59 PM.
 
1 members found this post helpful.
Old 11-11-2014, 04:02 PM   #3
moisespedro
Senior Member
 
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223

Original Poster
Rep: Reputation: 195Reputation: 195
Oh, thanks. Now I've got it.
 
  


Reply



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
Not able to understand the output of w command pinga123 Linux - Newbie 3 08-12-2011 01:10 AM
[SOLVED] Unable to understand ps output. pinga123 Linux - Newbie 6 05-03-2011 11:20 AM
need help to understand tcpdump output tkmsr Linux - Networking 3 03-07-2011 01:47 AM
[SOLVED] how to understand the following output of traceroute jcwkyl Linux - Networking 3 07-12-2010 03:33 AM
help me to understand the output of chkrootkit ddaas Linux - Security 2 04-19-2005 02:28 AM

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

All times are GMT -5. The time now is 08:09 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