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-03-2011, 09:29 AM   #16
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208

Book recommendation: The C Programming Language (K&R); it is beautifully elegant, a classic and an icon. My gast is flabbered that no one has mentioned it already
 
Old 07-03-2011, 09:33 AM   #17
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
As Sergei points out, the default name of the compiler output 'a.out' can be modified by the '-o' option. When compiling a single source file to it's executable of the same base name, I tend to use the shortcut 'make program'. This will invoke the make command, which would use it's built-in rules to make the specified program. In the case of compiling a C-language source file, make knows how to launch the compiler to compile the C source file, and link the result with the standard libraries to create the runtime executable. This, of course, somewhat violates my earlier admonition about learning the process from the ground up. However, if you add the '-d' option to the make commandline, you will get a view of what make does, allowing you to follow the process and use the same process to build your own applications and, later, to create your own Makefiles. For your cited example, try this:
Code:
make m
./m
To expand on Sergei's point about how to run the resultant executable, the runtime binary that is created will usually end up in the current working directory, '.' . Since the directory '.' is not normally include in $PATH, you must specify the complete path to the executable in order to run it. This isn't a C or programming issue per se, but is a concept foreign to many who are new to Linux shell methods.

--- rod.

Last edited by theNbomr; 07-03-2011 at 09:35 AM.
 
1 members found this post helpful.
Old 07-03-2011, 10:18 AM   #18
Nabeel
Member
 
Registered: Nov 2009
Location: Pakistan
Distribution: Ubuntu
Posts: 294

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by Sergei Steshenko View Post
You should run it:
Thanks
.
Thanks




Quote:
Originally Posted by theNbomr View Post
As Sergei points out, the default name of the compiler output 'a.out' can be modified by the '-o' option. When compiling a single source file to it's executable of the same base name, I tend to use the shortcut 'make program'. This will invoke the make command, which would use it's built-in rules to make the specified program. In the case of compiling a C-language source file, make knows how to launch the compiler to compile the C source file, and link the result with the standard libraries to create the runtime executable. This, of course, somewhat violates my earlier admonition
Is this the Make command used in compiling sources, I remember once being told to use it to compile basic256 source package, but I chose check install(or some thing like that , which gave out a deb binary). Thanks for the detailed info on the make command compiler.

Last edited by Nabeel; 07-03-2011 at 12:19 PM.
 
Old 07-03-2011, 10:31 AM   #19
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
This is a commercial book. I have no doubt that you will find illegal copies on the net, but asking for copyrighted material here is in direct violation to the LQ rules, please delete this question.
 
2 members found this post helpful.
Old 07-03-2011, 10:40 AM   #20
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Quote:
Originally Posted by TobiSGD View Post
This is a commercial book. I have no doubt that you will find illegal copies on the net, but asking for copyrighted material here is in direct violation to the LQ rules, please delete this question.
I agree, also in my opinion it is an excellent book, but no good choice for a newbie to programming at all. As far as I remember the authors recommend the book for people with programming-experience in other languages.

Fortunately there are many online-tutorials in the net. http://www.faqs.org/docs/learnc/ and http://www.linfo.org/create_c1.html and many others, you may look here: http://www.linuxquestions.org/questi...orials-825748/

Markus
 
Old 07-03-2011, 12:09 PM   #21
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by Nabeel View Post
Is this the Make command used in compiling sources
Yes, almost; it is make, not Make. It is often used to build packages when they are distributed as a source tarball. That is how the authors built them as they were being developed. Now you are the developer, and make is in play for you.
make serves two principle roles in the development process.
  • optimizing the build process
  • codifying the recipe (Makefile) used to build programs
The former is done using rules known to make regarding dependency relationships between source modules and object modules, and file time/date stamps. Only modified source files need to be compiled to build new object modules. A Makefile provides a way to describe all of the component parts of a package and how they fit together.

make will make a lot more sense to you when you've started creating applications that use multiple source files and libraries.
--- rod.

Last edited by theNbomr; 07-03-2011 at 12:11 PM.
 
Old 07-04-2011, 11:49 AM   #22
Nabeel
Member
 
Registered: Nov 2009
Location: Pakistan
Distribution: Ubuntu
Posts: 294

Original Poster
Rep: Reputation: 17
Well Here is another Program That I wrote
Code:
/* BioDATA prograam */
/* Author : Nbl*/
/* Dated 4/7/2011 */

#include <stdio.h>



void main()
{
 char n,p;
 int b;


 printf("Please input your name");
 scanf("%c %c",&n,&p);
 printf(" Input your age");
 scanf("%d",&b);

 printf("%c ,&p);
 printf("%c ,&n); 

 printf("%d",&b);

}
and upon running it I got the following error.
Quote:
c m.c
m.c:20:9: warning: missing terminating " character
m.c: In function ‘main’:
m.c:20: error: missing terminating " character
m.c:21:9: warning: missing terminating " character
m.c:21: error: missing terminating " character
m.c:23: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
m.c:23: error: expected ‘)’ before ‘;’ token
m.c:25: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast
/usr/include/stdio.h:339: note: expected ‘const char * __restrict__’ but argument is of type ‘int’
m.c:25: warning: format not a string literal and no format arguments
m.c:25: error: expected ‘)’ before ‘}’ token
m.c:25: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast
/usr/include/stdio.h:339: note: expected ‘const char * __restrict__’ but argument is of type ‘int’
m.c:25: warning: format not a string literal and no format arguments
m.c:25: error: expected ‘;’ before ‘}’ token

