How can I run a "hello, world!" programme? - c

I have this basic program:
#include<stdio.h>
int main() {
printf("Hello, world!");
return 0;
}
but how can I saw the compiler said: Hello, world!
and how can I start it?
sorry if it's simple but I'm new.

If you're using any UNIX (Linux/Mac) machine it's pretty simple:
You open up the terminal in the folder your program is located.
For example you type in gcc "program_name.c" -o "program_name" to compile the program
And now you have to execute the program in the terminal typing this ./"program_name"
you can put whatever you like in the "program_name" :)

Related

Gitpod to run a C hello world program

I am new to C language but I use Gitpod for other languages. I would like to know how to run this simple "hello world" main.c program from the Gitpod interface step by step?
#include <studio.h>
void main(){
printf("hello")
}
Many thanks !
After fixing the errors you can compile the code with gcc main.c and run the binary a.out.
Errors as already mentioned:
studio.h -> stdio.h
...hello") -> ...hello");
after the printf("hello")
you should put ;
//like this
printf("hello");
#include <stdio.h>
int main(){
printf("hello");
return 0;
}

"Hello world" program not running

I typed this program on code blocks but it is showing error on int main line
Here's the program
#include <stdio.h>
int main()
{
printf("Hello");
return 0;
}
The message return is " multiple definition of main"
The sample code is correct. It might be an IDE configuration error. Use gcc to compile your code on Linux and on Windows you can install MinGW and execute from a command window.

C netbeans error

I've started to learn a C language and i am using netbeans IDE.
I tried the most simple classic = printing "Hello World".
code
int main(int argc, char** argv) {
printf('Hello World!');
return (EXIT_SUCCESS);
}
When i run projects, netbeans writes BUILD SUCCESSFUL (total time: ...)
but then it writes
read from master failed
: Input/output error
RUN FAILED (exit value 1, total time: 414ms)
How to make it work?
This is the Hello World program. It includes the relevant library header file. I added the newline with \n to ensure the output buffer is flushed.
#include<stdio.h>
int main(void) {
printf("Hello World!\n");
return 0;
}
Program output:
Hello World!
Your problem is coming from Netbeans IDE. Right click your project and go to properties. In the properties, click run and change the console type to standard output.

where can i find an implementation of the coordination language linda in c?

I am trying to find an implementation of the language in c that is actually based on the language standards ,here is a simple example
#include "linda.h"
int worker(int num)
{
int i;
for (i=0; i<num; i++)
out("hello, world");
return 0;
}
int main()
{
int result;
eval("worker", worker(5));
in("hello, world");
in("hello, world");
in("hello, world");
in("hello, world");
in("hello, world");
in("worker", ? result);
return 0;
}
So directly to your question:
Where can i find an implementation of the coordination language linda in c?
Here: http://www.comp.nus.edu.sg/~wongwf/linda.html
You can download it and use it!
If you download it you should get this:
linda //dir
|
| - linda.c //file
| - linda.h //file
| - primes.c //file
| - README.txt //file
But you have to know:
You must call linda_init() before you do linda stuff
You must call linda_end()before exiting
There is no eval() function but you can use spawn()
The library varargs.h is outdated! So i would recommend you to use: stdarg.h
So that means it could be that you have to rewrite stuff!
So there is only one thing todo!
Download it and get started!
EDIT:
Another way you could go is to download a Linda-C compiler and write your programs in Linda-C (*.clc).
So but back to library from above:
I got a program working with linda in c with this library!
I have: GNU GCC compiler(tdm64-1) 4.7.1 MinGW64
What you have to look out for:
In linda.h you have to change the include sys/varargs.h to stdarg.h since varargs is outdated!
That means also that you have to change in linda.c all va_arg() calls and change the following type:
float -> double
char -> int
After you have done that you should be able to compile linda.c to linda.o with this command line:
gcc -c -O linda.c (Also it could be that you have to direct to the gcc.exe so that the command gcc is known in Windows cmd)
So as an example:
Path to gcc compiler:
"C:\Program Files (x86)\Dev-Cpp\MinGW64\bin"
Path to linda library:
"C:\Users\xy\Downloads\linda\linda"
So now if you open cmd and type in: gcc --version and it's not found you have to direct to the gcc.exe file like this (otherwise your fine and you don't have to direct to the compiler dir):
cd "C:\Program Files (x86)\Dev-Cpp\MinGW64\bin"
So if your in the directory where gcc.exe is located you should be able to execute that command: gcc --version
And from there you can execute the command to create linda.o! Which looks like this in this example:
gcc -c -O C:\Users\xy\Downloads\linda\linda\linda.c
If all worked out! You should end up with the file linda.o in your compiler directory
And now with the file linda.o you can compile primes.c (Which is in this library as an example) and get a linda programm in c working! with the following line:
gcc -o C:\Users\xy\Downloads\linda\linda\primes -O C:\Users\xy\Downloads\linda\linda\primes.c C:\Users\xy\Downloads\linda\linda\linda.o -lpthread -lm
With this line you should end up with the .exe file: primes.exe! Which you can copy in the compiler directory and execute! If you don't have it in this dir then lpthreadGC2_64.dll is unknown and it can't be executed!
Few side notes to the compiling of this example:
lpthread -> pthread library
lm -> math library
Also i would recommend you to add the following line in the primes.c file at the end so that if you execute it, the window does not close instant: system("pause");
Also for your problem that with this library you can't use eval() there are a few workarounds/ solutions:
You can make a for loop and call your functions multiple times! (Since the function you call with spawn() has to have void as return type)
You can define a constant with: #define CALLS 5 and use this in the function itself for the commands to execute in a for loop or also in the main function to call spawn() multiple times
I think there are more ways to solve this!
So all that said. Your program should look something like this:
#include "linda.h"
#define NUM 5
void worker() {
int i;
for (i = 0; i < NUM; i++)
out("%s", "hello, world");
}
int main() {
int result;
spawn(worker);
in("%s", "hello, world");
in("%s", "hello, world");
in("%s", "hello, world");
in("%s", "hello, world");
in("%s", "hello, world");
in("%s?d", "worker", &result);
system("pause");
return 0;
}
Hope this helps you!
Cheers

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