Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > whether "abc"[0] in standard C

Reply
Thread Tools

whether "abc"[0] in standard C

 
 
ccwork
Guest
Posts: n/a
 
      02-28-2005
Hi all,
I found some people writing code as:
/****************** sample code ********************/
int ans;
...
printf("%c\n", " yn"[ans]);
/***************** sample code end *****************/

The value of variable "ans" will be an index to print ' ', 'y' or
'n'. Is this a syntax " yn"[ans] in standard C?

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      02-28-2005
ccwork wrote:

> Hi all,
> I found some people writing code as:
> /****************** sample code ********************/
> int ans;
> ...
> printf("%c\n", " yn"[ans]);
> /***************** sample code end *****************/
>
> The value of variable "ans" will be an index to print ' ', 'y' or
> 'n'. Is this a syntax " yn"[ans] in standard C?


Yes. It's just array indexing.

--
Chris "electric hedgehog" Dollin
 
Reply With Quote
 
 
 
 
Serve Lau
Guest
Posts: n/a
 
      02-28-2005

"ccwork" <> wrote in message
news: oups.com...
> Hi all,
> I found some people writing code as:
> /****************** sample code ********************/
> int ans;
> ...
> printf("%c\n", " yn"[ans]);
> /***************** sample code end *****************/
>
> The value of variable "ans" will be an index to print ' ', 'y' or
> 'n'. Is this a syntax " yn"[ans] in standard C?


yes


 
Reply With Quote
 
Krishanu Debnath
Guest
Posts: n/a
 
      02-28-2005
ccwork wrote:

>Hi all,
> I found some people writing code as:
> /****************** sample code ********************/
> int ans;
> ...
> printf("%c\n", " yn"[ans]);
> /***************** sample code end *****************/
>
> The value of variable "ans" will be an index to print ' ', 'y' or
>'n'. Is this a syntax " yn"[ans] in standard C?
>
>
>

yes, even ans["yes"] is valid too.

Krishanu
 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      02-28-2005
On 2005-02-28 04:18:15 -0500, "ccwork" <> said:

> Hi all,
> I found some people writing code as:
> /****************** sample code ********************/
> int ans;
> ...
> printf("%c\n", " yn"[ans]);
> /***************** sample code end *****************/
>
> The value of variable "ans" will be an index to print ' ', 'y' or
> 'n'. Is this a syntax " yn"[ans] in standard C?


Yes. This might make more sense to you, if you consider that (" yn") is
nothing more than an array of characters. Your sample code is similar
to:

int ans;
const char array[] = " yn";
...
printf("%c\n", array[ans]);

--
Clark S. Cox, III


 
Reply With Quote
 
ccwork
Guest
Posts: n/a
 
      03-01-2005
Hi all,
Thanks for all

Clark S. Cox III wrote:
> On 2005-02-28 04:18:15 -0500, "ccwork" <> said:
>
> > Hi all,
> > I found some people writing code as:
> > /****************** sample code ********************/
> > int ans;
> > ...
> > printf("%c\n", " yn"[ans]);
> > /***************** sample code end *****************/
> >
> > The value of variable "ans" will be an index to print ' ', 'y'

or
> > 'n'. Is this a syntax " yn"[ans] in standard C?

>
> Yes. This might make more sense to you, if you consider that (" yn")

is
> nothing more than an array of characters. Your sample code is similar


> to:
>
> int ans;
> const char array[] = " yn";
> ...
> printf("%c\n", array[ans]);
>
> --
> Clark S. Cox, III
>


 
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
What are the standard network functions provided in standard C? disappearedng@gmail.com C Programming 5 06-10-2008 08:57 PM
add pexpect to the standard library, standard "install" mechanism. funkyj Python 5 01-20-2006 08:35 PM
whether is the standard input stream full buffered or line buffered after calling function setbuf()? kernelxu@hotmail.com C Programming 9 08-23-2005 02:24 PM
How standard is the standard library? steve.leach Python 1 04-18-2005 04:07 PM
How to check whether my GCC compiler support C99 standard or not? Peng Yu C++ 2 09-29-2004 04:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57