Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between byte and char in C

Tags:

c

I am wondering why I can't compile an example from book. I simplify the example here to avoid posting example from a copyrighted book.

#include <stdio.h>

BYTE *data = "data";

int main()
{
     printf("%s", data);
     return 0;
}

When compile with g++, i get error,

error: invalid conversion from 'const char*' to 'BYTE*'

The program works by simply replacing BYTE with char, but I must be doing something wrong since the example comes from a book.

Please help pointing out the problem. Thanks.

like image 683
idazuwaika Avatar asked Oct 20 '09 06:10

idazuwaika


People also ask

What is difference between byte and char?

The main difference between a byte and char data type is that byte is used to store raw binary data while other is used to store characters or text data. You can store character literals into a char variable e.g. char a = 'a'; A character literal is enclosed in single quotes.

Is a byte a char in C?

char is 1 byte in C because it is specified so in standards. The most probable logic is. the (binary) representation of a char (in standard character set) can fit into 1 byte.

Is a char 1 byte?

The char type takes 1 byte of memory (8 bits) and allows expressing in the binary notation 2^8=256 values. The char type can contain both positive and negative values. The range of values is from -128 to 127.

Can char be 2 bytes in C?

The 'char' data type in Java originally used for representing 16-bit Unicode. Therefore the size of the char data type in Java is 2 byte, and same for the C language is 1 byte.


1 Answers

BYTE isn't a part of the C language or C standard library so it is totally system dependent on whether it is defined after including just the standard stdio.h header file.

On many systems that do define a BYTE macro, it is often an unsigned char. Converting from a const char* to an unsigned char* would require an explicit cast.

like image 160
CB Bailey Avatar answered Sep 21 '22 19:09

CB Bailey