-2

I tried to make a program (in VS 2019) that prints out each values of certain characters, but it doesn't seem to work well. When executed, it prints out the number '0', regardless of which I entered.

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

typedef struct value //Value for each characters
{
    char tier;
    int weight;
    float airspeed;
    float fallspeed;
    float fastfall;
    float dash;
}V;

 V mario; //I set 'Mario' as a variable.
 V mario; tier = 'A';
 V mario; weight = 98;
 V mario; airspeed = 1.208f;
 V mario; fallspeed = 1.5f;
 V mairo; fastfall = 2.4f;
 V mario; dash = 1.76f;

int main(void)
{
    printf("%d", mario.weight); //mario.weight is '98'.
    return 0;
}

I thought that "mario.weight" would be printed as '98', but when I executed it printed '0'.

srilakshmikanthanp
  • 2,231
  • 1
  • 8
  • 25
김도영
  • 31
  • 5

1 Answers1

0

You can do something like

     V mario={'A',98,1.208f,1.5f,2.4f,1.76f};

or

     V mario={.tier='A',.weight=98,.airspeed=1.208f,.fallspeed=1.5f,.fastfall=2.4f,.dash=1.76f};

1)In you code

 V mario; //I set 'Mario' as a variable.
 V mario; tier = 'A';
 V mario; weight = 98;
 V mario; airspeed = 1.208f;
 V mario; fallspeed = 1.5f;
 V mario; fastfall = 2.4f;
 V mario; dash = 1.76f;

Here all

     V mario; 

goes to Tentative definitions and defaults to 0.

2)Here

     tier,weight,airspeed,fallspeed,fastfall,dash.

all are defaults to int you can check this by changing mario.weight to weight in printf it will print 98.

3)global variable can initialized when it is defined but you can not do this because it is not initialization

V mario; //I set 'Mario' as a variable.
mario.tier = 'A';
mario.weight = 98;
mario.airspeed = 1.208f;
mario.fallspeed = 1.5f;
mario.fastfall = 2.4f;
mario.dash = 1.76f; 

but you can do this in main, if you want to know why this is not possible see this

Why can't I assign values to global variables outside a function in C?

for info about Tentative definitions see this https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_71/rzarg/tentative_defn.htm

srilakshmikanthanp
  • 2,231
  • 1
  • 8
  • 25