473,382 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

output

o/p of this is nothing .. can u explain why.. i know its gonna
something to do with signed and unsigned..

#include<stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};

int main()
{
int d;
for(d=-1;d <=(TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);

return 0;
}

Apr 7 '06 #1
10 2318
Abhishek Parwal wrote:
o/p of this is nothing .. can u explain why.. i know its gonna
something to do with signed and unsigned..

#include<stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};

int main()
{
int d;
for(d=-1;d <=(TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);

return 0;
}


Please search the archives before posting, this specific question has
been discussed several time before:

http://groups.google.com/group/comp....f96e862940333b
http://groups.google.com/group/comp....1a62b1532e620d
http://groups.google.com/group/comp....18e466cd0ae5c5

Robert Gamble

Apr 7 '06 #2
Abhishek Parwal wrote:
o/p of this is nothing .. can u explain why.. i know its gonna
something to do with signed and unsigned..

#include<stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};

int main()
{
int d;
for(d=-1;d <=(TOTAL_ELEMENTS-2);d++) <<< sizeof is type size_t, not int for(d=-1;d <=(int) (TOTAL_ELEMENTS-2);d++) printf("%d\n",array[d+1]);

return 0;
}


So here, size_t appears to be unsigned thus 'd' gets promoted to
unsigned for the comparison and -1 is a very big positive number,
bit-wise. You could use a little indenting too. Also, array can be
defined within main().
--
- Mark

Apr 7 '06 #3
No, it has nothing to do with the unsigned or signed because int is by
default taken by the compiler as signed ( this however maybe
implementation dependent or compile dependent).
You can check this by prefixing the declaration with signed keyword.

The problem lies with the # define statement in you code which is an
expression. Thereby it cannot be compared in the for loop with the
variable d, resulting in no loop processing.

Apr 7 '06 #4
shishir wrote:
No, it has nothing to do with the unsigned or signed because int is by
default taken by the compiler as signed ( this however maybe
implementation dependent or compile dependent).
You can check this by prefixing the declaration with signed keyword.

The problem lies with the # define statement in you code which is an
expression. Thereby it cannot be compared in the for loop with the
variable d, resulting in no loop processing.


1. Please read <http://cfaj.freeshell.org/google/> before posting
another followup message.

2. You might consider some knowlege of C a prerequisite to answering
questions about C. Every single statement in your response is
incorrect:

a. It has everything to do with signed/unsigned
b. Whether int is signed or unsigned is not implementation dependent,
it is always signed
c. There is nothing wrong with the #define statement, your explanation
is nonsense.

Robert Gamble

Apr 7 '06 #5
shishir wrote:
No, it has nothing to do with the unsigned or signed because int is by
default taken by the compiler as signed ( this however maybe
implementation dependent or compile dependent).


Yes it does. Read my post and understand integer promotion.

--
- Mark

Apr 7 '06 #6
shishir wrote:

Learn to /quote/, for heaven's sake. Your message is incomprehensible
without its context. The OPs code was:

#include<stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};

int main()
{
int d;
for(d=-1;d <=(TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);

return 0;
}
No, it has nothing to do with the unsigned or signed
Wrong.
because int is by default taken by the compiler as signed
By /definition/, not default.
( this however maybe implementation dependent or compile dependent).
No. Plain `int` is signed.
You can check this by prefixing the declaration with signed keyword.

The problem lies with the # define statement in you code which is an
expression. Thereby it cannot be compared in the for loop with the
variable d,


Completely wrong. Why do you think expressions cannot be compared in
a for-loop?

--
Chris "not a Tuvela" Dollin
"To say that the human is thus and so is almost always to lie
automatically."
Apr 7 '06 #7
shishir wrote:

Please provide context when replying. See
http://cfaj.freeshell.org/google/ and
http://www.safalra.com/special/googlegroupsreply/ for details.
No, it has nothing to do with the unsigned or signed
Wrong. It has everything to do with signed and unsigned types.
because int is by
default taken by the compiler as signed
True.
( this however maybe
implementation dependent or compile dependent).
If you don't even know the basics, why are you offering advice? int is
signed by definition.
You can check this by prefixing the declaration with signed keyword.
That checks nothing.
The problem lies with the # define statement in you code which is an
expression.
Wrong. The #define
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
is a perfectly standard C technique.
Thereby it cannot be compared in the for loop with the
variable d, resulting in no loop processing.


Complete rubbish. You can use as complex an expression as you like.

This question has been correctly answered here many times in the past, I
suggest you read the other posts in this thread (if you see them) or
search the archive.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Apr 7 '06 #8
In article <11*********************@v46g2000cwv.googlegroups. com> "Robert Gamble" <rg*******@gmail.com> writes:
Abhishek Parwal wrote:
o/p of this is nothing .. can u explain why.. i know its gonna
something to do with signed and unsigned..

#include<stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};

int main()
{
int d;
for(d=-1;d <=(TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);

return 0;
}


Please search the archives before posting, this specific question has
been discussed several time before:

http://groups.google.com/group/comp....f96e862940333b
http://groups.google.com/group/comp....1a62b1532e620d
http://groups.google.com/group/comp....18e466cd0ae5c5


Interestingly, this is the fourth time this exact same program comes up
here. Is it a quiz question?
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Apr 7 '06 #9
Dik T. Winter wrote:
In article <11*********************@v46g2000cwv.googlegroups. com> "Robert Gamble" <rg*******@gmail.com> writes:
> Abhishek Parwal wrote:
> > o/p of this is nothing .. can u explain why.. i know its gonna
> > something to do with signed and unsigned..
> >
> > #include<stdio.h>
> >
> > #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
> > int array[] = {23,34,12,17,204,99,16};
> >
> > int main()
> > {
> > int d;
> > for(d=-1;d <=(TOTAL_ELEMENTS-2);d++)
> > printf("%d\n",array[d+1]);
> >
> > return 0;
> > }

>
> Please search the archives before posting, this specific question has
> been discussed several time before:
>
> http://groups.google.com/group/comp....f96e862940333b
> http://groups.google.com/group/comp....1a62b1532e620d
> http://groups.google.com/group/comp....18e466cd0ae5c5


Interestingly, this is the fourth time this exact same program comes up
here. Is it a quiz question?


http://www.gowrikumar.com/c/cquestions.html

Second question from the top.

Robert Gamble

Apr 7 '06 #10
Groovy hepcat Abhishek Parwal was jivin' on 7 Apr 2006 05:49:37 -0700
in comp.lang.c.
output's a cool scene! Dig it!
o/p of this is nothing .. can u explain why.. i know its gonna
something to do with signed and unsigned..
Right.
#include<stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
sizeof returns a size_t. The expression in this macro, therefore,
yields a size_t. Aritmetic on two size_t objects yields a size_t,
naturally. Now, size_t is an unsigned integral type.
int array[] = {23,34,12,17,204,99,16};

int main()
{
int d;
for(d=-1;d <=(TOTAL_ELEMENTS-2);d++)


When you campare two objects of differing types, they must be
converted to a common type, according to certain rules called "usual
arithmetic conversion". The result of comparing a size_t to an int is
size_t. The negative int is converted, therefore, to a positive
unsigned value greater than TOTAL_ELEMENTS, according to another set
of rules. The comparison always returns false, therefore.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Apr 9 '06 #11

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

Similar topics

4
by: Mark Wilson CPU | last post by:
This must be easy, but I'm missing something... I want to execute a Perl script, and capture ALL its output into a PHP variable. Here are my 2 files: -------------------------------------...
3
by: edgekaos | last post by:
Is method 2 valid? Method 1: wstring input = L"STRING"; wstring output = input; transform(output.begin(), output.end(), output.begin(), towupper); Method 2: wstring input = L"STRING";...
4
by: Kevin Mansel via .NET 247 | last post by:
Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that...
24
by: kalamantina | last post by:
#include "stdafx.h" #include <stdio.h> #define output( x ) printf( #x "\r\n" );fflush( stdout ) class CMyBase { public: CMyBase() { output( CMyBase() ); f(*this);
0
by: newbie | last post by:
i'm a newbie of c language. can anyone help me to implement the code so that I can get the ciphertext from the output. thanks. #ifndef _3DES_H #define _3DES_H #ifndef uint8 #define uint8 ...
32
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
3
by: MatsL | last post by:
Hi, This is seriously driving me crazy, could anyone explain to me why neither of these doesn't produce XHTML compliant output (it is being called in Render() btw): output.WriteLine("<img...
3
by: undshan | last post by:
I am writing a code that needs to open a file, create an output file, run through my function, prints the results to the output file, and closes them within a loop. Here is my code: #include...
5
by: amit.uttam | last post by:
Hey everyone, I've recently jumped big time into python and I'm working on a software program for testing automation. I had a question about proper logging of output. What I would like is: 1....
2
by: gabosom | last post by:
Hi! I've been breaking my head trying to get the output variables from my Stored Procedure. This is my SP code CREATE PROCEDURE GetKitchenOrderDetail( @idService int, --outPut Variables ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.