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 06-07-2005, 08:16 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Rep: Reputation: 45
permitted or not


Hi is this permitted in c code?
for (int i=0;......)
 
Old 06-07-2005, 09:09 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Yes, but the i variable only has scope inside the loop.
 
Old 06-07-2005, 09:20 AM   #3
junaid18183
Member
 
Registered: Mar 2005
Location: India
Distribution: RedHat 9.0 and EL
Posts: 31

Rep: Reputation: 15
Hi alaios you know the basic syntax of the for loop as

for (initialization;condition;increment)

though this is recommended that all these must be used ;all of parameter of the for loop can be ommited (but you must have a semicolon )

for simplicity I am giving you the sample code

1]
#include<stdio.h>

int main() {

int i;
for(i=0;i<10;i++) {

printf("the value of i is %d \n",i);
}
return(0);
}

/* The above Code use the standerd format of the for loop.

2]
#include<stdio.h>

int main() {

int i=0;
for(;i<10;i++) {

printf("the value of i is %d \n",i);
}
return(0);
}


/* The above Code has ommited the initilization note the i is initilized outside the for loop.


3]
#include<stdio.h>

int main() {

int i=0;
for(;i<10 {

printf("the value of i is %d \n",i);
i++;
}
return(0);
}


* The above Code has ommited the initilization as wel as increment of i, note the i is initilized outside the for loop and i is incremented in the loop itself.


The above all three programme are same and give same output as
the value of i is 0
the value of i is 1
the value of i is 2
the value of i is 3
the value of i is 4
the value of i is 5
the value of i is 6
the value of i is 7
the value of i is 8
the value of i is 9


Now look at the code which is just variant to the third one

4]

#include<stdio.h>

int main() {

int i=0;
for( ; ; ) {

printf("the value of i is %d \n",i);
i++;
//if (i>9)
//exit(0);

}
return(0);
}

The above code is fully valid and runs note that the syntax of for loop used.

the only difference is this for loop is contineous and to stop the program you have to use ctrl+C.

To overcome this you can test for the value of i inside the loop itself as I done using the if condition just uncommnet the 2 lines and the program provides the same output as the above 3 programmes.


**Note to run the programme just save on your directory with name say test.c
now compile with gcc as

gcc -o test test.c
and finlly on command promt just enter .

./test



So the moral of this story is for ( ; ; is fully functional but
Quote:
for (int i=0;......)
This will not

Regards Junaid
 
Old 06-07-2005, 09:25 AM   #4
junaid18183
Member
 
Registered: Mar 2005
Location: India
Distribution: RedHat 9.0 and EL
Posts: 31

Rep: Reputation: 15
Hi ,in the mean time I am replying you have posted the reply,

but the initialization of the i in the loop never supported by the c code but with C++ code.


alaios I think I have misunderstand your questtion on first sight didnot look at the int i inside the for sorry




thanks junaid.
 
  


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
Operation not permitted jakkals Linux - General 3 09-26-2005 07:30 PM
su - operation not permitted jrtayloriv Linux - Newbie 2 06-09-2005 09:27 PM
why is it not permitted? bvav22 Linux - Hardware 8 05-13-2005 07:19 PM
operation not permitted jamaso Slackware 2 05-08-2004 05:55 AM
Operation not permitted FrozenShadow23 Linux - Newbie 6 10-05-2003 05:13 PM

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

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