Reputation:
I want to learn C. I would like to get my notepad++ to compile and run my .c files. I have done what is needed to be done. I am using MinGW and have added the plugin. I added this prompt:
npp_save
cd "$(CURRENT_DIRECTORY)"
gcc -Wall -Werror "$(FILE_NAME)" -o $(NAME_PART) -march=native -O3
NPP_RUN $(NAME_PART)
but whenever I go to compile and run, a command prompt appears and takes all the input. But when the time comes to show me an output command prompt closes out. Also there is no output on the console embedded in the notepad++ either. I then have to use Windows cmd to execute. Can someone please help me. I am a beginner.
for instance, take a look at this:
#include <stdio.h>
/* Note: Program assumes years are in the same century. */
int main(void)
{
int month1, day1, year1, month2, day2, year2;
int first_earlier = 0;
printf("Enter first date (mm/dd/yy): ");
scanf("%d/%d/%d", &month1, &day1, &year1);
printf("Enter second date (mm/dd/yy): ");
scanf("%d/%d/%d", &month2, &day2, &year2);
if (year1 != year2)
first_earlier = year1 < year2;
else if (month1 != month2)
first_earlier = month1 < month2;
else
first_earlier = day1 < day2;
if (first_earlier)
printf("%d/%d/%d is earlier than %d/%d/%d\n", month1, day1, year1, month2, day2, year2);
else
printf("%d/%d/%d is earlier than %d/%d/%d\n",month2, day2, year2, month1, day1, year1);
getchar();
return 0;
}
when pressing F6, the command prompt appears, it asks dates and when pressing enter after entering the second date prompt closes without showing me an output anywhere.
Upvotes: 1
Views: 1926
Reputation: 13580
First of all, g++
is the C++ compiler. If you have C code, then you have to
use gcc
to compile C code.
I don't really understand what you mean by a command prompt appears and takes all the input, but judging from the behaviour that the console closes immediately, then this is because the console closes right after the program exits.
When making a double-click on an (console) executable, a terminal is spawned and it executes your program (not the command line). Normal behaviour of terminals is that when the executed program exists, the terminal closes. This also would happen when launching the program through your IDE.
As you can see, if you open a terminal and execute it from there, the terminal stays open, because the command line is still active1.
If you want to launch a program via double-click or IDE, then you have to make sure that your program doesn't exit right away. An easy way to do this is by making the user wait for an input.
#include <stdio.h>
int main(void)
{
printf("hello world\n");
puts("Press Enter to continue...");
getchar();
return 0;
}
Here the getchar
would wait for user input and it would exit after the user hits ENTER. This is a workaround for launching console programs via
double-click and IDEs. However I think this is bad practice, the correct way
would be to start a terminal yourself and execute your program yourself.
Many terminals have the option that they don't close immediately when the running program ends. For that you should be able to check the settings of the terminal. Sometimes IDEs have also a checkbox in the settings that you have to check so that the terminal doesn't close right away.
edit
The reason why getchar
at the end does not wait is because of the previous
scanf
.
When you enter something in the command line, a newline ('\n'
) is also added
to the input stream.
scanf("%d/%d/%d", &month2, &day2, &year2);
If the format is correct, scanf
will consume all input but leave behind the
newline in the input buffer. The last getchar()
will consume the newline that
is already in the buffer, and because of that it doesn't wait for further user
input.
You have to clear your input buffer. Add this function before the main
:
void clear_stdin(void)
{
int c;
while((c = getchar()) != '\n' && c != EOF);
}
The call it after the scanf
calls:
printf("Enter first date (mm/dd/yy): ");
scanf("%d/%d/%d", &month1, &day1, &year1);
clear_stdin();
printf("Enter second date (mm/dd/yy): ");
scanf("%d/%d/%d", &month2, &day2, &year2);
clear_stdin();
Now that the input buffer is cleared, the last getchar
will wait for more user
input and you program will block until you press ENTER.
Fotenotes
1Note that a terminal (console) is not the same as the command
line. The terminal is the program that displays the text and allow users to
type with the keyboard. A command line is just a program that allows you to
enter commands and start programs. In Windows the command line is cmd.exe
called command line, it is mostly found in C:\Windows\System32
.
The default settings are that when you open a terminal without telling which
command to execute, it will automatically open a command line, in Windows would
be cmd.exe
by default.
Upvotes: 2