I asked a friend to over look the program and he made the following corrections
Code:
/* BioDATA prograam */
/* Author : Nbl*/
/* Dated 4/7/2011 */
 
#include<stdio.h>
#include<conio.h>                      // console input output library function
void main()
{
    clrscr();                                 // clear the output screen
    int b;
    char p;                                  // declaring variables
    printf("Enter your name: ");
    scanf("%s",&p);
    printf(" \n Your name is %s",&p);
    printf(" \n Enter your age: ");
    scanf("%d",&b);
    printf(" \n Your age is %d",b);
    getch();                                           // hold the output screen
}
Sadly this thing again generated the following message
Quote:
M.c:6:80: error: conio.h: No such file or directory
Now could some-one please identify the mistakes in My program and how should I correct them, also Any advise on How Can i make my friend's version execute.
 
Old 07-04-2011, 12:04 PM   #23
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hello Nabeel,

the conio.h library is found on Dos/Windows systems but not on Linux. You may check the ncurses-library. Look at the output of the following commands
Code:
man ncurses
and
Code:
apropos curses
I'm not a programmer, so I can't tell you which function's you'll have to use instead of those in your program.

Markus
 
Old 07-04-2011, 12:09 PM   #24
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,220

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
In the first version of your Biodata program, you forgot some quotatation marks. This would have been obvious if you were using an editor with syntax highlighting (which you should be).

Furthermore, I would recommend never asking your friend anything again. Not only is his knowledge of C++ outdated to the point of worthlessness, but he also lacks the responsibility to confirm that his changes work before declaring himself finished.

Last edited by dugan; 07-04-2011 at 12:13 PM.
 
Old 07-04-2011, 12:40 PM   #25
Nabeel
Member
 
Registered: Nov 2009
Location: Pakistan
Distribution: Ubuntu
Posts: 294

Original Poster
Rep: Reputation: 17
Question

Quote:
Originally Posted by dugan View Post
In the first version of your Biodata program, you forgot some quotatation marks. This would have been obvious if you were using an editor with syntax highlighting (which you should be).

Furthermore, I would recommend never asking your friend anything again. Not only is his knowledge of C++ outdated to the point of worthlessness, but he also lacks the responsibility to confirm that his changes work before declaring himself finished.
Sir Dugan

I am using gedit editor, Shold I switch to someone else? If so could you please recommend a few.
 
Old 07-04-2011, 12:50 PM   #26
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,220

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Quote:
Originally Posted by Nabeel View Post
Sir Dugan

I am using gedit editor, Shold I switch to someone else? If so could you please recommend a few.
gedit has syntax highlighting. You should have been able to tell from the colors on the screen that you were missing quotation marks.
 
Old 07-04-2011, 12:52 PM   #27
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
Sir Dugan

I am using gedit editor, Shold I switch to someone else? If so could you please recommend a few.
GEdit has syntax highlighting, and it's fine for programming.

But there are two very popular editors that are often used for programming, vi and Emacs. Both of them are not like simple GUI editors, you can't just sit down and use them without learning how they work first. Personally I like Vim (a vi clone with added features), since you can easily do everything with the keyboard without reaching for the mouse. Many others also like Emacs. I think that it's good, but all the key combinations that it uses make my fingers hurt.

You can try different ones until you find what you like.
 
Old 07-04-2011, 12:56 PM   #28
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
Code:
 char n,p;

 // <snip>

 scanf("%c %c",&n,&p);
"char"s hold single characters, not strings.

http://duckduckgo.com/?q=c+pointer+tutorial&v=

EDIT: Here's a partial explanation I wrote in another thread:

Quote:
Originally Posted by MTK358 View Post
Basically, a plain "char" holds one character (not a string). A "char*" contains a memory address. Why "char" before the asterisk, then? That means that when you "dereference" the pointer (get the value at the memory address it stores), it will be assumed that it's a "char" (You can use "void*" to just refer to a memory address without specifying any type).

So what does this have to do with strings? The idea is that a string is a sequence of "char"s in memory (with a NULL character at the end to indicate the end of the string), one after another, and the "char*" points to the first one. To get a character at the specified index, you add the character's index to the pointer and dereference it:

Code:
second_char = *(str + 1);
(The "*" operator dereferences a pointer)

And this is so common, that there's special syntax for it:

Code:
second_char = str[1];
(This is identical to the above example)

Last edited by MTK358; 07-04-2011 at 01:00 PM.
 
1 members found this post helpful.
Old 07-04-2011, 01:04 PM   #29
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
So what you need to do is this:

Code:
char n[100];
char p[100];

scanf("%s, %s", n, p);
The [n] syntax when creating a variable actually puts n amount of varaibles, one after another, on the stack.
 
Old 07-04-2011, 01:15 PM   #30
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
conio.h sounds like something out of the DOS world. For Linux you will need to use the termios API and/or the ncurses library instead. Time to start learning how to link with external libraries. For details, consult your local man pages.

--- rod.
 
  


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 09:40 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