Why there's variables defined inside the main??

akhilc47

Journeyman
I am new to c programming and my prof has given me an assignment to do. He has given a stub code in which I have to add some more lines to complete it. I have a very basic knowledge of C programming and I've always started with main(). But first line of prof was like this: int main(int argc, char **argv). Why there's arguments inside the main?Could anyone please explain? I am reading a basic book "Let us C" which you guys are against(just read a few posts here), so what would be a good book to start? Any help would be much appreciated.
 

deepakkrishnan

Broken In
^ as Harshil mentioned.

just an example

the command "DIR" displays the directory structure. Now at the same time "DIR /P" is also displays the directory structure but in a different way. Now " /P" is an additional argument that is provided to to the "DIR" command.

If you want the same kind of functionality in your program then you'll have to pass these arguments to the program when they are executed. And to do that you require arguments in the main() func.

Hope I was clear.. :)
 
Last edited:

Nue

...
Those are called command line arguments ; argc ("argument count") is the number of command line arguments (including the name of the program itself). argv ("argument vector") is an array of pointers to the command line arguments, which are stored in string from. I'm hoping you're familiar with arrays and pointers otherwise this wouldn't make any sense.
 
OP
akhilc47

akhilc47

Journeyman
I have used pointers and arrays in programs. But I don't understand this "passing arguments to program". May be I should google more. Can you suggest a good book with simple examples and all. Thank you very much.
 

Santa Maria!

Journeyman
I have used pointers and arrays in programs. But I don't understand this "passing arguments to program". May be I should google more. Can you suggest a good book with simple examples and all. Thank you very much.

If you are even 20% serious about learning C properly, ditch 'Let us C' and get this book:
The C Programming Language: Ansi C Version 2 Edition - Buy The C Programming Language: Ansi C Version 2 Edition by Kernighan, Brian W.|Author; Ritchie, Dennis M.|Author; Online at Best Prices in India - Flipkart.com
 
OP
akhilc47

akhilc47

Journeyman
If you are even 20% serious about learning C properly, ditch 'Let us C' and get this book:
The C Programming Language: Ansi C Version 2 Edition - Buy The C Programming Language: Ansi C Version 2 Edition by Kernighan, Brian W.|Author; Ritchie, Dennis M.|Author; Online at Best Prices in India - Flipkart.com
Thanks for the suggestion seems like a great book. But all the reviews in flipkart are saying excellent book but not for beginners!. Will I be able to understand this book? The only coding I've done before is in matlab in which almost all the functions are readily available.
 

deepakkrishnan

Broken In
The mentioned book is to the point and no crap. It like "Let us C" on steroids. You'll have to start reading from the 1st chapter and don't skip or skim this book, you won't make head or tail of it if you do that.
 

vickybat

I am the night...I am...
Thanks for the suggestion seems like a great book. But all the reviews in flipkart are saying excellent book but not for beginners!. Will I be able to understand this book? The only coding I've done before is in matlab in which almost all the functions are readily available.

I suggest you to skip that book if you are a beginner. It won't encourage you to code. Get this eyes closed instead:

Head First C - Buy Head First C by David|Author;Griffiths, Dawn|Author; Online at Best Prices in India - Flipkart.com

Its simply fantastic for beginners and i assure you that you won't regret one bit.
 

krishnandu.sarkar

Simply a DIGITian
Staff member
I suggest you to skip that book if you are a beginner. It won't encourage you to code. Get this eyes closed instead:

Head First C - Buy Head First C by David|Author;Griffiths, Dawn|Author; Online at Best Prices in India - Flipkart.com

Its simply fantastic for beginners and i assure you that you won't regret one bit.

Same here. Believe me, I have read all the 3 Let Us C (in college), Dennis M. Ritchie and Head First C.

I won't recommend Ritchie to a newbie. It'll confuse a beginner more.

Instead get Head First C and you'll like the approach. It's really a great book.

Infact all the Head First series are good, it describes things slowly and to the point. And we all know pictures are worth 1000's of words, and that's what they follow in Head First Series.

I have seen that Ritchie book starts explaining pointers from very beginning, which will ofcourse going to confuse newbie. C is a great language and capable of doing great things as we all know.

But if you can't grasp it correctly, it can become a nightmare for you.
 

ico

Super Moderator
Staff member
I'd say get both.

Head First C and The C Programming Language. The latter is actually quite cheap and available for Rs. 80. Makes no harm. :)
 

Santa Maria!

Journeyman
Thanks for the suggestion seems like a great book. But all the reviews in flipkart are saying excellent book but not for beginners!. Will I be able to understand this book? The only coding I've done before is in matlab in which almost all the functions are readily available.

You (and everyone else in this thread) are right in that it's not strictly a book for a beginner to programming. It's a small book with an immense amount of valuable information. I haven't read it in its entirety, but much like Stroustrup's book on C++, almost every sentence has something valuable, so if your attention wavers for even a moment, you might miss something important :)

While you could grit your teeth and slowly make your way through K&R by reading slowly, since you say that your only programming experience is a bit of MATLAB, I concede that you do what was suggested and what is usually done - get a beginners book, but also definitely keep K&R for your next stage of enlightenment when you advance to the next level.
 
Top Bottom