whether "abc"[0] in standard C

C

ccwork

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?
 
C

Chris Dollin

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. It's just array indexing.
 
S

Serve Lau

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
 
K

Krishanu Debnath

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, even ans["yes"] is valid too.

Krishanu
 
C

Clark S. Cox III

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]);
 
C

ccwork

Hi all,
Thanks for all :)
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]);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top