473,657 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2342

"Gautam" <ga********@yah oo.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********@yaho o.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.c om/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
2739
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), will be robust etc. As an example, I am developing a BSD Sockets suite which I want to run under various Unixes, including the Zaurus version of Linux, and also run parts of it under Windows. My thought is to
22
2251
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 detecting a key board hit so that it could invoke scanf(). So a portable key board data available routine would satisfy my minimum requirements. But more generally I want to know if one can write portable C code that allows a user to change the...
10
2180
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 include. What sites do people know about that are comprehensive in their differences? For example, MacOSX complained about <string.h>. With Solaris I needed
8
7066
by: suresh_C# | last post by:
Dear All, What is difference between Portable Executable (PE) file and a Assembly? Thanks, Mahesh
2
1489
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. Basically I need to know the best practice, or most reliable way of sending and receiving data on a serial connection. In VB6/EVB I used to use the RAPI commands for sending data, until the 2003
131
6108
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 portable C code - *not only* because, in a 'C sense', I
162
6664
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. http://www.developerdotstar.com/community/node/291 This "teacher of C" demonstrates his prowess with a masterful display of incompetence in a 200-line program that travels as swiftly as possible to Segfault City.
409
10972
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 objects that are more than 4GB long. The problem is, when you have in thousands of places
10
1807
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 programs to run. The problem is that as new software comes out, they seem to take up more and more space on the HD. I use most of the PortableApps.com programs from my usb with great success and ease.
23
3326
by: asit | last post by:
what is the difference between portable C, posix C and windows C ???
0
8399
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
8827
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
8732
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...
1
8504
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8606
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
6169
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
5632
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1959
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.