-1

i am a beginner in C programming, and while making a program i wondered if there can be a universal input function input(); be made, such that the variable name given in braces be asked for input like this:

input(a);

result:
enter value of a: 10

now (after the input) if i write like this in any program like:

#include<stdio.h>
main()
{
    int a;
    float b;
    char c;
    input (a);
    printf("value of a = %d\n",a);  // 1
    input(b);
    input(c);
    printf("value of b = %f\n",b);  // 2
    printf("c = %c\n",c);           // 3
}

then output should be like this:
output:
enter value of a: 10
***value of a = 10***
enter value of b: 10.0
enter value of c: D
***value of b = 10.000000***
***c = D***

i.e the function parameter should take values acording to the type of variable concerned(like char,float,int etc) like if a were char then value entered in a would be saved accordingly. i thought to implement it using structures but i am not able to think, how to link the actual parameter passsed in input(); with the struct members

update: i have written the lines 1,2,and 3(in comments: the printf() functions) just to show that the values of a,b, and c are aptly/rightfully stored in their corresponding types

r_goyal
  • 1,117
  • 1
  • 11
  • 20
  • 1
    Try [union](http://www.tutorialspoint.com/cprogramming/c_unions.htm). – Dayal rai Sep 20 '13 at 07:31
  • You can't do such things in C. But with macros you can try to realize something alike. – Eddy_Em Sep 20 '13 at 07:42
  • 1
    There's no universal `output` function either. You use `printf` above. So don't bother and use `scanf`: `printf ("enter value of a: ") ; scanf ("%d", &a) ;` As @Eddy_Em said, you might be able to use macros to get the name of the variable into the prompt string, but usually it is not worth the effort. If you find yourself inputting too many things, it is advisable to consider some kind of structured input -- configuration files etc. – Anton Tykhyy Sep 20 '13 at 08:04
  • you should use different functions or use an extra argument to specify type in input function. – 999k Sep 20 '13 at 08:06
  • I think this will give you the answers http://stackoverflow.com/q/479207/1151831 – 999k Sep 20 '13 at 08:13
  • Passing a variable by value will never let you build any kind of general input function... – Dariusz Sep 20 '13 at 08:14
  • @555k but how am i supposed to add an argument with a variable type, do you want to say something like `input(int a)`; can i do like this? won't it invoke an error? – r_goyal Sep 20 '13 at 08:15
  • 1
    It's a waste of your valuable time to design such a function, believe me even i used to think like this when i was a beginner(same concept!) – nj-ath Sep 20 '13 at 08:16
  • @Dariusz then can i use passing by pointer method? – r_goyal Sep 20 '13 at 08:24
  • Have to agree with @TheJoker - if you want this, either you don't need C (obviously one of it's main features is not hiding something behind syntax sugar) or you think that you need this abstraction while you really don't. – keltar Sep 20 '13 at 08:51
  • @keltar "obviously one of it's main features is not hiding something behind syntax sugar"... how can u say that. its not a feature of C..rather it is a feature of c++ that distinguishes C from c++,also u can implement such functions by declaring them extern type in caling file.. ita a basic feature of C you must have forgot – r_goyal Sep 20 '13 at 11:18
  • @akash_sinha13134 no ideas what are you trying to say. I meant, C code is clear as-is, if you see something - you can be sure it's exactly as it reads; opposed to C++ where simple code could be anything (overloading). Why have you brought extern here? And _what_ exactly have i forgotten? – keltar Sep 20 '13 at 11:23
  • @keltar it is a basic property of c wherein one can use a function declared globally in any file by including that file name in any program and then declaring the function as `extern`. in this case the `codes` in that function remain hidden from the calling file – r_goyal Sep 20 '13 at 11:31
  • @akash_sinha13134 again, why are you bringing it here? If you called a function, well - you called a function, you know at least it's address and return/argument types and maybe name, but it's exactly what you did. In C++, each operator and each function could be overloaded, so it's much less clean what exactly will be called (you need to check every potential overloaded operator and function). And what are you trying to say now - well, i surely don't understand you, sorry. – keltar Sep 20 '13 at 11:37
  • @keltar i am just saying that to tell you that i am talking about c not c++ – r_goyal Sep 20 '13 at 11:50
  • @akash_sinha13134 no, i surely don't get it. The whole point was that you *could* call function defined in another object file? It's possible in about any language i know. – keltar Sep 20 '13 at 12:00

1 Answers1

2

With literally the same syntax as you've written - no, you need C++ features for this (function overloading), but it worth knowing that internally it will just use set of different functions, so actually it's almost exactly the same as input_int, input_char, etc.

You could use union (probably union within a struct - you need to save actual type somewhere), but it's quite different approach.

Macros (especially C11 _Generic's, if you could use C11 features) could be good alternative too.

keltar
  • 17,711
  • 2
  • 37
  • 42