Thread: Setting Ints to 0 makes code slow, why ?

  1. #1
    Registered User
    Join Date
    Dec 2023
    Posts
    13

    Setting Ints to 0 makes code slow, why ?

    Code:
    #include <stdio.h>
    
    int main(void)
    
    {
    
    int fahr = 0, total = 0, step = 0;
    
    printf("Fahrenheit ?: ");
    scanf("%d", &fahr);
    
    printf("Total ?: ");
    scanf("%d", &total);
    
    printf("Steps ?: ");
    scanf("%d", &step);
    
    for(fahr = fahr; fahr <= total; fahr+=step)
    printf("Fahrenheit: %d\t Celsius %d\n", fahr, (fahr - 32) * 5/9);
    
    }
    If I simply do "int fahr, total, step;" then the code runs immediately but if I set them to 0 it takes ages until the first question comes up and Iam just curious why thats the case ?

    Thanks in advance (inserted the text without formatting, hopefully thats correct this time, VisualStudioCode only has the option to "copy").

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by crusher152 View Post
    If I simply do "int fahr, total, step;" then the code runs immediately but if I set them to 0 it takes ages until the first question comes up and Iam just curious why thats the case ?

    Thanks in advance (inserted the text without formatting, hopefully thats correct this time, VisualStudioCode only has the option to "copy").
    This would seem to be a compiler/O/S implementation problem. Under Debian Linux, I see no difference, using gcc on the command line, with or without initialization, and should not see any difference!.

    Local variables should ALWAYS be initialized.

    You should also add the following to the bottom of main();
    Code:
    return 0;
    Last edited by rstanley; 01-19-2024 at 05:46 AM.

  3. #3
    Registered User
    Join Date
    Dec 2023
    Posts
    13
    Ok thank you I will reinstall everything and see if it makes a difference. I actually have sometimes compiling issues so something doesnt seem to be right. Again thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to modify this code for chars instead of ints
    By Zone-C# in forum C++ Programming
    Replies: 1
    Last Post: 08-27-2019, 03:29 PM
  2. Uncommented code makes for a bad day
    By Landslyde in forum C++ Programming
    Replies: 10
    Last Post: 09-24-2014, 10:35 AM
  3. I can't figure out why this C code is running so slow...
    By Stachelsk in forum C Programming
    Replies: 7
    Last Post: 01-28-2010, 08:01 AM
  4. Slow drawing code
    By tjpanda in forum Windows Programming
    Replies: 5
    Last Post: 05-09-2008, 05:09 PM
  5. This program makes my computer SLOW
    By Granger9 in forum C Programming
    Replies: 5
    Last Post: 10-15-2002, 01:18 AM

Tags for this Thread