473,408 Members | 2,832 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,408 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 2326

"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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.