473,672 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printing array elements

Hi
It seems something really simple, but can't figure out.
It's not my homework :), was just looking at some C problems on the web
and came across this one.
why doesn't the following code print anything?

#include <stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {1,2,3,4,5,6,7} ;

int main()
{
int d;
for(d=-1;d <= (TOTAL_ELEMENTS - 2);d++)
printf("%d\n",a rray[d+1]);
return 0;
}

crazy

Nov 13 '05 #1
7 20028
"crazy" <ia*********@ho tmail.com> wrote in message
news:qCIeb.4778 56$cF.162833@rw crnsc53...
Hi
It seems something really simple, but can't figure out.
It's not my homework :), was just looking at some C problems on the web
and came across this one.
why doesn't the following code print anything?

#include <stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {1,2,3,4,5,6,7} ;

int main()
{
int d;
for(d=-1;d <= (TOTAL_ELEMENTS - 2);d++)
for(d=-1;d <= (int)(TOTAL_ELE MENTS - 2);d++)
printf("%d\n",a rray[d+1]);
return 0;
}


Read about 'signed', 'unsigned', and 'conversions'.

IMO it's poor practice to use negative offsets
with arrays like this. Or was this just a 'puzzle'
to be solved?

-Mike
Nov 13 '05 #2

"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:0m******** **********@news read3.news.pas. earthlink.net.. .
"crazy" <ia*********@ho tmail.com> wrote in message
news:qCIeb.4778 56$cF.162833@rw crnsc53...
Hi
It seems something really simple, but can't figure out.
It's not my homework :), was just looking at some C problems on the web
and came across this one.
why doesn't the following code print anything?

#include <stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {1,2,3,4,5,6,7} ;

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


for(d=-1;d <= (int)(TOTAL_ELE MENTS - 2);d++)
printf("%d\n",a rray[d+1]);
return 0;
}


Read about 'signed', 'unsigned', and 'conversions'.

IMO it's poor practice to use negative offsets
with arrays like this. Or was this just a 'puzzle'
to be solved?


Additonal hint: 'sizeof' returns an (impelemenation-
defined) unsigned type.

-Mike
Nov 13 '05 #3
In article <qCIeb.477856$c F.162833@rwcrns c53>, crazy wrote:
Hi
It seems something really simple, but can't figure out.
It's not my homework :), was just looking at some C problems on the web
and came across this one.
why doesn't the following code print anything?

#include <stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {1,2,3,4,5,6,7} ;

int main()
{
int d;
for(d=-1;d <= (TOTAL_ELEMENTS - 2);d++)
printf("%d\n",a rray[d+1]);
return 0;
}


Cast "(TOTAL_ELEMENT S - 2)" to int and all will be fine.

To explore further, run this:

#include <stdio.h>
#include <stddef.h>

int
main()
{
size_t i = 10; /* unsigned integer type */
int j = -1; /* signed integer type */

if (j < i) { /* what gets converted to what? */
printf("j < i\n"); /* will not print */
} else {
printf("j >= i\n"); /* will print */
}

return 0;
}

C99 says, "if the operand that has unsigned integer type has
rank greater or equal to the rank of the type of the other
operand, then the operand with signed integer type is converted
to the type of the operand with unsigned integer type."

So, size_t must have the same or greater rank than int. In my
smaller example, the -1 in j gets converted into an unsigned
integer type. Try printing -1 cast to size_t and see what it
is.

--
Andreas Kähäri
Nov 13 '05 #4
"Andreas Kahari" <ak*******@free shell.org> wrote in message
news:sl******** **************@ vinland.freeshe ll.org...
In article <qCIeb.477856$c F.162833@rwcrns c53>, crazy wrote:
----snipped----

Cast "(TOTAL_ELEMENT S - 2)" to int and all will be fine.

To explore further, run this:

#include <stdio.h>
#include <stddef.h>

int
main()
{
size_t i = 10; /* unsigned integer type */
int j = -1; /* signed integer type */

if (j < i) { /* what gets converted to what? */
printf("j < i\n"); /* will not print */
} else {
printf("j >= i\n"); /* will print */
}

return 0;
}

This example definitely helped understand what was wrong.

My mistake was printing d as %d and not as %u.
The moment I did that, it became very clear what was happening.

C99 says, "if the operand that has unsigned integer type has
rank greater or equal to the rank of the type of the other
operand, then the operand with signed integer type is converted
to the type of the operand with unsigned integer type."
What is meant by rank here? what is the rank precedence?

So, size_t must have the same or greater rank than int. In my
smaller example, the -1 in j gets converted into an unsigned
integer type. Try printing -1 cast to size_t and see what it
is.

--
Andreas Kähäri

-crazy

Nov 13 '05 #5
"crazy" <ia*********@ho tmail.com> wrote in message news:<qCIeb.477 856$cF.162833@r wcrnsc53>...
why doesn't the following code print anything?

#include <stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {1,2,3,4,5,6,7} ;

int main()
{
int d;
for(d=-1;d <= (TOTAL_ELEMENTS - 2);d++)
printf("%d\n",a rray[d+1]);
return 0;
}


Because size_t has equal or higher rank than an int on your machine.
Try experimenting with (-1 < 1u).

--
Peter
Nov 13 '05 #6
On Thu, 02 Oct 2003 00:02:58 GMT, "crazy" <ia*********@ho tmail.com>
wrote in comp.lang.c:
"Andreas Kahari" <ak*******@free shell.org> wrote in message
news:sl******** **************@ vinland.freeshe ll.org...
In article <qCIeb.477856$c F.162833@rwcrns c53>, crazy wrote:


