What does the "exit code 10" mean in C? [duplicate] - c

This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 7 years ago.
I am using Sublime to program in C but when the code is compiled it keeps returning [Finished in 1.1s with exit code 10]. What does that mean? What is going on?
#include <stdio.h>
void main()
{
int x;
scanf("%d", &x);
printf("%d", x);
}

#include <stdio.h>
int main()
{
int x;
scanf("%d", &x);
printf("%d", x);
return 0;
}
main( ) does not take void as a return type.In C programming language we use int as a return type of main .Exit codes are nothing but Windows System Error Codes .I think you are using windows Operating system and for windows Operating system
exit code 10 means :The environment is incorrect.
May be your installation process was not correct .
Reinstall it and run the program .

Related

How do I get Linux system's up-time? [duplicate]

This question already has answers here:
What API do I call to get the system uptime?
(5 answers)
Closed 5 years ago.
I want to find a way to find the linux system's uptime.
I do not want to read the uptime file in /proc/.
Any other way?
#include <stdio.h>
#include <sys/sysinfo.h>
int main(void)
{
struct sysinfo sys_info;
sysinfo(&sys_info);
printf("Seconds since uptime: %lu\n", sys_info.uptime);
return 0;
}
This should get you started. If you need more help, type man sysinfo into the terminal.

Printf() not executing before the "while(1)" loop in C language in Linux [duplicate]

This question already has answers here:
Why does printf not flush after the call unless a newline is in the format string?
(10 answers)
Closed 5 years ago.
I noticed something strange with the following code.
int main()
{
printf("Test"); // Section 1 do something here....
while(1)
{
;
}
}
Section 1 should be executed first, then the program should get stuck in while loop.
But the result was that "Test" didn't get printed, but it got stuck in the while loop. I wonder why the code in Section 1 does not get executed?
I ran the code on Ubuntu 14.04 LTS(compiled with the default gcc compiler)
The stdout stream is buffered, therefore it will only display what's in the buffer after it reaches a newline. Add :
fflush(stdout);
after line :
printf("Test");
See also this link for other alternatives.
This must work:
#include <stdio.h>
int main() {
printf("Test");
while(1){}
}
To compile:
gcc file.c
To execute:
./a.out

Cygwin showing error iostream.h is unable to locate [duplicate]

This question already has answers here:
fatal error: iostream.h no such file or directory [duplicate]
(3 answers)
Closed 8 years ago.
I wanted to run C programs on windows in order to achieve this I downloaded cygwin(Linux like environment for windows) made a program and kept it on a directory called ..\cygwin\home\Computer
Code goes here
#include<iostream.h>
void main(){
printf("Hai");
}
When i am trying to execute this program using command prompt.
$ g++ hai.c
Its throwing out an error
hai.c:1:21: fatal error: iostream.h: No such file or directory
#include<iostream.h>
^
compilation terminated.
What is going on any idea?
change to
#include <iostream>
#include <cstdio>
int main(){
printf("Hai");
}
or with g++ -x c hai.c or gcc hai.c
#include <stdio.h>
int main(){
printf("Hai");
}

C program sqrt not working [duplicate]

This question already has answers here:
Why do you have to link the math library in C?
(14 answers)
Closed 9 years ago.
I also entered include<math.h> but it still doesnt work. People are saying to enter -Im but im new to this where do I put -Im and how do I fix this.
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
float a=0, b=0, c=0, root1=0, root2=0;
printf("Enter the value of a,b and c to determine the roots\n");
scanf("%f%f%f",&a,&b,&c);
root1=(-b+sqrt(b*b-4*a*c))/(2*a);
root1=(-b-sqrt(b*b-4*a*c))/(2*a);
printf("The first roots of the quadratic equation are\nFirst root=%.1f\nSecond root=%.1f",root1,root2);
return 0;
}
You have a copy-paste bug here:
root1=(-b+sqrt(b*b-4*a*c))/(2*a);
root1=(-b-sqrt(b*b-4*a*c))/(2*a);
should be:
root1=(-b+sqrt(b*b-4*a*c))/(2*a);
root2=(-b-sqrt(b*b-4*a*c))/(2*a);
Also you may need to link with the math library, e.g.
$ gcc -Wall foo.c -o foo -lm
Two things: first you copy pasted "root1" twice so you will lose the "plus" value and root2 will be zero. Second, for the benefits of others, the problem is most probably at compile time and the googled answer is there:
http://www.cs.cf.ac.uk/Dave/C/node17.html
And you should test for imaginary values:
if(b*b-4*a*c < 0){
printf("error: complex solution unsupported, see http://en.wikipedia.org/wiki/Square_root\n");
exit(1);
}

How to use System(const char*) in TC++

Today , When i coding, met a question..my Code as follow:
#include<stdlib.h>
void main()
{
system("dir");
getch();
}
The question : The user Screen is nothing..Why ? where is my result?
If you want the output when using system, at least into something you can read in your application, you need to pipe the output:
system("dir > /tmp/output.txt");
FILE *f = fopen("/tmp/output.txt", "r");
char text[1024]; // max sizeof of 1 kb, any more and I'd consider using `malloc()` instead.
fread(text, 1, 1024, f);
printf("%s\n", text);
fclose(f);
There are some problems in your program, at least one of which has already been mentioned.
void main() should be int main(void).
As I recall, the Windows/DOS getch function is declared in <conio.h>; you should have a #include directive for it. Be aware that both <conio.h> and getch are non-standard.
Since main returns int, you should return an int result.
But none of these problems explain the problem you're seeing.
With these changes:
#include <stdlib.h>
#include <conio.h>
int main(void)
{
system("dir");
getch();
return 0;
}
This should work; it should show a directory listing of whatever directory your program runs in (which is determined by TC; I don't know the details).
It's possible that the program is running in an empty directory, which means the dir command wouldn't show any files, but it should still produce some output.
Try commenting out the system() call and adding a printf call (note the added #include <stdio.h>):
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(void)
{
printf("Hello, world\n");
getch();
return 0;
}
This should open a console window, print "Hello, world" in it, and wait for you to type Enter.
If you still don't see any output (either no console window, or a console window with nothing in it), then you have a problem that's not related to the system() call. Most likely the problem has to do with the way you're using Turbo C (I presume that's what "TC" stands for).
The main function in every C program is supposed to return an int you are returning void
Change void to int:
#include<stdlib.h>
int main()
{
system("dir");
getch();
}
When I tested, the dir command ran in my console and printed to standard out.
May be he is the running the program directly in the Turbo C IDE and hence his output is not visible. If he runs the program directly from cmd line it works. I remember you need to run Alt - F5 or some other combination to see the output window in Turbo C++

Resources