LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Ubuntu (https://www.linuxquestions.org/questions/ubuntu-63/)
-   -   execute a programm (https://www.linuxquestions.org/questions/ubuntu-63/execute-a-programm-4175485226/)

goldengriff 11-20-2013 02:53 AM

execute a programm
 
hi dear's,
how and where i can execute below code??

int main()
{
system("ls");
return 0;
}

thanks

colucix 11-20-2013 03:16 AM

This is a little C program that does a system call to execute the ls command (pretty not useful, since you can do ls directly from the shell). Anyway, you have to write the code in a text file and then compile it using gcc. Run the resulting executable (a.out by default) and you will get a list of the files in the current working directory.

goldengriff 11-20-2013 03:40 AM

i run nano main.c and write below code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("\nHello World,\nWelcome to my first C program in Ubuntu Linux\n\n");
return(0);
}
when i run above programm,system show below error ,why??
root@goldengriff:/home/goldengriff/CCPP/HelloWorld# ./main.c.save
./main.c.save: line 1: syntax error near unexpected token `newline'
./main.c.save: line 1: `#include<stdio.h> '

tigerton 11-20-2013 04:08 AM

Before running, you have to compile it!!!!!!
Type:

Code:

gcc main.c -o main
Then you can run it:

Code:


./main


goldengriff 11-20-2013 05:04 AM

when i compile code,below error showed:
test.c:1:21: fatal error: iostream.h: No such file or directory
compilation terminated.

tigerton 11-20-2013 05:24 AM

Post the code

goldengriff 11-20-2013 05:32 AM

#include<iostream>
main()
{

cout<<"hello";
return(0);
}

tigerton 11-20-2013 05:43 AM

Here's the right code:

Code:


#include<iostream>
#include <stdio.h>

using namespace std;

main()
{

cout<<"hello";


}

for compiling it:

Code:


g++ test.cpp -o test

for running it:

Code:


./test

Keep studying!!!!!!!


All times are GMT -5. The time now is 07:58 AM.