473,566 Members | 3,307 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

for loop, two different types

Hello comp.lang.c

I had to write a for() loop with two temporary variables for it.
What I *usually* do is define the variables at the top so it works in
C89 as well.
But I realized that it just can't be done in the C99 'for' loop!

Assume I want two variables, int i and size_t size.

for(int i, size_t size; ...; ...)

is a syntax error

Is there a way to do this? I'm not talking about putting these two
types in a struct.

Assuming this can't be done, isn't the addition of defining variables
in the first part of the for loop in C99 useless, or at least
"incomplete "?
Jul 2 '08 #1
5 1560
vi******@gmail. com wrote:
Hello comp.lang.c

I had to write a for() loop with two temporary variables for it.
What I *usually* do is define the variables at the top so it works in
C89 as well.
But I realized that it just can't be done in the C99 'for' loop!

Assume I want two variables, int i and size_t size.

for(int i, size_t size; ...; ...)

is a syntax error

Is there a way to do this? I'm not talking about putting these two
types in a struct.

Assuming this can't be done, isn't the addition of defining variables
in the first part of the for loop in C99 useless, or at least
"incomplete "?
Well you can't do:

int i, size_t size;

anywhere else either.

Jul 2 '08 #2
vi******@gmail. com wrote:
Hello comp.lang.c

I had to write a for() loop with two temporary variables for it.
What I *usually* do is define the variables at the top so it works in
C89 as well.
But I realized that it just can't be done in the C99 'for' loop!

Assume I want two variables, int i and size_t size.

for(int i, size_t size; ...; ...)

is a syntax error

Is there a way to do this? I'm not talking about putting these two
types in a struct.
{
int i;
size_t size;
for (i=0, size=42; ...) {
... loop body ...
}
}
Assuming this can't be done, isn't the addition of defining variables
in the first part of the for loop in C99 useless,
Yes. It doesn't sing, dance, play the mandolin, or bring
me breakfast in bed. It doesn't do everything one could desire,
so off with its head!
or at least
"incomplete "?
<Shrug. Define "complete," and then we can all have fun
mocking you for the things you overlooked. ("That silly vippstar!
His so-called `complete for' doesn't permit function definitions
inside the increment portion. What a shortsighted moron, tee-hee!")

--
Er*********@sun .com
Jul 2 '08 #3
On Jul 2, 7:08 pm, Eric Sosman <Eric.Sos...@su n.comwrote:
vipps...@gmail. com wrote:
Assuming this can't be done, isn't the addition of defining variables
in the first part of the for loop in C99 useless,
or at least
"incomplete "?

<Shrug. Define "complete," and then we can all have fun
mocking you for the things you overlooked. ("That silly vippstar!
His so-called `complete for' doesn't permit function definitions
inside the increment portion. What a shortsighted moron, tee-hee!")
I guess I'm just looking for things I found in other languages that in
C wouldn't make sense :-)
(I'm talking about the common lisp 'LOOP' macro)

Regardless - this particular problem wasn't important to me, I was
just curious why such thing wasn't allowed and now I believe I
understand.
Jul 2 '08 #4
vi******@gmail. com wrote:
On Jul 2, 7:08 pm, Eric Sosman <Eric.Sos...@su n.comwrote:
>vipps...@gmail .com wrote:
Assuming this can't be done, isn't the addition of defining
variables in the first part of the for loop in C99 useless,
or at least
"incomplete "?

<Shrug. Define "complete," and then we can all have fun
mocking you for the things you overlooked. ("That silly vippstar!
His so-called `complete for' doesn't permit function definitions
inside the increment portion. What a shortsighted moron, tee-hee!")
I guess I'm just looking for things I found in other languages that in
C wouldn't make sense :-)
(I'm talking about the common lisp 'LOOP' macro)

Regardless - this particular problem wasn't important to me, I was
just curious why such thing wasn't allowed and now I believe I
understand.
I think C99 allows a single declaration in the initialisation part of
the for loop. What you wanted were multiple declarations, which would
require multiple statements, and that is a syntax error in a for loop.
It would have been nice if it were legal though - no need for ugly
blocks.

Jul 3 '08 #5
On Wed, 2 Jul 2008 08:16:33 -0700 (PDT), vi******@gmail. com posted:
Hello comp.lang.c

I had to write a for() loop with two temporary variables for it.
What I *usually* do is define the variables at the top so it works in
C89 as well.
But I realized that it just can't be done in the C99 'for' loop!

Assume I want two variables, int i and size_t size.

for(int i, size_t size; ...; ...)
What happens in the body of the loop with i and size such that your syntax
would be different than garden-variety nested loops?
--
Democracy is also a form of worship. It is the worship of Jackals by
Jackasses.
H. L. Mencken
Jul 5 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
3570
by: Bo | last post by:
When I had this code, a and b's value never increases. for( int i=0, double a=0.0, double b=0.0 ; i<100; a+=0.1, b+=0.2 ) { printf( "%s\n", i+a+b ); } This works, however: double a=0.0, b=0.0;
2
7798
by: Jim | last post by:
Im getting way too many rows retured..what its trying to do is insert a 0 for revenue for months 7 - 12 (aka July through December) for each of these cost centers for each payor type..Im getting a lot of repeats and the concatenation field date always comes back as January 2003 instead of the month and date its supposed to --Fiscal...
2
2178
by: Che | last post by:
Greetings, I am writing an application that uses an extendible XML file. in order to validate that XML I use a main XSD and in additional - few more extensions XSD's that uses the types in the main XSD as base types. my clients can define their own XSD's extensions and use my generic application to process their XML's. The problem:
5
5966
by: John Dumais | last post by:
Hello, I have been trying to figure out how to write an array of doubles (in this specific case) to a binary stream without using a loop. What I have been doing is... foreach(double d in TraceData) { instanceOfBinaryWriter.Write(d); }
17
4329
by: Allerdyce.John | last post by:
Hi, I am trying to compare the amount of work between using STL algorithm VS a plain Java loop. Let's say the class Rect has 2 attributes: area, and areaPerCent. In Java, I just write a plain for loop with a list: public static void calculateAreaPerCent(List rectList, float containerArea) {
22
26692
by: silversurfer2025 | last post by:
Hello everybdy, I am a little confused for the following reason: In my code I used a simple for-loop in order to initialize a 2D-array of floats to zero. Because of efficiency reasons, I changed it to use memset and I get totally different results.. How can this be? Here is the example: float gaborfilter;
8
2093
by: yuyang08 | last post by:
As we all known, each for-loop can be expressed in while statement. Can we also say that each while statement can be expressed in for-statment? Thanks! -Andy
29
5071
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is: 65,535. But i'm trying to write a program to test this, assuming I didn't know this number in advance. I came up with the following but have two...
10
58184
by: charlie.xia.fdu | last post by:
Hi C++ users, for(int i=0,int j=10 ; i<5&&j<10 ; i++ , j--) {} example from: http://www.tech-faq.com/iterations.shtml Is not valid in my eclipse cdt. Is there multiple initialization in C++? How can we use that? Thanks!
0
7673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.