{
  "$type": "site.standard.document",
  "bskyPostRef": {
    "cid": "bafyreiae3fzxulmefwfkm56wa2mhgrywlahm3vgk5ghra6hc4hjo42f7ua",
    "uri": "at://did:plc:4n6wgsqsqm6q2hjncgwmreey/app.bsky.feed.post/3mkduotb2y5p2"
  },
  "path": "/post/49376060",
  "publishedAt": "2026-04-25T14:44:39.000Z",
  "site": "https://programming.dev",
  "tags": [
    "Learn Programming",
    "akunohana",
    "learn_programming",
    "0 comments"
  ],
  "textContent": "submitted by akunohana to learn_programming\n2 points | 0 comments\n\nIf I allocate `[]` or `[1]` to `intCheck` the program goes into an endless loop from the very start. No user input required. If, however, I allocate `[2]` or more, the program works as intended, as long as the user input is _less_ than the number of bytes previously allocated. If, however, the user input is _greater_ , the program repeats the `else` condition of the `while` loop - here, `printf` - the number of times that corresponds to the byte size previously allocated to `char intCheck`. Is this an overflow of `choice` or what?\n\n\n    //Bank of Haruhi\n\n    //TODO\n    // - Create user account: require user first name, last name, user name, password, age (deny if < 20 yrs)\n    // - Prompt login (deny if != password && username)\n    // - Display menu (\"About the Bank of Haru\", \"Account Settings\", \"Check your balance\", \"Deposit\", \"Withdraw\", \"Close account\", \"Logout\")\n\n    #include <stdio.h>\n    #include <string.h>\n    #include <stdbool.h>\n\n    int main(void) {\n\n    //Base\n    char intCheck[3] = \"\"; //Will \"if (sscanf(char,%d,&int) == true)\" check for ANY \"char\" or a predetermined amount of \"char\"?\n\n    //Registration and login\n    char firstName[] = \"\";\n    char lastName[] = \"\";\n    char password[] = \"\";\n    int age = 0;\n\n    //Menues and selections\n    int choice = 0;\n    int arithmeticChoice = 0;\n\n    //Balance, deposit and withdrawal\n    float balance = 0.0;\n    float deposit = 0.0;\n    float withdraw = 0.0;\n\n    printf(\"Welcome to the Bank of Haruhi! Please login (1) or create an account (2): \");\n\n    while (fgets(intCheck, sizeof(intCheck),stdin)) {\n    intCheck[strlen(intCheck) - 1] = '\\0';\n    if (sscanf(intCheck, \"%d\", &choice) == true && sizeof(intCheck) == 4 && choice == 1 || choice == 2) break; //I'm keeping \"sizeof(intCheck)\" in order to excercise byte size input validation, but I could just remove it and let \"if choice == 1 || 2\" restrict the valid input.\n    else printf(\"Please enter \"\"1\"\" to login or \"\"2\"\" to create a new account: \");\n    }\n\n    return 0;\n\n    }\n\n\nThe program is, of course, not nearly complete. I just stopped doing anything else, as soon as I stumbled upon this phenomenon.",
  "title": "[C] What is the relationship between string length, allocated memory size and overflow?"
}