LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-06-2011, 03:01 PM   #46
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled

A syntaxerror in a.out makes no sense to me.

Could you do what someone recommended in a post above and (provided your program is called nabeel.c)
Code:
make nabeel
or as usual
Code:
gcc -o nabeel nabeel.c
and then try to execute the program
Code:
./nabeel
Markus
 
1 members found this post helpful.
Old 07-06-2011, 03:15 PM   #47
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Also, I recommend that you use "int main()" instead of "void main()". "void main()" is actually wrong, even though some compilers support it.
 
2 members found this post helpful.
Old 07-06-2011, 10:45 PM   #48
Nabeel
Member
 
Registered: Nov 2009
Location: Pakistan
Distribution: Ubuntu
Posts: 294

Original Poster
Rep: Reputation: 17
The gcc command worked and the program executed
Thanks
 
Old 07-07-2011, 08:21 AM   #49
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Nabeel View Post
The gcc command worked and the program executed
Thanks
Good, but I wanted to say something:

In another thread, you tried to execute C code using the shell. Did you try running the executable like this:

Code:
sh a.out
? That clearly won't work, because compiled code is machine code that runs on the CPU, not a bunch of shell commands. The shell doesn't run stuff (other than shell scripts), the kernel does.

I see that it's a common misconception that the shell is somehow strongly integrated into the OS, and that's simply wrong. It's just another program, there's nothing special about it. All it does is both provide an interactive environment that lets you easily run other programs, and let you automate things by putting the commands in a file instead of typing them interactively.
 
2 members found this post helpful.
Old 07-07-2011, 08:53 AM   #50
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Quote:
Originally Posted by MTK358 View Post
...
Code:
sh a.out
...
Yes, this would make sense as an explanation.

@Nabeel: be sure to read the manuals! There are many tutorials in the internet, about C, about Linux in general, the Shell and many more.

Markus
 
1 members found this post helpful.
Old 07-08-2011, 10:50 PM   #51
pafoo
Member
 
Registered: Jul 2011
Location: Alabama
Distribution: Red Hat/Ubuntu/Solaris
Posts: 37

Rep: Reputation: 11
*chant* python! python! python!

Atleast it will teach you to indent =) A skill alot of programmers lack lol
 
Old 07-11-2011, 04:53 AM   #52
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Spam, reported.
 
Old 07-12-2011, 11:51 PM   #53
Nabeel
Member
 
Registered: Nov 2009
Location: Pakistan
Distribution: Ubuntu
Posts: 294

Original Poster
Rep: Reputation: 17
Hi
When I compile a program, The compiler throws out a binary file. SO my Question is "Can I take this binary file to any other PC and Run it there, Provided that, the new machine is a windows or linux based pc"????
 
Old 07-13-2011, 12:21 AM   #54
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
It will run on a Linux-PC only.

If you have a 32bit Linux your program will run on any 32bit Linux-PC. 64bit respectively, but it is possible to run 32bit-programs on 64bit-systems when one has a multilib-environment.

Long story short: programs for Linux will only run on Linux. Programs for Windows will only run on Windows.

There are some exceptions: Java-programs run inside a virtual-machine and are independent of the operatingsystem. And Scripting-languages are not compiled but have an interpreter, so they are also independent of the OS.

Markus
 
1 members found this post helpful.
Old 07-13-2011, 02:57 AM   #55
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Quote:
Originally Posted by Nabeel View Post
Hi
When I compile a program, The compiler throws out a binary file. SO my Question is "Can I take this binary file to any other PC and Run it there, Provided that, the new machine is a windows or linux based pc"????
Quote:
Originally Posted by markush View Post
It will run on a Linux-PC only.

If you have a 32bit Linux your program will run on any 32bit Linux-PC. 64bit respectively, but it is possible to run 32bit-programs on 64bit-systems when one has a multilib-environment.

Long story short: programs for Linux will only run on Linux. Programs for Windows will only run on Windows.

There are some exceptions: Java-programs run inside a virtual-machine and are independent of the operatingsystem. And Scripting-languages are not compiled but have an interpreter, so they are also independent of the OS.

Markus
Clarification: if may run on other Linux systems (if you compiled it under Linux), provided that the environments are similar. For example if the program uses libraries that are not present on the other system, then it won't run. If you're careful enough, there are good chances that it will. And if you write your code not to depend on any external libraries that are operating system specific, you can expect to be able to compile your code on Linux and Windows alike; typically, though, programs use system-specific libraries which means they cannot be "just compiled" on another OS (say, Linux -> Windows) and have it work. In such cases those "dependencies" have to be modified to work on the other system before compilation.
 
Old 07-13-2011, 08:12 AM   #56
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
This is maybe a minor point maybe, but you should not be writing void main() in modern C code. It should be more like this:

Code:
int main(void) {
    # Do whatever
    
    # Later, perhaps much later...
    return 0;
}
There are other more complex forms of that, but that's a simple start.

See the following for discussion:
 
Old 07-25-2011, 02:58 PM   #57
Nabeel
Member
 
Registered: Nov 2009
Location: Pakistan
Distribution: Ubuntu
Posts: 294

Original Poster
Rep: Reputation: 17
I had wrote and compiled this program tonight, although it got compiled but gave"segmentation fault" in its execution

Here is the program
Code:
#include <stdio.h>

int main(void)
{
	int age;
	char gender;
	char name[10]=("\0");
	char class[4]=("\0");
	
	printf("Input your name \n");
	scanf("%s",name);
	printf("Input your Course Name \n");
	scanf("%s",class);
	printf("YOUR AGE???? \n");
	scanf("%d",age);
	printf("Gender \n");
	scanf("%s",gender);
printf("Your Name is %s",name);
printf("You are studying in %s",class);
printf("Your gender is %s",gender);
printf("and you are %d years old",age);

getchar();


}
And this is the error
Code:
Input your name 
Nabeel
Input your Course Name 
BSc
YOUR AGE???? 
18
Gender 
m
Segmentation fault
 
Old 07-25-2011, 03:29 PM   #58
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Do you understand that a char is different from a char[]?
 
Old 07-25-2011, 03:49 PM   #59
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by b0uncer View Post
...
For example if the program uses libraries that are not present on the other system, then it won't run. If you're careful enough, there are good chances that it will.
...
This all depends on the sonames of the libraries you link to, which are generally associated with the major and minor release of the library. This means most binaries that link to even just libc might not work on a different release of the same distribution. With gcc, the default options will cause linking to at least libc; you have to go out of your way to have no run-time dependencies.

In general, you should assume that a compiled program will at best only work on the exact version of the distribution you compiled it on. This is why compiled software packages are usually specific to single versions of a distribution.
Kevin Barry
 
Old 07-26-2011, 12:56 AM   #60
pielas
Member
 
Registered: Jan 2009
Location: Poland
Distribution: Arch Linux
Posts: 50

Rep: Reputation: 17
You should use scanf("%d",&age) instead of scanf("%d",age) and scanf("%s",&gender) instead of scanf("%s",gender).
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help learning programming Fred_mike Programming 49 10-19-2010 08:52 AM
programming learning way siaswar Programming 5 09-29-2009 11:58 AM
Learning C Programming Trizon Programming 8 03-30-2007 12:37 PM
learning programming nin881 Programming 13 10-19-2005 12:17 AM
C programming learning introuble Programming 7 01-03-2005 11:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration