r/cprogramming • u/[deleted] • May 16 '25
Why code runs accurately only when 1 line is commented out?
Hi Guys. Wondering if anyone can explain to a noob why in the below code when I compile and run, it will create the basic encrypted number accurately only when I comment out the line "int flag = 0" (even when the related while loop for the flag is commented out). If I remove the comment on "int flag = 0" it just creates junk code?
#include<stdio.h>
int main (void) {
int digit_1, digit_2, digit_3, digit_4, number, temp, num, counter, encrypted_number = 0;
printf ( "%s", "Please Enter Number: " );
scanf ( "%d", &num);
//int flag = 0;
/*while ( flag == 0 ) {
if ( num < 0 ) {
puts ( "Number Too Small" );
printf ( "%s", "Enter New Number: " );
scanf ( "%d", &num);
} //end if
else if ( num > 9999 ) {
puts ( "Number too large" );
printf ( "Enter New Number: " );
scanf ( "%d", &num);
} //end if
else {
flag = 1;
} //end else
} //end while */
while ( counter < 4 ) {
temp = num;
printf ( "%s" "%d\n", "temp = num: ", temp );
temp %= 10;
printf ( "%s" "%d\n", "temp %= 10: ", temp );
temp += 7;
printf ( "%s" "%d\n", "temp += 7: ", temp );
temp %= 10;
printf ( "%s" "%d\n", "temp %= 10: ", temp );
if ( counter == 0 ) {
digit_4 = temp;
} //end if
else if ( counter == 1 ) {
digit_3 = temp;
} //end else if
else if ( counter == 2 ) {
digit_2 = temp;
} //end else if
else {
digit_1 = temp;
} //end else
printf ( "%s" "%d\n", "counter: ", counter );
counter++;
num /= 10;
printf ( "%s" "%d\n", "num /= 10: ", num );
puts ( "" );
} //end while
printf ( "%s%d\n%s%d\n%s%d\n%s%d\n",
"digit_1: ", digit_1, "digit_2: ", digit_2, "digit_3: ", digit_3, "digit_4: ", digit_4 );
encrypted_number = ( digit_3 * 1000 ) + ( digit_4 * 100 ) + ( digit_1 * 10 ) + ( digit_2 );
printf ( "%s%d\n", "Encrypted Number: ", encrypted_number );
} //end main