Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3980058
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:14:44+00:00 2026-05-20T05:14:44+00:00

I have a bit of c code that I’ve been working on in Xcode

  • 0

I have a bit of c code that I’ve been working on in Xcode on my mac. I then wanted to work with it on a Windows machine and compile it with TinyC. When I run it, the output is different.

Is it possible that this is due to using different compilers?

Thanks!


EDIT 1

The code is a simple script that opens up a wav file to throw all the samples into an array.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>



void read_wav_header(unsigned int *samp_rate, unsigned int *bits_per_samp,
                 unsigned int *num_samp);
void read_wav_data(int *data, unsigned int samp_rate,
               unsigned int bits_per_samp, unsigned int num_samp);
int conv_bit_size(unsigned int in, int bps);



int main(void)
// int read_wav(void)
{


unsigned int samp_rate, bits_per_samp, num_samp;
read_wav_header(&samp_rate, &bits_per_samp, &num_samp);

printf("samp_rate=[%d] bits_per_samp=[%d] num_samp=[%d]\n",
       samp_rate, bits_per_samp, num_samp);

int *data = (int *) malloc(num_samp * sizeof(int));
read_wav_data(data, samp_rate, bits_per_samp, num_samp);

unsigned int i;
// for (i = 0; i < num_samp; ++i) {

for (i = 0; i < 100; ++i) {
    printf("%d\n", data[i]);
}

return EXIT_SUCCESS;
    }

   void read_wav_header(unsigned int *samp_rate, unsigned int *bits_per_samp,
                 unsigned int *num_samp)
{
unsigned char buf[5];

// freopen ("/Users/ericbrotto/Desktop/iPhoneData/tmp/Hack.wav","r",stdin);

 freopen ("C:/Documents and Settings/Eric.Brotto/Desktop/Eric_Other/Files/Hack.wav","r",stdin);



/* ChunkID (RIFF for little-endian, RIFX for big-endian) */
fread(buf, 1, 4, stdin);
buf[4] = '\0';
if (strcmp((char*)buf, "RIFF")) exit(EXIT_FAILURE);


/* ChunkSize */
fread(buf, 1, 4, stdin);

/* Format */
fread(buf, 1, 4, stdin);
buf[4] = '\0';

printf("IS THIS WAVE?  -->%s<--\n",(char*)buf);


if (strcmp((char*)buf, "WAVE")) exit(EXIT_FAILURE);

/* Subchunk1ID */
fread(buf, 1, 4, stdin);
buf[4] = '\0';
printf("IS THIS fmt?  -->%s<--\n",(char*)buf);

if (strcmp((char*)buf, "fmt ")) exit(EXIT_FAILURE);

/* Subchunk1Size (16 for PCM) */
fread(buf, 1, 4, stdin);
if (buf[0] != 16 || buf[1] || buf[2] || buf[3]) exit(EXIT_FAILURE);

/* AudioFormat (PCM = 1, other values indicate compression) */
fread(buf, 1, 2, stdin);
if (buf[0] != 1 || buf[1]) exit(EXIT_FAILURE);

/* NumChannels (Mono = 1, Stereo = 2, etc) */
fread(buf, 1, 2, stdin);
unsigned int num_ch = buf[0] + (buf[1] << 8);
if (num_ch != 1) exit(EXIT_FAILURE);

/* SampleRate (8000, 44100, etc) */
fread(buf, 1, 4, stdin);
*samp_rate = buf[0] + (buf[1] << 8) +
(buf[2] << 16) + (buf[3] << 24);

/* ByteRate (SampleRate * NumChannels * BitsPerSample / 8) */
fread(buf, 1, 4, stdin);
const unsigned int byte_rate = buf[0] + (buf[1] << 8) +
(buf[2] << 16) + (buf[3] << 24);

/* BlockAlign (NumChannels * BitsPerSample / 8) */
fread(buf, 1, 2, stdin);
const unsigned int block_align = buf[0] + (buf[1] << 8);

/* BitsPerSample */
fread(buf, 1, 2, stdin);
*bits_per_samp = buf[0] + (buf[1] << 8);

if (byte_rate != ((*samp_rate * num_ch * *bits_per_samp) >> 3))
    exit(EXIT_FAILURE);

if (block_align != ((num_ch * *bits_per_samp) >> 3))
    exit(EXIT_FAILURE);



/* Subchunk2ID */
// fread reads line by line until the end. 

fread(buf, 1, 4, stdin);
  buf[4] = '\0';

if (strcmp((char*)buf, "data")) exit(EXIT_FAILURE);



/* Subchunk2Size (NumSamples * NumChannels * BitsPerSample / 8) */
fread(buf, 1, 4, stdin);

const unsigned int subchunk2_size = buf[0] + (buf[1] << 8) +
(buf[2] << 16) + (buf[3] << 24);
*num_samp = (subchunk2_size << 3) / (
                                     num_ch * *bits_per_samp);
 }


 void read_wav_data(int *data, unsigned int samp_rate,
               unsigned int bits_per_samp, unsigned int num_samp)
 {

// freopen ("/Users/ericbrotto/Desktop/iPhoneData/tmp/Hack.wav","r",stdin);
 freopen ("C:/Documents and Settings/Eric.Brotto/Desktop/Eric_Other/Files/Hack.wav","r",stdin);

  unsigned char buf;
unsigned int i, j;
for (i=0; i < num_samp; ++i) {
    unsigned int tmp = 0;
    for (j=0; j != bits_per_samp; j+=8) {
        fread(&buf, 1, 1, stdin);           
        tmp += buf << j;
    }
    data[i] = conv_bit_size(tmp, bits_per_samp);
}
 }


 int conv_bit_size(unsigned int in, int bps)
 {
const unsigned int max = (1 << (bps-1)) - 1;
 return in > max ? in - (max<<1) : in; // Supposedly this is not correct: http://ubuntuforums.org/showthread.php?p=10442711
//  return in > max ? in - ((max<<1)+2) : in;

 }

EDIT 2

On my mac it outputs all the samples in the array (ints roughly between -32000 and 32000). Here I get the output you see in the image followed by a few million zeros.

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:14:45+00:00Added an answer on May 20, 2026 at 5:14 am

    Yes. Even assuming that both compilers comply with the ISO standard (which is not necessarily so), there’s still a lot of leeway in that standard.

    For example, if your program uses implementation defined or locale-specific behaviour, the output can be different.

    If whoever wrote the program used undefined behaviour, then that’s also a possibility for different output.

    Your best bet would be to show us the code for proper analysis.

    If you’re interested in the sorts of things that can differ, Annex J of C99 (you can downlaod the draft with TC1, 2 and 3 from the bottom of that page – the differences between that and the final product are minimal) lists the portability issues (unspecified, undefined, implementation-defined and locale-specific behaviour).

    One thing you may want to be careful of. This may not apply to Tiny C but I know that the Microsoft compilers I’ve used are one of that class where "r" and "rb" are treated differently in fopen/freopen. If you just specify "r", translation takes place which may give you the wrong data from a binary file such as a wav file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bit of code that needs to sit on a windows server
I have a very simple bit of code that won't work, and have no
i have bit of code that causes an underflow: var t1, t2, delta: DWORD:
I have a bit of code that basically reads an XML document using the
I have a bit of code that passes around a ton of objects and
I have a bit of code that looks like this: text = reg.Replace(text, new
I have this bit of code that is being converted from vb6 to vb.net.
I have this bit of code that is outputting the wrong results. #include <stdio.h>
I have this bit of code that is receiving a series of messages: byte[]
I have a bit of code that determine whether or not a control (within

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.