1

The thing with both C and Python is that you can actually change the variable type by using a function. An example of this is atoi() which converts a char * (string) into an int. So I experimented it by making a function:

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

int make_int(char *str);

int main() {
    char str[256];
    scanf("%255s", str);
    printf("%i\n", make_int(str));
}

int make_int(char *str) {
    return atoi(str);
}

It works as intended, but what I did not account for is that in C, you have to tell the variable what kind it is, unlike Python, where you can just make a name and assign it. I want to actually do other variables, but it might get more complicated in the process.

Q: How do I make a function where the parameters are actually random kinds like how Python does it? How do I actually change any variable to an int as well?

BMPL
  • 35
  • 16

1 Answers1

1

You don't as such. C is a statically typed language. However, as per Random Davis's comment, void * and passing the address can work, but you have no way of knowing what the passed variable's type is without passing another parameter (thus format strings for printf etc, and they use another mechanism: varargs (... in the prototype)).

taniwha
  • 371
  • 2
  • 5
  • That really helped. But I'm not accepting this just yet. I forgot a little question that I had to ask alongside the problem with changing the variable into an `int`. – BMPL Sep 30 '20 at 15:36