473,804 Members | 3,750 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do i declare a byte variable?

hi,

I have a problem, a stupid problem. I can't declare a variable of type
byte.

The g++ said that i have syntactic error in this line. The code is
this: byte * variable;

well, i think that the mistake is a stupidity but i don't find it.
where is it?

Aug 10 '06 #1
20 225091
Manuel wrote:
hi,

I have a problem, a stupid problem. I can't declare a variable of type
byte.

The g++ said that i have syntactic error in this line. The code is
this: byte * variable;

well, i think that the mistake is a stupidity but i don't find it.
where is it?
Standard C++ doesn't have a built-in type called "byte." Just use a
char or, possibly, an unsigned char.

Try this:

char* variable1;
unsigned char* variable2;

Or perhaps you need to tell us more about what you intend to do with
the variable.

Best regards,

Tom

Aug 10 '06 #2
On 10 Aug 2006 13:57:01 -0700 in comp.lang.c++, "Manuel"
<mf*****@gmail. comwrote,
>I have a problem, a stupid problem. I can't declare a variable of type
byte.

The g++ said that i have syntactic error in this line. The code is
this: byte * variable;
A byte in C++ is char, or unsigned char, or signed char.

But your variable above would not be a byte an any case. It would
be a pointer. If you are asking what is a byte, you should probably
avoid bytes and pointers for now and use std::string etc..

