bdrmachine
Posts: 47
Joined: Fri Sep 20, 2013 8:52 pm

printf problem

Tue Nov 27, 2018 5:35 pm

I'm using netbeans to write code for my remotely connected Pi. Could someone please explain to me why printf works with this code:

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv) 
{    
    int x;
    
    //for(x=0;x>5;++x)
	
    for(;;)
    {    
    printf("hello c WORld");    
    }

    return (EXIT_SUCCESS);
}
Sending the "hello c World" message to the output window in netbeans.

BUT

This code executes without sending anything to the output window:

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv) 
{    
    int x;
    
    for(x=0;x>5;++x)
	
    //for(;;)
    {    
    printf("hello c WORld");    
    }

    return (EXIT_SUCCESS);
}
Last edited by bdrmachine on Tue Nov 27, 2018 6:10 pm, edited 1 time in total.

User avatar
davidcoton
Posts: 5322
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: printf problem

Tue Nov 27, 2018 5:40 pm

I can't see any difference between the two code blocks.
Please post code in [code]...[/code] tags so that white space is preserved.
Location: 345th cell on the right of the 210th row of L2 cache

User avatar
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: printf problem

Tue Nov 27, 2018 5:53 pm

Both are the same. Did you accidentally double copy the same file?
RPi = The best ARM based RISC OS computer around
More than 95% of posts made from RISC OS on RPi 1B/1B+ computers. Most of the rest from RISC OS on RPi 2B/3B/3B+ computers

ethanol100
Posts: 650
Joined: Wed Oct 02, 2013 12:28 pm

Re: printf problem

Tue Nov 27, 2018 6:34 pm

Code: Select all

for(x=0;x>5;++x)
is wrong.

You start with x=0 and it is never bigger than 5. This will just skip the printf block. Try < instead of the >.

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: printf problem

Tue Nov 27, 2018 6:35 pm

They are NOT the same. Look more carefully. (Unless, of course, OP has edited since the responders responded)

The problem is this:

for(x=0;x>5;++x)

It should be pretty obvious what's up with that...
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
PeterO
Posts: 5958
Joined: Sun Jul 22, 2012 4:14 pm

Re: printf problem

Tue Nov 27, 2018 6:54 pm

If it isn't obvious....

Code: Select all

for(x=0;x>5;++x)
Reads as "Starting with x set to 0, while x is greater than 5 increment x"

You want "less than" rather then "greater than"

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 27040
Joined: Sat Jul 30, 2011 7:41 pm

Re: printf problem

Tue Nov 27, 2018 7:12 pm

ethanol100 wins - he was first!
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: printf problem

Tue Nov 27, 2018 7:59 pm

jamesh wrote:
Tue Nov 27, 2018 7:12 pm
ethanol100 wins - he was first!
Actually, I started my post first - but took longer to compose and post. He slipped in.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

Return to “General discussion”