473,322 Members | 1,610 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,322 software developers and data experts.

Non Portable C

Can any one tell me what are the types in C which are non portable/which make C
non-portable

is it

a)structures, or
b)unions, or
3)bit-fields, or

are all of them , i am confused

thanx
Nov 14 '05 #1
3 2324

"Gautam" <ga********@yahoo.com> wrote in message
Can any one tell me what are the types in C which are non
portable/which make C non-portable

is it

a)structures, or
b)unions, or
3)bit-fields, or

are all of them , i am confused

C isn't binary compatible. For instance float x = 0.1f will produce
different bit patterns on machines with different floating-point
architectures, and since 0.1 cannot be represented exactly in most formsts
may be slightly under on some machines and slightly over on others.
Similarly int, longs, and even chars may have different numbers of bits, and
might not even have twos complement representation for negative numbers.
When you get to structures, unions and bitfields, not only may the
individual members differ, but the padding and layout in memory may differ.
This is because some machines like values to be aligned on word boundaries.

For practical purposes, what you need to know is that fwrite() ing a
structure to disk will produce an binary image that can be fread() only by a
program produced by the same compiler (the same goes of course for sending
bytes down a socket). You also need to know that C programs won't
necessarily produce the same bit-for-bit result, even though they will still
be correct, if you use floating point. If you use pointers incorrectly as
scalar values, or you exceed the minimum size of an integer type, you will
also get different results on different platforms.
Nov 14 '05 #2
On 9 May 2004 00:07:03 -0700, in comp.lang.c , ga********@yahoo.com
(Gautam) wrote:
Can any one tell me what are the types in C which are non portable/which make C
non-portable
a)structures, or
b)unions, or
3)bit-fields, or


None of these is nonportable, unless you start treating them badly. You
also need to be careful when writing them to file as binary data, and then
reading back on a different platform.

Structures can have padding. The amount and size of that padding is
system-dependent, so if you rely on the padding being X bytes wide then you
will have problems. If you save the struct as binary data, the padding
will have been saved with the data, so if you read it on a different
system, it will cause problems.

Objects can have different sizes on different platforms. If you rely on an
int being (say) 4 bytes, then you will have problems. If, in a union, you
rely on an int being the same size as a float, then problems. If you save
the data to a binary file, same problem as for structs.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #3
Gautam wrote:
Can any one tell me what are the types in C which are non portable/which make C
non-portable

is it

a)structures, or
b)unions, or
3)bit-fields, or

are all of them , i am confused


In its normal sense, portability is the ease with which you might
'port' a C program from one platform to another. The main purpose of
the C Standard is to define rules for compiler writers and
programmers so that this 'porting' of C programs more easily and
correctly.

There is nothing in the C Standard concerning the compatibility of
internal data structures or external file formats among platforms.
Nothing! You are on your own in this area.

Luckily we have ASCII (American Standard Code for Information
Interchange) text files common among most platforms. There is also
an ISO standard mirroring ASCII. EBCDIC (Extended Binary Coded
Decimal Interchange Code) is IBM's way of being one-up on ANSI in
the day. But I digress.

The various C implementations agree on what text representation of
values mean: '12345' a positive integer, '-23456' a negative one.
'1.25' is a floating point value. Virtually all C systems can read
these representations from a text file and come up with the same
internal values, endianness, width, etc. notwithstanding.

The text file is the answer. First, you can read it. If the
recipient of your data file doesn't already know its format, you can
explain it to her in the file itself and even include the C code to
interpret it.

Text rules! XML anyone?
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #4

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

Similar topics

13
by: James Harris | last post by:
Hi, Can someone recommend a book that will teach me how to approach C programming so that code is modularised, will compile for different environments (such as variations of Unix and Windows),...
22
by: SeeBelow | last post by:
Is there any way, in C, of interacting with a running program, but still using code that is portable, at least between linux and Windows? By "interacting" it could be something as simple as...
10
by: Jason Curl | last post by:
Dear C group, I'm very interested in writing portable C, but I only have GNU, Sparc and Cygwin to compile on. What I find is the biggest problem to writing portable C is what headers to...
8
by: suresh_C# | last post by:
Dear All, What is difference between Portable Executable (PE) file and a Assembly? Thanks, Mahesh
2
by: Tull Clancey | last post by:
Hi all. I'm nearing completion of a host app that needs to send data to, and receive data back from a portable, an HP device. The application running on the portable will be a .net application....
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
162
by: Richard Heathfield | last post by:
I found something interesting on the Web today, purely by chance. It would be funny if it weren't so sad. Or sad if it weren't so funny. I'm not sure which. ...
409
by: jacob navia | last post by:
I am trying to compile as much code in 64 bit mode as possible to test the 64 bit version of lcc-win. The problem appears now that size_t is now 64 bits. Fine. It has to be since there are...
10
by: bramnizzle | last post by:
I don't know if this is the right thread or not, but... In my endless pursuit to hold on to my dinosaur laptop (Dell 1100 Inspiron - circa 2004)...I want to keep as much space free for my...
23
by: asit | last post by:
what is the difference between portable C, posix C and windows C ???
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...

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.