Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why we use char array to create a buffer [duplicate]

Tags:

c

Why we use a char array to create a buffer in the memory instead of a int array and if int array can be used to create the buffer,how to get the output from it ?

int main()
{
    char  buffer[100];
    fread(buffer,sizeof(int),4,stdin);
    int i=0;
    while(i<4)
    {
        printf ("%d,\n",buffer[i]);
        i=i+1;
    }    
}
like image 598
Shivam Dhabhai Avatar asked Jul 15 '26 05:07

Shivam Dhabhai


2 Answers

Very simply: we use a char array if we want to read bytes.

Either text strings, or (more generally), binary objects.

A "byte" is usually 1/4 the size of an "int".

Moreover, socket "read" and "write" counts are byte counts. By longstanding convention, they expect byte buffers.

like image 160
paulsm4 Avatar answered Jul 18 '26 21:07

paulsm4


Because char in the C language is exactly 1 byte. So it's logical to write in the file byte by byte.

like image 25
boomz Avatar answered Jul 18 '26 21:07

boomz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!