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

In summary, This conversation is about fixing an error in a C program that converts days into years, weeks, and days. The program was originally written in a file called "DaysConvertion.c" and compiled using gcc, but the file disappeared from the Documents folder after the command was entered. The conversation includes suggestions on how to fix the error, including checking the file names and using an online IDE. The conversation also addresses the issue of red highlighted characters in the code and offers tips on how to properly copy and paste code from a web page.
  • #1
WMDhamnekar
MHB
381
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
  • #2
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
  • #3
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.
 
  • #4
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
  • #5
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
 
  • #6
Well, what happens if you put it back?
 
  • #7
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.
 
  • #8
Can you post the result of the dir *.c and the compile command using a screenshot of the DOS Box?
 
  • #9
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

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

Why is my C program not compiling at the Windows command prompt?

There could be several reasons why your C program is not compiling at the Windows command prompt. Some common reasons include syntax errors, missing header files, and incorrect compiler options.

How can I fix a compilation error in my C program?

To fix a compilation error in your C program, you will need to carefully review the error message and check for any syntax errors or missing header files. You may also need to edit your code to ensure it is compatible with the compiler you are using.

What does it mean if my C program compiles but does not execute at the Windows command prompt?

If your C program compiles but does not execute at the Windows command prompt, it could mean that there is an error in your code that is preventing it from running properly. You may need to use debugging techniques to identify and fix the issue.

Can I use the Windows command prompt to compile and execute a C program?

Yes, the Windows command prompt can be used to compile and execute C programs. However, you will need to have a C compiler installed on your computer and ensure that it is properly configured.

How can I check if my C program was successfully compiled and executed at the Windows command prompt?

You can check if your C program was successfully compiled and executed at the Windows command prompt by looking for any error messages during the compilation process and checking the output of your program after execution. If there are no errors and the output is as expected, then your program was likely compiled and executed successfully.

Back
Top