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 8251251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:10:26+00:00 2026-06-08T00:10:26+00:00

I know there is a C++ version of this question, however I’m using standard

  • 0

I know there is a C++ version of this question, however I’m using standard typedefs not templates.

I’ve written a program that works with 16-bit wav files. It does this by loading each sample into a short. The program then performs arithmetic on the short.

I’m now modifying the program so it can with with both 16- and 32-bit wavs. I was hoping to do a conditional typedef, i.e. using short for 16-bit and int for 32-bit. But then I realised that the compiler probably would not compile the code if it did not know what the type of a variable is beforehand.

So I tried to test out the following code:

#include <stdio.h>

int
main()
{
  int i;
  scanf("%i", &i);

  typedef short test;

  if(i == 1)
    typedef short sample;
  else 
    typedef int sample;

  return 0;
}

And got got the following compiler errors:

dt.c: In function ‘main’:
dt.c:12:5: error: expected expression before ‘typedef’
dt.c:14:5: error: expected expression before ‘typedef’

Does this mean that runtime conditional typedefs in C are not possible?

[Open-ended question:] If not, how would you guys handle something like this?

  • 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-06-08T00:10:27+00:00Added an answer on June 8, 2026 at 12:10 am

    All types in a program must be known at compile time.

    In C++ you could compile your code for short and int using templates; in C you can do this using macros (specifically, X-macros).

    Put your calculation code in a separate file called e.g. dt.tmpl.c, then in dt.c write:

    #define sample int
    #include "dt.tmpl.c"
    
    #define sample short
    #include "dt.tmpl.c"
    

    Your dt.tmpl.c code can then use sample as a preprocessor token to name types and paste into function names, for example:

    #define PASTE(name, type) name ## _ ## type
    #define FUNCTION_NAME(name, type) PASTE(name, type)
    
    sample FUNCTION_NAME(my_calculation, sample)(sample i) {
        return i * 2;
    }
    

    This will result in two functions int my_calculation_int(int i) and short my_calculation_short(short i) which you can then use elsewhere.

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

Sidebar

Related Questions

This question refers to performance, not to file size. I know there are min
NOTE: This is the simple version of my previous question that SHOULD have been
I am using ApacheDS version 2.0.0-M3 (I not very familiar with how this works).
I know there were earlier problems with this in < 4.7.4 Qt versions. has
Does anyone know if there will ever be a true 64-bit version of Cygwin?
Is there a ActionScript-version of JavaScript's Canvas.clearRect()? I only know graphics.drawRect(...) which allows me
Is there any way to know which version of MS-Office I have on my
I know there are lots of tools on the net that can make our
I know there is a lot on this topic but I can't get any
I've got an Adobe Flash 10 program that freezes in certain cases, however only

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.