----snipped----

Cast "(TOTAL_ELEMENT S - 2)" to int and all will be fine.

To explore further, run this:

#include <stdio.h>
#include <stddef.h>

int
main()
{
size_t i = 10; /* unsigned integer type */
int j = -1; /* signed integer type */

if (j < i) { /* what gets converted to what? */
printf("j < i\n"); /* will not print */
} else {
printf("j >= i\n"); /* will print */
}

return 0;
}


This example definitely helped understand what was wrong.

My mistake was printing d as %d and not as %u.
The moment I did that, it became very clear what was happening.

C99 says, "if the operand that has unsigned integer type has
rank greater or equal to the rank of the type of the other
operand, then the operand with signed integer type is converted
to the type of the operand with unsigned integer type."


What is meant by rank here? what is the rank precedence?

So, size_t must have the same or greater rank than int. In my
smaller example, the -1 in j gets converted into an unsigned
integer type. Try printing -1 cast to size_t and see what it
is.

--
Andreas Kähäri

-crazy


The integer types have a ranking in C, such that each higher-ranked
integer type must be able to contain at least the range of values of
lesser ranked types, if not more. This is also subject to the minimum
allowable absolute range for each type.

Signed or unsigned short int must be able to hold all the valuer of
signed and unsigned char, respectively.

(un)signed int must be able to hold all the values of (un)signed
short.

(un)signed long must be able to hold all the values of (un)signed int.

(un)signed long long must be able to hold all the values of (un)signed
long.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #7
In <SW************ ********@rwcrns c51.ops.asp.att .net> "crazy" <ia*********@ho tmail.com> writes:
My mistake was printing d as %d and not as %u.
Nope, that was NOT your mistake: d has type int, therefore it CANNOT be
printed with %u, which expects an unsigned int!
The moment I did that, it became very clear what was happening.


To do that properly, you must use (unsigned)d.
C99 says, "if the operand that has unsigned integer type has
rank greater or equal to the rank of the type of the other
operand, then the operand with signed integer type is converted
to the type of the operand with unsigned integer type."


What is meant by rank here? what is the rank precedence?


At your current level, you can safely ignore the C99 lingo. Simply
read about the arithmetic conversions in your favourite C book.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #8

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

Similar topics

2
16323
by: juliadream | last post by:
Can anyone tell my why print @array produces output different from print "@array" The second one embeds spaces between the items; the first one does not.
5
2216
by: Mark Preston | last post by:
Admission first - I don't actually have a problem here but have noticed that a lot of people have been asking similar questions and getting very varied answers. What I've done is to sort of "compile the questions" into a theoretical problem to see what people think should be done to solve it. Maybe it will be a worthwhile discussion, but more importantly maybe it will find out the very best way to sort this kind of problem out so that...
42
5913
by: junky_fellow | last post by:
Consider an implementation that doesn't use all bits 0 to represent a NULL pointer. Let the NULL pointer is represented by 0x12345678. On such an implementation, if the value of NULL pointer is printed will it be all 0's or 0x12345678 int main(void) { char *ptr; ptr = 0;
4
9262
by: Arif | last post by:
I C# code prints very slow as compared to a third party barcode printing software. That software prints approximately 10 labels in 2 seconds while my C# code prints 10 labels in 5 to 6 seconds. And this differences increases with the increase number of labels. The code is as follwods: Here rdr = OleDbDataReader Font is Times New Roman, 12pt
5
1424
by: Lloyd Dupont | last post by:
I'm trying to print some of my document. The problem is, the rendering of the document is not done through GDI+ at all but through plain old GDI, and I need it like that as I am using Uniscribe to layout text. Anyway, when printing the text is completely out of place, I wonder if it's due to incorrrect Dpi settings? (or something else?) I will investigate that..... Anyway if any of you had similar experience and could shed some light?...
1
1240
by: appearinggalaxy | last post by:
Hi, I am trying to churn out some statistical data from database and print it out by using printdocument and printdialog, but I realised that the alignment of the data is gone, for instance, I use "vbTab", but seems like it does not take effect on preview screen. I am vex on this issue and would like to ask for the advice from anyone who knows how to develop a good reporting module by using .net printdocument and printdialog.
6
4084
by: Siv | last post by:
Hi, I am getting into printing with VB.NET 2005 and want to implement the usual capability that a user can select a selection of pages. I have a report that is generated by my application that if the user wants all pages will produce 3 pages. I want to offer the user the ability to select via the print dialog that only pages 1 and 2 of it are printed or possibly pages 1 and 3 but not 2. At the moment I can produce all three pages...
8
17739
by: Steve Macleod | last post by:
Hi, I was wondering if anyone had a solution for printing HTML elements (especially style elements). I do not wish to make any changes to the page, other than in the <css media="print"block. I can use: select { border:1px solid #FFFFFF; } to get rid of the border, but dont know a way to get rid of the arrow
2
1443
by: Latina | last post by:
Hi, Can some one help to figure out why is only printing '{ }' and not the values the user is entering? Here is part of my code: void IntegerSet::setString() { cout<<"{"; for(element=0; element<26; element++) { if(set==true)
0
8498
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8418
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8940
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8840
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8694
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6249
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4433
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2830
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2083
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.