0

How can we print the name of youngest in output? I want to calculate the youngest person.

That is my code:

    #include <stdio.h>
    #include <conio.h>
    int main() {
    int john;
    int ahmad;
    int saleem;

    printf("Enter the age of john,ahamd, saleem simaltanoeusly\n");
    scanf_s("%d\n%d\n%d", &john, &ahmad, &saleem);


    int youngest = john;
    if (john > ahmad) 
        youngest = ahmad;
    if (ahmad > saleem)
        youngest = saleem;

    printf("youngest of you is %d", youngest);

    _getch();
    return 0;
}
Régis B.
  • 10,092
  • 6
  • 54
  • 90
M Ahmad Rana
  • 33
  • 1
  • 9

4 Answers4

5

You can do it like this, with a macro

#include <stdio.h>

#define SHOW(varname) printf("%s is age %d", #varname, varname)

int main(void){
    int john   = 23;
    int ahmed  = 19;
    int saleem = 27;
    if (john < ahmed && john < saleem)
        SHOW(john);
    else if (ahmed < saleem)
        SHOW(ahmed);
    else
        SHOW(saleem);
    return 0;
}

Program output:

ahmed is age 19
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
2

Better to put the names in an array. Then refer to each name by its index.

const char * names[] = {
    "John",
    "Ahmad",
    "Salem",
};

int youngestage = -1;
char* youngest = 0;
printf("Enter the age of john,ahamd, saleem simaltanoeusly\n");
for (int i=0;i<sizeof(names)/sizeof(names[0]);i++)
{
  int age;
  scanf("%d", &age);

  if (youngestage == -1 || age < youngestage)
  {
    youngestage = age;
    youngest = names[i];
  }
}
printf("%s is the youngest", youngest);
printf("youngest of you is %d", youngestage);
ronalchn
  • 12,225
  • 10
  • 51
  • 61
0

The simplest solution is to create a structure for each person, which would contain their name as well as their age.

struct person {
    char *name;
    int age;
}

You also need to fix your calculation of the youngest age. In your case, you could just do:

int youngest = john;
if (youngest.age > ahmad.age) 
    youngest = ahmad;
if (youngest.age > saleem.age)
    youngest = saleem;

For a universal solution you should do this by looping through an array.

MC93
  • 791
  • 6
  • 14
0

So you want to print the variable name associated with the smallest value here?

The preprocessor directive # when used with the variable in the macro outputs the name of the variable used by the coder

//Code by Rahul Anand Jha
//using GCC Compiler
#include <stdio.h>
#include <conio.h>
#define display(n) printf(#n) //add this
int main() {
int john;
int ahmad;
int saleem;

printf("Enter the age of john,ahamd, saleem simaltanoeusly\n");
scanf("%d\n%d\n%d", &john, &ahmad, &saleem);


if(john<ahmad && john <saleem)
    display(john);

else if(ahmad<john && ahmad<saleem)
display(ahmad);

else
    display(saleem);

return 0;
}

However i would suggest you not to use such a solution since this would be benefecial only when the name of the persons you are testing them against are John,Ahmad,Saleem.Otherwise you need to modify your code every time.

You could also use Structures,Arrays.

Rahul Jha
  • 1,131
  • 1
  • 10
  • 25
  • In languages such as C# you have huge amount of inbuilt fatures to look back on your code(self-introspect) and this has been given the name Reflection.It is computationally expensive.I cant comment about C though.Read [this](http://stackoverflow.com/questions/1353022/reflection-support-in-c) – Rahul Jha Oct 08 '15 at 10:35