Aug 10 '06 #3
ouch, iam stupid ;(

well, I thought about using the variable of type byte because I need to
store values from 0 to 255.
I think that i can use this:

Thomas Tutone wrote: typedef unsigned char byte;
So this way i can use byte.

Thank you for your help.

Bye.
Manuel Fernadez Campos
Manuel wrote:
hi,

I have a problem, a stupid problem. I can't declare a variable of type
byte.

The g++ said that i have syntactic error in this line. The code is
this: byte * variable;

well, i think that the mistake is a stupidity but i don't find it.
where is it?

Standard C++ doesn't have a built-in type called "byte." Just use a
char or, possibly, an unsigned char.

Try this:

char* variable1;
unsigned char* variable2;

Or perhaps you need to tell us more about what you intend to do with
the variable.

Best regards,

Tom
Aug 10 '06 #4
Manuel wrote:
ouch, iam stupid ;(

Please read the information below.


Brian

--
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.c om/c++-faq-lite/how-to-post.html>
Aug 10 '06 #5
Manuel posted:
well, I thought about using the variable of type byte because I need to
store values from 0 to 255.

C++ has a series of integer types, all of which come in a signed and
unsigned flavour:

char
short
int
long

The fundamental differences between them are:

(1) Their range:

rangeof(signed char) is a subset of rangeof(signed short),
which is a subset of rangeof(signed int), which is a subset of rangeof
(signed long).
rangeof(unsigne d char) is a subset of rangeof(unsigne d short),
which is a subset of rangeof(unsigne d int), which is a subset of rangeof
(unsigned long).

Note that this does not forbid them from all having the same range -- the
following could very well be true on a given system:

rangeof(signed char)==rangeof( short)==rangeof (int)==rangeof( long)

Note though that it's possible that the upper limit of "unsigned char" be
higher than the upper limit of "long".

(2) The amount of memory they occupy.

Put quite simply:
sizeof(signed char) <= sizeof(short) <= sizeof(int) <= sizeof(long)

(3) How efficiently the system deals with them (i.e. how fast it can work
with them)

The fastest type should be int, as it's supposed to be the "natural"
integer size for the system.

Now that we know a few differences between the integer types, we can make
an educated decision as to which to use for each purpose. Before we go any
further however, we should note that the Standard guarantees a minimum
range for each type:

(1) unsigned char : 0 through 255
(2) unsigned short : 0 through 65 535
(3) unsigned int : 0 through 65 535
(4) unsigned long : 0 through 4 294 967 295

(1) signed char: -127 through 127
(2) signed short: -32 767 through 32 767
(3) signed int: -32 767 through 32 767
(4) signed long: -2 147 483 647 through 2 147 483 647

The first thing to consider is whether we shall be storing negative
numbers; if so, we want a "signed" type. The first choice should be "signed
int" or "unsigned int":

int i = -3;

unsigned j = 5;

The next thing to consider is whether the number we store might fall
outside the guaranteed range of "signed int" or "unsigned int". If it does,
then we should "unsigned long" or "signed long":

long i = -70000;

long unsigned i = 70000U;

(If you're writing a positive integer literal which is larger than 65 535,
then append "U" to it.)

If the number is within range of "int" or "unsigned", then the last thing
to consider is whether memory is scarce. If we want to define an array of
87 thousand integers, we might consider using a "char" or a "short" -- it
will be less efficient, but it will use only half or a quarter of the
memory (depending on the system).

One last thing: If you don't specify the signedness of a type, then it
defaults to signed (i.e. "long" == "long signed"). However this isn't quite
true for "char". A plain char might be signed or unsigned, depending on the
system, so it isn't any use for doing arithmetic or storing numbers; it
should only be used for storing characters. If you want a signed char, then
make sure you specify "signed":

char unsigned a = 5; /* This is unsigned */

char signed b = -5; /* This is signed */

char c = 2; /* This could be either */

If you really want to become expert at working with integer types in C and
C++, then you might also want to learn about "integer promotion".

To answer your question though: If you want to store a number in the range
0 through 255, then just use "unsigned":

unsigned i = 255;

Only consider using "char" or "short" if memory is scarce, e.g.:

char unsigned array[99999999];

--

Frederick Gotham
Aug 10 '06 #6
Frederick Gotham posted:
(If you're writing a positive integer literal which is larger than 65
535, then append "U" to it.)

Actually, that's only necessary if the value is greater than 2 147 483 647.

The following is guaranteed to work fine on every system:

unsigned i = 2147483647;

The following might result in an ill-formed program on some systems (because
the signed integer literal might be out of range):

unsigned i = 2147483648;

Therefore, it's wise to append U to it:

unsigned i = 2147483648U;

--

Frederick Gotham
Aug 10 '06 #7

"Frederick Gotham" <fg*******@SPAM .comwrote in message news:2bOCg.1251 5
>
To answer your question though: If you want to store a number in the range
0 through 255, then just use "unsigned":

unsigned i = 255;

Only consider using "char" or "short" if memory is scarce, e.g.:

char unsigned array[99999999];
Why? I'd probably use "unsigned char", especially if I was porting Pascal
code or something. That's most likely what was meant by "byte", don't you
think?

-Howard

Aug 10 '06 #8
Howard posted:
>To answer your question though: If you want to store a number in the
range 0 through 255, then just use "unsigned":

unsigned i = 255;
Why? I'd probably use "unsigned char", especially if I was porting
Pascal
code or something. That's most likely what was meant by "byte", don't
you
think?

The extra efficiency of "unsigned int" is worth the cost of using up
another three bytes (or thereabouts).

Even if I were to write:

char unsigned i = 255;

, then it would still probably consume 4 bytes (or thereabouts) on most
systems.(If you have a system which can only access 4 bytes of memory at a
time, rather than one sole byte, then it would be efficient to store the
byte value in the least significant byte of the 4 bytes, and to just ignore
the values of the subsequent 3 bytes).

An easy test for this is the following:

#include <stdio.h>

int main(void)
{
struct FourBytes { char a,b,c,d; };

switch(sizeof(s truct FourBytes))
{
case sizeof(char[4]):
puts("Packed nice and tight!");
break;

case sizeof(int[4]):
puts("Aligned for efficient access.");
break;

default:

puts("Something funky's going on...");
}

return 0;
}

Notwithstanding any of this, your char is going to be promoted to int every
time you lay a finger on it.

--

Frederick Gotham
Aug 10 '06 #9
Frederick Gotham posted:

<snip C code>

Forgot I was posting to comp.lang.c++ (rather than comp.lang.c) -- I would
have written C++-style code if I had realised.

Nonetheless, the code is also valid C++ code (with deprecated features
albeit).

--

Frederick Gotham
Aug 10 '06 #10

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

Similar topics

1
8239
by: Wajih-ur-Rehman | last post by:
I am using the following query: query = "insert into SystemEventsProperties (ID,PName,PValue) values (@id,@pName,@pValue)"; OdbcParameter param1 = new OdbcParameter(); param1.DbType = DbType.Int32; param1.ParameterName = "@id"; param1.Value = maxId;
41
10681
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query = String.Format("SELECT * FROM dbo.documents WHERE ") & query End Sub
24
1624
by: Desmond Cassidy | last post by:
Hi, I don't know whether this is the correct place to ask this question..anyway here goes... I have a large solution with about 300 .vb files split into 9 projects VB.net Framework 1.1 WinXP Pro Whilst editing, as soon as I try and enter a statement like Private strName as string
1
2845
by: Pucca | last post by:
I have a string, "-1608" that I need to append to a byte variable. How can I convert the string to a byte so I can append it? thanks. -- Thanks.
15
3003
by: esha | last post by:
I need to have a Public variable in my project. In VB it can be declared in a standard module. Where can I do it in C# ? I tried to do it in default class Program.cs and I tried it in an added by me class. No success So, how and where I declare a variable visible by any module in the project? Esha
2
1325
by: NiteshR | last post by:
Hi, I want to declare a variable outside a loop. The variable then needs to change everytime it runs within the loop. example: DataRow name; //Once in the for loop, the variable name must change to
1
2787
by: ares.lagae | last post by:
- I have a typelist and I want to declare a member variable for each of the types. How can I do that? E.g. I have the typelist "typedef boost::mpl::vector<int, float> types;" and I want to declare member variables with type "int" and "float". - I have a typelist and I want to declare a variable based on each of the types types. How can I do that? E.g. I have the typelist "typedef boost::mpl::vector<int, float> types;" and I want to...
1
2875
by: adz1809 | last post by:
I'm having a problem with updating a record through a form. Here is the error: Server Error in '/AccoEndUser' Application. -------------------------------------------------------------------------------- Must declare the variable '@Title'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
1
1467
by: lokesh kumar | last post by:
where and how to declare global variable in vb2005 ? EggHeadCafe - .NET Developer Portal of Choice http://www.eggheadcafe.com
0
9706
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
9584
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
10583
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
10337
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
10082
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...
0
9160
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6854
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
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2995
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.