I'm programming in C and I download code block IDE because it's easier to use. As you know, the minimal code in C is to write: Hello World in a CMD window. When I try to launch the program via code block, it works but when I open the .exe file directly, it open and close quickly. Can someone explain me why?
#include <stdio.h>
int main() {
int age = 0;
printf("how old are you?");
scanf("%d", &age);
printf("You are %d", age);
getchar();
return 0;
}
I am guessing your program looks something like this:
#include <stdio.h>
int main()
{
printf("Hello, world!\n");
}
The program prints Hello, world! to the screen and then ends because it has nothing left to do.
A simple fix to this is to add the function getchar() after the printf statement. This will cause the program to wait for any user input before closing. It stands for get character.
Your new program should look like this:
#include <stdio.h>
int main()
{
printf("Hello, world!\n");
getchar();
}
UPDATE:
#include <stdio.h>
int main() {
int age = 0;
printf("how old are you?\n");
scanf("%d", &age);
getchar();
printf("You are %d.\n", age);
getchar();
}
What I think you're describing is the OS destroying the temporary command window when the program is done executing. Try opening a command window yourself, and then running your .exe from there. Alternatively, use int t; scanf("%d", &t) (or something) to keep your program from finishing, thus holding the window open
Put a getchar() as the last line of main:
int main()
{
// code
getchar();
return 0;
}
Related
I'm simply trying to write down something to console using C language before getting user input, but it always asks for input then when I press enter it print what I wanted to print before getting user input.
I'm using Eclipse IDE (version 2020 -09) and my compiler is Cygwin.
this is how I'm to trying to accomplish that:
#include <stdio.h>
int main() {
printf("please enter your name: ");
char a[3];
gets(a);
return 0;
}
Output is usually buffered by default.
You can flush the output buffer explicitly by calling fflush(stdout) after printf(), eg:
#include <stdio.h>
int main() {
printf("please enter your name: ");
fflush(stdout); // <-- here
char a[3];
gets(a);
return 0;
}
Or, you can include \n in the output, eg:
#include <stdio.h>
int main() {
printf("please enter your name: \n");
char a[3];
gets(a);
return 0;
}
I'm using Eclipse to code in C/C++ and I'm struggling with what might be something pretty easy. In my code below I use printf() and after scanf(). Althougth printf is written before scanf() the output differs. I was able to find out something about similar issue here. But I wasn't able to solve it. Any ideas?
Code:
#include <stdio.h>
int main()
{
int myvariable;
printf("Enter a number:");
scanf("%d", &myvariable);
printf("%d", myvariable);
return 0;
}
Expected output:
Enter a number:1
1
Instead I get:
1
Enter a number:1
Your output is being buffered.
You have 4 options:
explicit flush
fflush after each write to profit from the buffer and still enforce the desiredbehavior/display explicitly.
fflush( stdout );
have the buffer only buffer lines-wise
useful for when you know that it is enough to print only complete lines
setlinebuf(stdout);
disable the buffer
setbuf(stdout, NULL);
disable buffering in your console through what ever options menu it provides
Examples:
Here is your code with option 1:
#include <stdio.h>
int main() {
int myvariable;
printf("Enter a number:");
fflush( stdout );
scanf("%d", &myvariable);
printf("%d", myvariable);
fflush( stdout );
return 0;
}
Here is 2:
#include <stdio.h>
int main() {
int myvariable;
setlinebuf(stdout);
printf("Enter a number:");
scanf("%d", &myvariable);
printf("%d", myvariable);
return 0;
}
and 3:
#include <stdio.h>
int main() {
int myvariable;
setbuf(stdout, NULL);
printf("Enter a number:");
scanf("%d", &myvariable);
printf("%d", myvariable);
return 0;
}
Ok, so finally I used something similar to what #zsawyer wrote as an option labelled 3.
In my code I inserted this line:
setvbuf(stdout, NULL, _IONBF, 0);
As a first line in main():
#include <stdio.h>
int main()
{
setvbuf(stdout, NULL, _IONBF, 0);
int myvariable;
printf("Enter a number:");
scanf("%d", &myvariable);
printf("%d", myvariable);
return 0;
}
I got it from here.
I was trying to make a shell interpreter similar to DOS in C language (for fun obviously)
And when i type clear, as shown in the code below it should make it so it clears the screen. But it doesn't.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
char command[128];
int loop = 0;
void main(){
clrscr();
printf("Starting shell\n");
clrscr();
while ( loop == 0){
printf("command:");
scanf("%s", &command);
if(command=='clear'){
printf("Clearing screen");
clrscr();
}
/** Other Code **/
if(command=='clear')
it's not valid string comparison. use strcmp to compare string in C.
It should be
if (!strcmp(command, "clear"))
{
printf("Clearing screen");
clrscr();
}
I know this question gets asked a hundred times over, and I've scoured all of the possibilities, but I guess I'm not adept enough to know where this problem lies. I'm programming a program where I need to fill a struct with data (ints and strings). The first time I tried it it skipped over everything but the first one, but I didn't panic since I remembered from class I needed to use fflush(stdin) to overcome this. Websites I've searched vote against use of fflush(stdin), since it has undefined behaviour. They say using getchar() would eat the extra newline, thus fixing the problem. Hence my code:
int manNode(){
Item *p;
int helper;
p = (Item*)malloc(sizeof(Item));
printf("Welk type? (Taak:1, Examen:2, Voordracht:3)\n");
scanf("%u",&helper); //selecting an itemtype
if (helper < 1 || helper > 3)
{
printf("wrong value, please try again");
return 0;
}
getchar(); //I've just put getchars everywhere for safety.
p->entrytype = helper-1;
helper = 0;
printf("Vul een naam in:\n");
scanf("%s", p->name); //this one fills in fine
getchar();
printf("Vul een vaknaam in: \n");
scanf("%s", p->course); //this one gets skipped if I type more than one letter in the last scanf()
getchar();
printf("Vul een starttijd in:\n"); //From here on out everything gets skipped
p->start = getTijd();
checkTijd(p->start);
printf("Vul een eindtijd in: \n");
p->end = getTijd();
checkTijd(p->end);
I know it's a bit messy, but focus on the scanfs and getchars. getTijd() also has a couple of scanfs in it that scan for integers, they also get skipped. I don't know where to go from here. (The code isn't incomplete, the rest is just irrelevant)
you can define a new getchar
#include <stdlib.h>
#include <stdio.h>
#define getchar(x) (scanf("%c", x))
int main ()
{
char x, y[10];
getchar(&x);
scanf("%s", y);
printf("got %s\n", y);
return 0;
}
update: this may be a better approach
#include <stdlib.h>
#include <stdio.h>
void work_to_do()
{
#define getchar(x) (scanf("%c", x))
char x, y[10];
getchar(&x);
scanf("%s", y);
printf("got %s\n", y);
#undef getchar
}
int main ()
{
work_to_do();
return 0;
}
to solve the scanf newline ignorance and getchar (but still scanf ignores whitespace)
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#define __getchar() (read(2, NULL, 1)) // 2 stands for standard error, we can make the user enter a character.
void work_to_do()
{
char y[10];
__getchar();
scanf("%s", y);
printf("got %s\n", y);
__getchar();
}
int main ()
{
work_to_do();
return 0;
}
and it's good to take a look at this:
Read space-separated values from file
scanf works as documented. Also, there is nothing wrong with scanf so long as its function is understood.
I created a copy of your code and distilled it to highlight what you want to do. You will need to fill-in the rest yourself.
In this code you need to enter a digit and press enter. Then enter a string and press enter, etc. Note. the fflush(stdout) statement is part of the standard C implementation per K&R. The fflush forces the contents of the buffer out to the console. This flushing makes for a reasonable user/computer dialog.
#include <stdio.h>
main()
{
char string[100];
int helper;
printf("Welk type? (Taak:1, Examen:2, Voordracht:3)\n");
fflush(stdout);
scanf("%d",&helper); //select an itemtype
if (helper < 1 || helper > 3)
{
printf("wrong value, please try again");
return 0;
}
printf("Vul een naam in:\n");
fflush(stdout);
scanf("%s", &string[0]);
printf("\n%s\n", string);
printf("Vul een vaknaam in: \n");
fflush(stdout);
scanf("%s", &string[0]);
printf("\n%s\n", string);
printf("Vul een starttijd in:\n");
fflush(stdout);
scanf("%s", &string[0]);
printf("\n%s\n", string);
printf("Vul een eindtijd in: \n");
fflush(stdout);
scanf("%s", &string[0]);
printf("\n%s\n", string);
}
This code ran on Eclipse with a Microsoft C Compiler.
Also, let me emphasize that if you enter: 1 AAA BBB CCC DDD and press enter then scanf will get executed five times. In this example, the code would run to completion!
So you need to trace your code logic (in this case a straight line) to see how the scanfs are invoked based on what data is entered.
Hope this helps. Please ask if more questions.
Can you tell what wrong here?
#include <stdio.h>
#include <stdlib.h>
int test (void)
{
int i;
printf("Enter a number: ");
scanf("%d",&i);
return i;
}
int main (void)
{
test();
return 0;
}
This is just a simple example but for some reason main doesn't run unless I get rid of the scanf.
Always use a '\n' at the end of your printf string. This makes the output buffer flush and print the string. Add more prints in your program.
You can rewrite your program like following, and the prints will help you understand what is happening with your program.
#include <stdio.h>
#include <stdlib.h>
int test (void)
{
int i;
printf("Enter a number: \n");
scanf("%d",&i);
printf("You just eneterd : %d\n",i);
return i;
}
int main (void)
{
printf("About to call test() \n");
test();
printf("Done calling test() \n");
return 0;
}
Better get a good C programming book for understanding these basic stuff. I suggest The C programming language
I think you must make use of fflush() or making use of a '\n' character at the end of the printf function which will eventually flush the std output buffer. For checking just make use of the printf() for printing the value of the variable just after reading the value.
Hope that helps....