Home > Software design >  Slight change in input makes the program go nuts
Slight change in input makes the program go nuts

Time:10-26

I have this piece of code to check whether second date is earlier than the first date the program works normally when I input these with this order without commas 9,9,2021 but when I do input 09 for the first input It skips the second input and directly jumps into the third input I can't figure out why this is the case since I am fairly new to C

#include <stdio.h>

// Array format [DD,MM,YYYY]
int date[3];
int secondDate[3];

// ANSI color codes
#define RED "\x1b[31m"
#define GREEN "\x1b[32m"

int main()
{
    printf("Enter the day of first date: ");
    scanf("%i", &date[0]);

    printf("Enter the month of first date: ");
    scanf("%i", &date[1]);

    printf("Enter the year of first date: ");
    scanf("%i", &date[2]);

    printf("Enter the day of the second date: ");
    scanf("%i", &secondDate[0]);

    printf("Enter the month of the second date: ");
    scanf("%i", &secondDate[1]);

    printf("Enter the year of the second date: ");
    scanf("%i", &secondDate[2]);

   return 0;

 }

CodePudding user response:

The %i formar specifier is recognizing the number base prefixes (that is "0x" for hexadecimal, and '0' for octal). The '0' prefix is for octal. And 09 is an invalid octal, as octal numbers contain digits 0-7.

You can use the %d specifier instead of %i, which is only interpreting decimal numbers.

  • Related