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?

4
  • 2
    Your language selection is wrong. Use another programming language that supports refering variables from their name expressed as strings. Commented Mar 31, 2022 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(). Commented Mar 31, 2022 at 11:38
  • 2
    In C variable names only exist in the source code. Commented Mar 31, 2022 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. Commented Mar 31, 2022 at 12:11

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.