Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to understand this line of c ?

Reply
Thread Tools

How to understand this line of c ?

 
 
tmp123
Guest
Posts: n/a
 
      01-01-2006
fidlee wrote:
> Thanks for answering this question.That brings up my next question.
> What is the significance of argv and argc in main() ?


Hi,

A full declaration of main is:

int main (int argc, char *argv[]);

That means:

1) main returns an integer. How the operating system, the caller shell,
or any caller interprets this result, is specific of the aplication.

2) The second parameter is an array of pointers to chars, or, in usual
language, an array of strings. Each item of the array is one parameter
of the call to the program. The number of used elements in the array is
stored in argc. The first element is typically the program name.

For example, if in a shell console you execute program "foo" typing:

> foo 1 hello


means argc=3, argv[0]="foo", argv[1]="1", argv[2]="hello"

Kind regards.

 
Reply With Quote
 
 
 
 
fidlee
Guest
Posts: n/a
 
      01-01-2006

tmp123 wrote:
> fidlee wrote:
> > Roberto Waltman wrote:
> > > "lnzju" <(E-Mail Removed)> wrote:
> > > >main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1);}

> >
> > can someone please explain as to what the initialization part of the
> > for loop does. How does the underscore '_' play a part in the for loop?
> > (in the initialization and the putchar function)?

>
> Hi,
>
> 1) "_" can be the name of a variable. In this case, it is the name of
> the first parameter of the function main (usually called argc). If the
> program is called without arguments, it takes value 1. (to be
> practical, I will rename it to "i").
>
> 2) This variable is used as iterator of the for statement. It is
> decremented at init (it takes value 0) and increment at for continue
> condition (++).
>
> 3) It is used as index to char string. Taken into account that:
>
> a[b] <==> *(a+b)
>
> The expression i["1234"] <=> *(i+"1234") <=> "1234"[i] : i-th character
> of the string.
>
> Hope this answer was useful to you.
>
> Kind regards.


Thanks for answering this question.That brings up my next question.
What is the significance of argv and argc in main() ?

 
Reply With Quote
 
 
 
 
John Bode
Guest
Posts: n/a
 
      01-01-2006

tmp123 wrote:
> lnzju wrote:
> > main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1);}

>


[snip]

> * integer applied to string returns n-th element. (this is something I
> do not know why).
>


The expression a[i] is defined as *(a+i); since addition is
commutative, it doesn't matter whether a is the pointer and i is the
index, or if i is the pointer and a is the index. Therefore, in C,
array indexing is commutative, so a[i] and i[a] evaluate to the same
thing.

It's fun to explain this to Ada programmers and watch their heads
explode.

 
Reply With Quote
 
Denis Kasak
Guest
Posts: n/a
 
      01-01-2006
Jordan Abel wrote:
> On 2006-01-01, Randy Howard <(E-Mail Removed)> wrote:
>> BTW, where is stdio.h?

>
> The implicit declaration of putchar is correct.


And that same implicit declaration was deprecated in C99.
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      01-01-2006
On 2006-01-01, Denis Kasak <(E-Mail Removed)> wrote:
> Jordan Abel wrote:
>> On 2006-01-01, Randy Howard <(E-Mail Removed)> wrote:
>>> BTW, where is stdio.h?

>>
>> The implicit declaration of putchar is correct.

>
> And that same implicit declaration was deprecated in C99.


And becomes a constraint error, not UB.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-01-2006
"John Bode" <(E-Mail Removed)> writes:
[...]
> The expression a[i] is defined as *(a+i); since addition is
> commutative, it doesn't matter whether a is the pointer and i is the
> index, or if i is the pointer and a is the index. Therefore, in C,
> array indexing is commutative, so a[i] and i[a] evaluate to the same
> thing.
>
> It's fun to explain this to Ada programmers and watch their heads
> explode.


Speaking as a long-time Ada programmer, I don't recall my head
exploding when I learned this. The explanation is perfectly clear,
but the rule is a bit silly. Nothing would have been lost by allowing
pointer+integer and forbidding integer+pointer.

I suppose it goes back to the days when C didn't make such a strong
distinction between pointers and integers.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Randy Howard
Guest
Posts: n/a
 
      01-01-2006
Jordan Abel wrote
(in article <(E-Mail Removed)>):

> On 2006-01-01, Denis Kasak <(E-Mail Removed)> wrote:
>> Jordan Abel wrote:
>>> On 2006-01-01, Randy Howard <(E-Mail Removed)> wrote:
>>>> BTW, where is stdio.h?
>>>
>>> The implicit declaration of putchar is correct.

>>
>> And that same implicit declaration was deprecated in C99.

>
> And becomes a constraint error, not UB.


I repeat my original question, how many UB's does one need? I
never said the above was UB either.

--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw





 
Reply With Quote
 
Dik T. Winter
Guest
Posts: n/a
 
      01-02-2006
In article <(E-Mail Removed) t> Randy Howard <(E-Mail Removed)> writes:
....
> >> "lnzju" <(E-Mail Removed)> wrote in message
> >> news:(E-Mail Removed) oups.com...
> >>> main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1);}

....
> > Well it ain't rubbish cause it is valid C!!

>
> Not really no.


What is wrong with it except the one argument main?
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
 
Reply With Quote
 
Dik T. Winter
Guest
Posts: n/a
 
      01-02-2006
In article <(E-Mail Removed) t> Randy Howard <(E-Mail Removed)> writes:
....
> >>>> "lnzju" <(E-Mail Removed)> wrote in message
> >>>> news:(E-Mail Removed) oups.com...
> >>>>> main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1);}

....
> How many examples of UB must be present in order to obtain such
> a license?
>
> BTW, where is stdio.h?


Not needed for "putchar", that is not a variadic function.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
 
Reply With Quote
 
Dik T. Winter
Guest
Posts: n/a
 
      01-02-2006
In article <(E-Mail Removed)> Keith Thompson <(E-Mail Removed)> writes:
....
> Speaking as a long-time Ada programmer, I don't recall my head
> exploding when I learned this. The explanation is perfectly clear,
> but the rule is a bit silly. Nothing would have been lost by allowing
> pointer+integer and forbidding integer+pointer.
>
> I suppose it goes back to the days when C didn't make such a strong
> distinction between pointers and integers.


No, it goes back to the days when operators like "+" where commutative.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Read a file line by line and write each line to a file based on the5th byte scad C++ 23 05-17-2009 06:11 PM
How to read a text file line by line and remove some line kaushikshome C++ 4 09-10-2006 10:12 PM
Read all of this to understand how it works. then check around on otherRead all of this to understand how it works. then check around on other thelisa martin Computer Support 2 08-18-2005 06:40 AM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM



Advertisments