integer variable starts at 1024?

Discussion in 'C' started by klattz, Jan 24, 2024.

  1. klattz

    klattz New Member

    Joined:
    Jan 23, 2024
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    I am following code academy's path to learning c and ran into a weird issue. When running my code the variable "a" has an initial variable of 1024? Can someone help me uderstand or lead me in the right direction to crafting a better google query? Very new to the language so all feedback is greatly appreciated. Ignore my awful code and random comments. I was trying to troubleshoot the error and went all over the place.
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main () {
      int counter1[4] = {0, 0, 0, 0};
      int counter2[4] = {0, 0, 0, 0};
    
      char string1[] = {"dbb cccccaacb cdbababdcdcdab dcdad"};
      char string2[] = {"bbbcc bdddccccad cdbbaaacaccdabdd"};
      char mainvar;
    
      int a,b,c,d = 0;
      printf("[+]%i[+]\n\n", a);
    
      for (int z = 0; z < strlen(string1); z++) {
      
        mainvar = string1[z];
        printf("%c\n", mainvar);
        if (mainvar == 'a') {
          a += 1;
          printf("%i\n", a);
          counter1[0] = a;
          //printf("InLoop %i\n", a);
          //printf("%i", counter1[0]);
        } else if (mainvar == 'b') {
            b += 1;
            counter1[1] = b;
            //printf("InLoop %i\n", b);
        } else if (mainvar == 'c') {
            c += 1;
            counter1[2] = c;
            //printf("%i", counter1[2]);
        } else if (mainvar == 'd') {
            d += 1;
            counter1[3] = d;
            //printf("%i", counter1[3]);
        }
    
        //printf("%i\t%i\t%i\t%i\n", counter1[0],counter1[1],counter1[2],counter1[3]);
    
    
      
       // for (int x = 0; x < strlen(string2); x++) {
    
        //}
      }
    
        printf("\n%i", counter1[0]);
        printf("\n%i", counter1[1]);
        printf("\n%i", counter1[2]);
        printf("\n%i", counter1[3]);
    }
     
  2. klattz

    klattz New Member

    Joined:
    Jan 23, 2024
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    I found my solution. It won't let me post the link but if you search /7565794/assign-single-value-to-multiple-variables @ google it is the first result.

    I was defining multiple variables incorrectly. In the above link the user DipSwitch explains you can do it either way below

    "You can do it two ways:
    char r = 0, g = 0, b = 0;
    or
    char r, g, b;
    r = g = b = 0;"

    This was a simple mistake on my end.
     
  3. Danishhafeez

    Danishhafeez New Member

    Joined:
    Jan 29, 2024
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    It seems like you're encountering an issue with the variable a having an initial value of 1024. Let's take a look at your code:

    int a,b,c,d = 0;

    In C, when you declare multiple variables on a single line like this, only the last variable (d in this case) is initialized to 0. The variables a, b, and c are not explicitly initialized, so they will contain garbage values (values from whatever happened to be in memory at that location). That's why a seems to have an initial value of 1024.

    To fix this, you need to explicitly initialize all variables to 0:

    int a = 0, b = 0, c = 0, d = 0;


    This way, all variables will start with a value of 0, and you won't encounter unexpected values.

    Also, a small note regarding your loop:

    for (int z = 0; z < strlen(string1); z++) {


    Calling strlen(string1) in each iteration of the loop can be inefficient, especially if string1 is long. It's better to calculate the length once outside the loop and store it in a variable. For example:

    int length1 = strlen(string1);
    for (int z = 0; z < length1; z++) {
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice