C Program Slow Start in MacOS - c

I'm following a tutorial in c programming, a very simple c file hello.c:
#include <stdio.h>
int main(void) {
int num;
printf("Hello world! Give me an integer:\n");
scanf("%d", &num);
printf("Thanks! I've always been fond of %d.\n", num);
return 0;
}
Source from: https://www.cs.princeton.edu/courses/archive/fall02/cos126/assignments/hello-osx.html
Then compile and run it in using gcc hello.c then ./a.out
There seem to be a pause before I see Hello world! Give me an integer: from the time of ./a.out and hit return.
Why is that so? And how to improve this startup?

Related

Certain C code not displaying output in VS Code

So basically, I have this simple Hello World program and when executed, it outputs correctly.
#include <stdio.h>
int main(){
printf("Hello");
}
Output: Hello
But for some reason adding a scanf command will cause the program to not output anything. It will display that it's running but it won't display anything.
#include <stdio.h>
int num1;
int main(){
printf("Enter number: \n");
scanf("%d", &num1);
printf("%d", num1);
}
Output:
[Running] cd "d:\programming\" && gcc main.c -o main && "d:\programming\"main
I know my code is okay since I tried it on other IDEs and it worked correctly. I even copied code from the internet and tried to run in VSCode but it still didn't work. It still works perfectly fine yesterday but this problem just pops in nowhere.
I use Visual Studio Code 1.62.3 with C/C++, C/C++ Compile Run, and Code Runner extensions.
It's a buffering problem. When standard output (stdout, where printf writes) is connected to an actual terminal then it's line-buffered, which means output is actually written to the terminal when there's a newline.
However, VSCode probably uses its own terminal emulation and uses pipes to connect stdout to that terminal. That means the output will be fully buffered, and you need to explicitly flush it.
So modify the code as such:
printf("Enter number: \n");
fflush(stdout); // Actually write the output to standard output
scanf("%d", &num1);
printf("%d", num1);
That it works for the first example is because then the process terminates, and all output is flushed automatically.

scanf() function failure

I installed the MingW C compiler and I am trying to compile and run a C program there.
I use Visual Stdio Code IDE.
OS is WINDOWS 10.
Compiler is MinGW.
My programs that do not use scanf() or gets() all run successfully but when I use scanf() function the program does not take input. I also used gets() function but that has the same problem. So when I press "run" button it does not give any output.
Here is my minimal reproducible example. Is this a compiler problem or is my code wrong?
#include <stdio.h>
int main()
{
int n;
printf("Enter a number: ");
//scanf("%d", &n);
}
OUTPUT for the above code is:
[Running] cd "d:\code\C\" && gcc #13sum.c -o #13sum && "d:\code\C\"#13sum
Enter a number:
[Done] exited with code=0 in 0.29 seconds
If I add a scanf():
#include <stdio.h>
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
}
OUTPUT for the above code is:
[Running] cd "d:\code\C\" && gcc #13sum.c -o #13sum && "d:\code\C\"#13sum
[Done] exited with code=1 in 31.526 seconds
After using fflush(stdout);
#include <stdio.h>
int main()
{
int n;
printf("Enter a number: ");
fflush(stdout);
scanf("%d", &n);
}
[Running] cd "d:\code\C\" && gcc #13sum.c -o #13sum && "d:\code\C\"#13sum
Enter a number:
[Done] exited with code=1 in 2.804 seconds
After using fflush(stdout); printf() statement is executed but now I am unable to enter the value. I am typing but it is not showing in the output .
Compiler is not any giving errors in any of the above case.
the problem is your compiler its working fine for me.
your code take input until you enter 0 and then print the sum.
here's my output:-
Enter a number: 45 Enter a number: 34 Enter a number: 0 Sum is = 79%
so, check if you installed your mingw C compiler properly here.

MinGW gcc C compiled program detected as virus by avast

I have readed a lot of people with the same problem but the most of them are Windows Defender, in my case it only happens with avast(free version) and it detects this simple hello program compiled version:
#include <stdio.h>
int main()
{
printf("Name: ");
char name[20];
scanf("%s", name);
printf("Hello, %s\n", name);
return 0;
}
as "Win32:TrojanX-gen[Trj]" but some days ago i made another program and ran it into notepad++ console using cmd and nothing happened, is it a problem with avast or is it my pc?
Thank you.

scanf produces segfault when the program is run with a custom entry point (using gcc 7.4.0)

Consider the following code:
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("main\n");
int a;
scanf("%d", &a);
printf("a = %d\n", a);
return 0;
}
int main1() {
printf("main1\n");
int a;
scanf("%d", &a);
printf("a = %d\n", a);
exit(0);
return 0;
}
int main2() {
printf("main2\n");
int a = getchar() - '0';
int b = getchar() - '0';
int c = getchar() - '0';
printf("a = %d\n", 100 * a + 10 * b + c);
exit(0);
return 0;
}
Assuming that the code resides in a file called test.c, the following works fine (it prints "a = 123"):
gcc -o test test.c
echo 123 | ./test
If, however, I run the program with a custom entry point, I get the dreaded Segmentation fault:
gcc -o test test.c -e"main1"
echo 123 | ./test
But if I replace the scanf with three getchars, the program runs fine again despite being run with a custom entry point:
gcc -o test test.c -e"main2"
echo 123 | ./test
To make things even more interesting, these problems occur with gcc 7.4.0 but not with gcc 4.8.4.
Any ideas?
The -e command line flag redefines the actual entry point of your program, not the “user” entry point. By default, using GCC with the GNU C standard library (glibc) this entry point is called _start, and it performs further setup before invoking the user-provided main function.
If you want to replace this entry point and continue using glibc you’ll need to perform further setup yourself. But alternatively you can use the following method to replace the main entry point, which is much simpler:
gcc -c test.c
objcopy --redefine-sym main1=main test.o
gcc -o test test.o
Note, this will only work if you don’t define main in your code, otherwise you’ll get a “multiple definition of `main'” error from the linker.

How to write a Hello World in C

I wrote little program to print "Hello world" in C. I'm not a C programmer, but I liked to try it. In my program there is an error. Please tell me what is it?
This is my program:
int main(){
printf("Hello World");
}
I wrote this with my Java Experiences. I can't find what is wrong.
You can't directly use printf() function as in Java. You should tell the compiler that you are going to use the input/output stream. You can tell it in this line:
#include <stdio.h>
and also you should enter this line at the end of the source code:
return 0;
this will tell the compiler :
"If the program succeed It will return 0 otherwise It will return any other number"
This means if your program is successful main() function will return 0. Then the compile know the program is Ok.
Then at last your complete code is:
#include <stdio.h>
int main() {
printf("Hello world");
return 0;
}
To compile this and see the word "Hello World", just save this file as a .c file and Open cmd in your program directory and type
gcc hello.c -o hello && hello
(Replace the 'hello.c' with your filename, and 'hello' with the name you want to put with your .exe file)
Remember My computer is Windows. And this compile code is for windows. If your OS is UNIX like OS. then use this code to compile:
gcc hello.c -o hello
./hello
A full hello world program in C:
#include <stdio.h>
int main(void) {
printf("Hello World\n");
return 0;
}
Then compile (assuming gcc) and execute it:
gcc -o test test.c
./test
First, you have to use a header file.
#include <stdio.h>
What that does is bring up a header file with a bunch of commands in them. That will make it recognize the "printf" piece of code.
Next, you have to close your program. By not having a closing statement, the program will not compile because it doesn't know if that is the end of the code. Use this at the end of your program...
return 0;
That closes the program, meaning that the compiler can stop looking for other code. You may also want to look at some programming manuals (there are dozens of free online ones) to learn about the syntax.
One last thing: Most pieces of C code require a semicolon at the end. This is not true for the "int main" statement, nor is it for the header file which I have defined above. The "return" function that closes the program, does however, need a semicolon.
Hoped this helped.
Should also include a pause at the end:
#include <stdio.h>
int main(void) {
printf("Hello World\n");
//Read a character from the console
getchar();
return 0;
}
Just like import in Java programs, in here you have to include libraries you're using in your program. You have used library function printf, but not included stdio.h.
I agree there are many ways to write one of the simplest way is
#include<stdio.h>
int main(void){
printf("Hello World\n");
return 0;
}
You can even use different ways as suggested above.
You should first look at the structure of "main". Try to understand the various parts as already explained so well in the above answers.
"#include" : The preprocessing directives to be included in the program. But why? Because you are trying to use the functions defined inside them.
int : The return type of "main" program. But why? Because the function calling "main" needs to know if the "main" program has functioned correctly.
main : The entry point of your code. Dont ask why here :-)
main( void ) : To tell the compiler that we are not passing any arguments to program "main"
return 0 : Beacuse you promised "main" that you will return something if "main" will function properly.
Finally the code:
#include <stdio.h>
int main( void )
{
printf( "Hello World\n" ) ; //Notice the '\n' here. Good coding practice.
return 0 ;
}
#include <stdio.h> //Pre-processor commands<br/>
void main() //Starting point of the program<br/>{ //Opening Braces
printf("Hello World\n"); //Print Hello World on the screen<br/>
return 0;
} //Ending Braces
Check it once it will work, I have written it with comments:
#include<stdio.h> //Pre-processor commands
void main() {
printf("Hello World\n"); //Print Hello World on the screen
}
A full hello world program in C:
#include <stdio.h>
int main(void) {
printf("Hello World\n");
return 0;
}
Then compile (assuming gcc) and execute it:
gcc -o test test.c
./test
You can't use printf() function as in Java. You have to tell the compiler what you are going to use.
You can tell this as follows:-
#include <stdio.h>
You must enter this line in last:-
return 0;
Then Your complete code is:-
#include <stdio.h>
int main(){
printf("Hello World");
return 0;
}
For compiling this and see the word "Hello World", just save this file as a .c file and Open cmd in your program directory and type:-
gcc hello.c -o hello && hello
(Replace the 'hello.c' with your filename, and 'hello' with the name you want to put with your .exe file)
Remember My computer is Windows. So I can compile only for Windows OS.
#include <stdio.h>
int main() {
// printf, used to print (display) Hello World
printf("Hello World ! ");
// return 0, as the main function is of type int so it must return an integer value
return 0;
}

Resources