0

Getting output as 0 instead of defined value

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

int main()
{
char *val;  
char MODE_VAR[1000]="MODE0";
int MODE0_SENSE=9;

val= strcat(MODE_VAR,"\_SENSE");
printf("String value = %s, Int value = %d\\n", MODE_VAR, atoi(val));

    return 0;

}

I get output as String value = MODE0_SENSE, Int value = 0

I would like to get: String value = MODE0_SENSE, Int value = 9

What is wrong in the code and how to fix?

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • 2
    Your language selection is wrong. Use another programming language that supports refering variables from their name expressed as strings. – MikeCAT Mar 31 '22 at 11:38
  • 1
    There's lots of things wrong with this program. The one that's most egregious is that you're not actually putting anything into `val` that can be parsed by `atoi()`. – Robert Harvey Mar 31 '22 at 11:38
  • 2
    In C variable names only exist in the source code. – stark Mar 31 '22 at 11:42
  • Thanks for the comments. Unfortunately I am stuck with C for this. What would be the correct way to do this? How do I get the concatenated string to refer to variable name? The reason I need the concatenation : parts of the argument will come from user input. – Gaurav Panchanan Mar 31 '22 at 12:11

0 Answers0