C program could not be compiled and executed at Windows command prompt

AI Thread Summary
The discussion revolves around a C program designed to convert a given number of days into years, weeks, and days. The initial code provided contains a syntax error in the include statement, which should be corrected to `#include <stdio.h>`. A user encountered issues compiling the program using gcc on Windows, including a missing file error and confusion over the command syntax. It was clarified that the command should be `gcc DaysConvertion.c`, and that incorrect file naming could lead to overwriting the source file. The user was advised to check their PATH settings for gcc and to use a proper code editor like Visual Studio Code to avoid issues with copying and pasting code. After addressing the syntax error, the program successfully ran in an online IDE, demonstrating the correct output for a sample input. The conversation highlights the importance of proper syntax and command usage in programming, as well as the need for appropriate tools for coding.
WMDhamnekar
MHB
Messages
376
Reaction score
28
TL;DR Summary
I have downloaded and installed ' C c-programs application from Microsoft Windows store. I copied one program named 'Days Convertion' from it to my Notepad++ and saved it as c source file in the Documents folder. When I tried to compile it using gcc, gcc said No such file or directory. How can I fix this error?

When I tried to run this c program in online IDE, it showed several compiling errors. https://ide.geeksforgeeks.org/?ref=ndm
C:
/*    Write a c program to convert given number of days to a measure of time  *    given in years, weeks and days. For example 375 days is equal to 1 year  *    1 week and 3 days (ignore leap year)  */

 #include stdio.h #define DAYSINWEEK 7

 void main() {    int ndays, year, week, days;

    printf("Enter the number of days\n");    scanf("%d",&ndays);

    year = ndays/365;    week = (ndays % 365)/DAYSINWEEK;    days = (ndays%365) % DAYSINWEEK;

    printf ("%d is equivalent to %d years, %d weeks and %d days\n",           ndays, year, week, days); }

 /*-----------------------------------------------

When I tried to compile it using gcc at windows command prompt, I got the following error several times. After giving this command at command prompt, 'DaysConvertion.c' file disappeared from the Documents folder.

1653322169276.png


How can I fix this error?
 
Technology news on Phys.org
Are you sure you are in the right Documents folder? At the command prompt, what do you get when you type dir *.c ?
 
  • Like
Likes WMDhamnekar
berkeman said:
Are you sure you are in the right Documents folder? At the command prompt, what do you get when you type dir *.c ?
Yes. I am in appropriate documents folder. In user variables, I set the PATH for gcc downloaded from https://winlibs.com/#usage-commandprompt. C: \mingw W64\bin.
 
The file names are the wrong way round in the command: you are telling gcc to compile the source file DaysConvertion (which doesn't exist) to produce the object file DaysConvertion.c.

Just type gcc DaysConvertion.c and see what happens.

By the way "convertion" is spelled "conversion" in English.
 
  • Like
Likes WMDhamnekar, phinds and berkeman
pbuk said:
The file names are the wrong way round in the command: you are telling gcc to compile the source file DaysConvertion (which doesn't exist) to produce the object file DaysConvertion.c.

Just type gcc DaysConvertion.c and see what happens.

By the way "convertion" is spelled "conversion" in English.
Thanks for finding out my and author's spelling mistake (author means author of 'C c-programs ' application from Microsoft windows store).
After gcc compiling command at command prompt, the file was no more in the documents folder.
The result of your suggestion is as follows:

1653327100570.png

My advise to you is Just run this program in the online IDE given by me , you will see plenty of errors in the program
 
Well, what happens if you put it back?
 
berkeman said:
Well, what happens if you put it back?
When I do these steps several times using Notepad++, I repeatedly get the same error at the command prompt.
 
Can you post the result of the dir *.c and the compile command using a screenshot of the DOS Box?
 
WMDhamnekar said:
When I tried to run this c program in online IDE, it showed several compiling errors. https://ide.geeksforgeeks.org/?ref=ndm

After I fixed one syntax error, I was able to get the program to run in the online IDE.
This line - #include stdio.h
should be written as #include <stdio.h>

When I ran that code, I entered 300, and the program reported "300 is equivalent to 0 years, 42 weeks and 6 days".

I don't use gcc, so can't give you any help on that compiler, other than to suggest that you don't have things set up correctly.
 
  • Like
Likes WMDhamnekar and berkeman
  • #10
Your earlier command in post #1 told gcc to overwrite your .c file with the compiler output. That might have wiped out your code .c file. The dir command in your post #5 says that the file doesn't exist, so what did you expect gcc to do?
I suggest that you start over and put your code in the DaysConvertion.c file. This time use the simple command "gcc DaysConvertion.c" and see what happens.
 
  • Like
Likes pbuk and berkeman
  • #11
Would you tell me which online IDE did you use successfully?
My use of online IDE gave me the following result:
1653375118483.png

1653375194969.png
 
  • #12
See all those characters highlighted in red? Why do you think they are highlighted in red?
 
  • Like
Likes FactChecker
  • #13
pbuk said:
See all those characters highlighted in red? Why do you think they are highlighted in red?
I don't know the meaning of …, .., . in red colour in the program I put in the online IDE. In my opinion, these red dots represents errors. But how to debug this program is my problem. Do you know it's solution? :confused::sorry:
 
  • #14
WMDhamnekar said:
In my opinion, these red dots represents errors. But how to debug this program is my problem. Do you know it's solution?
You could try deleting the errors (were they there in the code you copied or do you think they have appeared as part of the copying and pasting process?)

Also note that the Notepad doesn't work well with text copied from a web page, use a proper text editor designed for code like Visual Studio Code.
 
  • Like
Likes FactChecker and WMDhamnekar
  • #15
pbuk said:
You could try deleting the errors (were they there in the code you copied or do you think they have appeared as part of the copying and pasting process?)

Also note that the Notepad doesn't work well with text copied from a web page, use a proper text editor designed for code like Visual Studio Code.
These errors were appeared as a part of copying and pasting process.
1653395488779.png

This question is solved.:biggrin::smile::oldlaugh:
 
Last edited:
  • Like
Likes berkeman, FactChecker and pbuk
Back
Top