Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

2 bytes (16 bits) datatype, anyone?

+11 votes
asked Jun 13 by Riccardo Dell'Oro (220 points)
I'm using OnlineGDB compiler to test routines for embedded micros, where usual datatypes are:

'int8_t' and 'uint8_t', AKA 'char' and 'unsigned char', 1 byte-wide...

'int16_t' and 'uint16_t', AKA 'int' and 'unsigned int', 2 byte-wide...

'int32_t' and 'uint32_t', AKA 'double' and 'unsigned double' (sometimes referred to as 'long' and 'unsigned long'...), 4 byte-wide...

Apparently, OnlineGDB in "C" mode only supports 1 byte-wide (as 'char/uchar') and 4 byte-wide (as 'int/uint') datatypes (probably because aimed at the PC world) so before creating my own structure-based datatype, I would like to know if I'm missing something...

Any help appreciated, thanks.

3 Answers

+1 vote
answered Jun 14 by Shawn Armstrong (310 points)
I think you might have some confusion related to memory allocation for data types.

The C standard does not specify how many bits a specific data type is; thus, it is essentially left up to the operating system.

A long on a Windows 64-bit system is 4 bytes.

A long on Unix / Linux 64-bit system is typically 8 bytes.

This is why we have cstdint.h; it standardizes the number of bits for a particular data type regardless of your operating system enabling applications to be portable between different operating systems.

OnlineGDB is a Linux based system.
0 votes
answered Sep 1 by Shruti Gossain (200 points)
int takes 2/4 bytes depending upon memory

float - 4 bytes

char - 1 byte

double - 8 bytes
0 votes
answered Sep 2 by Peter Minarik (63,140 points)

Here are your standard integral data types and how many bytes they take up on the OnlineGDB machine:

#include <stdio.h>

int main()
{
    printf("sizeof(char): %lu\n", sizeof(char)); // 1
    printf("sizeof(short int): %lu\n", sizeof(short int)); // 2
    printf("sizeof(int): %lu\n", sizeof(int)); // 4
    printf("sizeof(long int): %lu\n", sizeof(long int)); // 8
    printf("sizeof(long long int): %lu\n", sizeof(long long int)); // 8

    return 0;
}

As others have already mentioned, these sizes are specific for the OnlineGDB machine and you may have different results on Windows or on your microcontroller.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...