I am new to C programming and I am currently learning loops. In the below program,
#include<stdio.h>
main()
{
int i;
for(i=1;i++<=5;printf("%d",i));
}
i tried to compile in dev c++ compiler but it is giving error "[Error] ld returned 1 exit status"
You need to include the <stdio.h> header, and also, main needs a return type (int) and a return value. Changing the program to this will make it compile (at least it did using GCC) and run:
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
for(i=1;i++<=5;printf("%d",i));
return 0;
}
The quotes you used in the ā%dā are illegal too, use normal quotes: "%d".
Apart from that, doing the printf inside the loop head might be legal, but it's pretty bad style. Usually in a for-loop you would have have initialization;condition;increment(or decrement or w/e) in the head, and do side-effects in the body of the statement.
I would try writing the for loop as:
for(i=1;i < 6;i++) { printf(ā%dā,i); }
I have run this program manually on my notebook and i got Output 23456
Then i run this on Dev c++ and it is giving the same output 23456 without any error and i have just copied and pasted from ur question dun know why its showing error on ur runtime may be u have not saved it as C file
Related
So here is my code:
#include <stdio.h>
typedef struct dat{
int broj;
int sir;
}sir;
int main() {
sir sir;
FILE* fordat;
int i=0;
fordat = fopen("dattk.txt","w+");
while(i<100) {
i++;
sir.broj = i;
sir.sir=-i;
fprintf(fordat,"%d %d", sir.broj, sir.sir);
}
// fseek(fordat,0,SEEK_SET);
//rewind(fordat);
// fscanf(fordat,"%d %d",sir.broj,sir.sir);
printf("%d% %d",sir.broj,c);
fclose(fordat);
return 0;
}
I am studying in C and made this simple example program of writing and reading from a file...
If I remove the comments from either fseek or rewind or fscanf, the program runs.
However, if I remove the comments from fseek AND fscanf the program compiles, but crashes on run.
Can't figure out why...
Your fscanf call is broken - change:
fscanf(fordat,"%d %d",sir.broj,sir.sir);
to:
fscanf(fordat,"%d %d",&sir.broj,&sir.sir);
Important: if you had compiled with warnings enabled (e.g. gcc -Wall ...) then the compiler would have helpfully pointed out this mistake to you, thereby saving you time and effort. Always enable compiler warnings and take heed of them.
And one more thing: you have absolutely no error checking in your code - you should check for failure after fopen and all other calls which might potentially fail.
#include <stdio.h>
int main(int argc, char *argv[])
{
print("Merry Christmas and happy holidays!");
return 0;
}
Can someone please run this on your system and check back why?
It says, linker error print is not defined.
There's no print function in standard C. The function you are looking for is printf (f here is short for formatted.
printf("Merry Christmas and happy holidays!");
It is not print, it is called printf function. There is nothing call print in C .standard
linker errors are due to the call of functions that are not defined or due to wrong way of call of a function.
In your program function print() does not exist anywhere as a user defined function nor does it exist in the header 'stdio.h' that you included.
To print a line in C you need to use printf() statement and not print() as in your program.
For a very specific project, I need to write a 16-bit program in C and I'm using Microsoft QuickC in MS-DOS to write this program. Now I'm pretty sure the syntax of my program is correct but the program just won't compile and it thinks I have syntax errors. Is this because C-compilers in MS-DOS using an older version of C with different syntax?
#include<stdio.h>
main()
{
printf("Hello World!");
}
Not even that simple hello world program will compile and run.
you should define main as int
so change your code to :
int main() { // define main as an int returning function
// your code
return 0; // Also make sure you have return statement in main
}
and it will compile
Here is what it says in the standards:
1 The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int :
int main(void) { /* ... */ }
Edit:
Ok from your comments .. you are now getting this error:
C1024: cannot open include file 'stdio.h'
Here is a cause and solution from microsoft:
http://support.microsoft.com/kb/97809
You can't omit the type of the function main, or any other C function, for that matter. So, you want
void main() { ... }
or
int main(int argc, char **argv) { ... }
although with the latter one the compiler will usually require you to return a value.
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;
}
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++