Need help with C Language

Discussion in 'C Programming' started by vinod.bhavnani@gmail.com, Aug 1, 2006.

  1. Guest

    Hello all,

    I have 2 qs about statements and their meanings in the C language.

    first If i have an array named

    int a[256]

    and if i use a staement like

    for(i=0;i<256;i++)
    If(a)
    {
    }
    how will this loop work? when will the IF loop be true and when will it
    be false

    the next qs is have is

    :
    if i have a statement array[j++]=3*bar;
    what exactly is happ to j
    does each time the loop increments and a new row of j is beaing
    assigned a new value or what is happ


    Thanks,
    Vinod
    , Aug 1, 2006
    #1
    1. Advertising

  2. wrote:
    > first If i have an array named
    >
    > int a[256]
    >
    > and if i use a staement like
    >
    > for(i=0;i<256;i++)
    > If(a)
    > {
    > }
    > how will this loop work? when will the IF loop be true and when will it
    > be false
    >

    First, the nit-picking: This snippet will not compile, probably. There
    is no standard keyword "If" in standard C.

    The answer is whether or not the a expression resolves to nonzero or
    not. What is the value contained in a? Only you know at this point.

    > if i have a statement array[j++]=3*bar;
    > what exactly is happ to j
    > does each time the loop increments and a new row of j is beaing
    > assigned a new value or what is happ
    >

    Post-increment. Check the c.l.c. FAQ <http://c-faq.com/>
    Clever Monkey, Aug 1, 2006
    #2
    1. Advertising

  3. said:

    > Hello all,
    >
    > I have 2 qs about statements and their meanings in the C language.
    >
    > first If i have an array named
    >
    > int a[256]
    >
    > and if i use a staement like
    >
    > for(i=0;i<256;i++)
    > If(a)


    C is a case-sensitive language. The 'if' keyword has a lower case i.

    > {
    > }
    > how will this loop work? when will the IF loop be true and when will it
    > be false


    It depends on the value of a. Since you didn't assign any values, the
    value is indeterminate. But let's say you do something like this:

    for(i = 0; i < 256; i++)
    {
    a = i % 2;
    }

    and then this:

    for(i = 0; i < 256; i++)
    {
    if(a)
    {
    printf("a is true for %d\n", i);
    }
    }

    then it will print:

    a is true for 1
    a is true for 3
    a is true for 5
    ...
    all the way down to
    ...
    a is true for 253
    a is true for 255

    > the next qs is have is
    >
    > :
    > if i have a statement array[j++]=3*bar;
    > what exactly is happ to j
    > does each time the loop increments and a new row of j is beaing
    > assigned a new value or what is happ


    In the example you give,

    array[j++] = 3 * bar;

    can be re-written as:

    array[j] = 3 * bar;
    j = j + 1;

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Aug 1, 2006
    #3
  4. Ronald Bruck Guest

    In article <>,
    <> wrote:

    > Hello all,
    >
    > I have 2 qs about statements and their meanings in the C language.
    >
    > first If i have an array named
    >
    > int a[256]
    >
    > and if i use a staement like
    >
    > for(i=0;i<256;i++)
    > If(a)
    > {
    > }
    > how will this loop work? when will the IF loop be true and when will it
    > be false
    >
    > the next qs is have is
    >
    > :
    > if i have a statement array[j++]=3*bar;
    > what exactly is happ to j
    > does each time the loop increments and a new row of j is beaing
    > assigned a new value or what is happ


    I am often amazed at reading questions like this. I remember when I
    was learning C (and Fortran, and Mathematica, and...) When I had a
    question like this, I'd program up a little example, change the
    parameters, and see what I got. It may not be what the manual strictly
    says it should be, but that's an even MORE informative fact about the
    compiler (or other program). You learn best by figuring it out
    yourself.

    (I particularly remember puzzling through the copy protection in
    Visicalc--for my own edification and admiration, not for nefarious
    purposes--before realizing it was exploiting a BUG in the 6502
    instruction set! What the code loop DID was different from what the
    6502 designers promised it WOULD do.)

    Has the habit of experimentation died? Has the Internet made us all
    lazy?

    In this case, of course, he has to put something IN his if... statement
    to distinguish what happens. (And I suggest he NOT make it an If
    statement, as posted. But he'd probably figure that out real quick.)
    As for the j++ question, ... Oh, I give up. Hint: why is j++ called
    POST increment? Second hint: it's not a USENET post.

    --
    Ron Bruck

    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    http://www.usenet.com
    Ronald Bruck, Aug 1, 2006
    #4
  5. Guest

    hey ,

    Thanks for all yr help .The actual piece ofcode is below:_ I was
    wondering if someone could explain whats going on in the code below

    int do_mval=0;

    if( mask_dset == NULL ){
    mmm = (byte *) malloc( sizeof(byte) * nvox ) ;
    if( mmm == NULL )
    return " \n*** Can't malloc workspace! ***\n" ;
    memset( mmm , 1, nvox ) ; mcount = nvox ;
    } else {

    mmm = THD_makemask( mask_dset , miv , mask_bot , mask_top ) ;
    if( mmm == NULL )
    return " \n*** Can't make mask for some reason! ***\n" ;
    mcount = THD_countmask( nvox , mmm ) ;


    /*-- allocate an array to histogrammatize --*/

    flim = mri_new( mcount , 1 , MRI_float ) ;
    flar = MRI_FLOAT_PTR(flim) ;

    /*-- load values into this array --*/

    switch( DSET_BRICK_TYPE(input_dset,iv) ){
    default:
    free(mmm) ; mri_free(flim) ;
    return "*** Can't use source dataset -- illegal data type!
    ***" ;

    case MRI_short:{
    short * bar = (short *) DSET_ARRAY(input_dset,iv) ;
    float mfac = DSET_BRICK_FACTOR(input_dset,iv) ;
    if( mfac == 0.0 ) mfac = 1.0 ;
    if( do_mval ){
    float val ;
    for( ii=jj=0 ; ii < nvox ; ii++ ){
    if( mmm[ii] ){
    val = mfac*bar[ii] ;
    if( val >= val_bot && val <= val_top ) flar[jj++] =
    val ;
    }
    }
    mval = jj ;
    } else {
    for( ii=jj=0 ; ii < nvox ; ii++ )
    if( mmm[ii] ) flar[jj++] = mfac*bar[ii] ;
    }
    }


    I understand most part of this code..Now the problem is

    What exactly is array mmm being assigned to if the maskdatset is not
    being assigned to it.
    is mmm being assigned to an array of all 1's

    if so
    could someone explain what exactly is happ in the switch case
    statement?

    like in case there is no maskdset and mmm array has been set to all 1's
    then what exactly does this piece of code do:
    I mean is it necessary to put if(mmm[ii] loop before assignment
    or is the if statement redudndant?


    for( ii=jj=0 ; ii < nvox ; ii++ )
    if( mmm[ii] ) flar[jj++] = mfac*bar[ii] ;

    Richard Heathfield wrote:
    > said:
    >
    > > Hello all,
    > >
    > > I have 2 qs about statements and their meanings in the C language.
    > >
    > > first If i have an array named
    > >
    > > int a[256]
    > >
    > > and if i use a staement like
    > >
    > > for(i=0;i<256;i++)
    > > If(a)

    >
    > C is a case-sensitive language. The 'if' keyword has a lower case i.
    >
    > > {
    > > }
    > > how will this loop work? when will the IF loop be true and when will it
    > > be false

    >
    > It depends on the value of a. Since you didn't assign any values, the
    > value is indeterminate. But let's say you do something like this:
    >
    > for(i = 0; i < 256; i++)
    > {
    > a = i % 2;
    > }
    >
    > and then this:
    >
    > for(i = 0; i < 256; i++)
    > {
    > if(a)
    > {
    > printf("a is true for %d\n", i);
    > }
    > }
    >
    > then it will print:
    >
    > a is true for 1
    > a is true for 3
    > a is true for 5
    > ...
    > all the way down to
    > ...
    > a is true for 253
    > a is true for 255
    >
    > > the next qs is have is
    > >
    > > :
    > > if i have a statement array[j++]=3*bar;
    > > what exactly is happ to j
    > > does each time the loop increments and a new row of j is beaing
    > > assigned a new value or what is happ

    >
    > In the example you give,
    >
    > array[j++] = 3 * bar;
    >
    > can be re-written as:
    >
    > array[j] = 3 * bar;
    > j = j + 1;
    >
    > --
    > Richard Heathfield
    > "Usenet is a strange place" - dmr 29/7/1999
    > http://www.cpax.org.uk
    > email: rjh at above domain (but drop the www, obviously)
    , Aug 1, 2006
    #5
  6. Ronald Bruck said:

    <snip>

    > Has the habit of experimentation died?


    Do your own sociology homework. :)

    > Has the Internet made us all lazy?


    I would answer that, but apathy prevents me.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Aug 1, 2006
    #6
  7. Ben Pfaff Guest

    Ronald Bruck <> writes:

    > I am often amazed at reading questions like this. I remember when I
    > was learning C (and Fortran, and Mathematica, and...) When I had a
    > question like this, I'd program up a little example, change the
    > parameters, and see what I got. It may not be what the manual strictly
    > says it should be, but that's an even MORE informative fact about the
    > compiler (or other program). You learn best by figuring it out
    > yourself.


    Sometimes experimentation is valuable. Other times, it's just
    misleading. What do you learn from an experiment that tests the
    behavior of "i = i++"? You learn how that kind of undefined
    behavior manifests on your compiler, today.
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
    Ben Pfaff, Aug 1, 2006
    #7
  8. On 2006-08-01, <> wrote:
    > Hello all,
    >
    > I have 2 qs about statements and their meanings in the C language.
    >
    > first If i have an array named
    >
    > int a[256]
    >


    You mean "if I have an array named `a'...", right? Because you can't
    have `int', spaces or brackets in a identifier name.

    > and if i use a staement like
    >
    > for(i=0;i<256;i++)
    > If(a)
    > {
    > }
    > how will this loop work? when will the IF loop be true and when will it
    > be false
    >


    Nope. It won't work.
    a) You don't have an "IF loop" in that code. There's no "IF loop" in C.
    b) What you do have, a "If" construct, isn't in C either.
    c) What you /should/ have, a "if statement", is not part of C.
    d) If you /did/ have a "if statement", it may never evaluate to false,
    because you haven't initialized the array. Reading an uninitialized
    array is Undefined Behavior, my friend.
    e) Where did you define i? It isn't a char, is it?

    > the next qs is have is


    Please say `question'. It takes two seconds.

    >:
    > if i have a statement array[j++]=3*bar;
    > what exactly is happ to j
    > does each time the loop increments and a new row of j is beaing
    > assigned a new value or what is happ
    >


    What loop? Where are array, j, bar, and i defined? "happ" is not a word.

    --
    Andrew Poelstra <website down>
    To reach my email, use <email also down>
    New server ETA: 42
    Andrew Poelstra, Aug 1, 2006
    #8
  9. On 2006-08-01, <> wrote:
    > hey ,
    >
    > Thanks for all yr help .The actual piece ofcode is below:_ I was
    > wondering if someone could explain whats going on in the code below
    >


    You are abusing the letter m. That's what's going on. ;-)

    > int do_mval=0;
    >
    > if( mask_dset == NULL ){
    > mmm = (byte *) malloc( sizeof(byte) * nvox ) ;

    Assume mmm is a pointer, use
    mmm = malloc (nvox * sizeof *mmm);

    > if( mmm == NULL )
    > return " \n*** Can't malloc workspace! ***\n" ;

    Why are you returning a string literal? Is your function defined to
    return a string literal? This snippit is not doing what you think it
    is. http://www.c-faq.com and buy a copy of _The C Programming Language_
    by Dennis Ritchie and Brian Kernighan (sp?).

    > memset( mmm , 1, nvox ) ; mcount = nvox ;

    That 1 would be better as "sizeof *mmm".

    > } else {
    >
    > mmm = THD_makemask( mask_dset , miv , mask_bot , mask_top );

    When you call functions you haven't defined, we can't tell you what
    happens. We aren't psychic.

    > if( mmm == NULL )
    > return " \n*** Can't make mask for some reason! ***\n" ;

    return sends a value back to the calling function. You know that, right?

    <snipped much of the same>
    >
    > I understand most part of this code..Now the problem is
    >


    I don't think you do.

    > What exactly is array mmm being assigned to if the maskdatset is not
    > being assigned to it.
    > is mmm being assigned to an array of all 1's
    >


    I dunno. What do all of your other functions do? What scope is mmm?



    Finally, some netiquette:
    1) Don't top post!
    2) Don't forget to snip signatures.
    Thanks for your consideration.

    --
    Andrew Poelstra <website down>
    To reach my email, use <email also down>
    New server ETA: 42
    Andrew Poelstra, Aug 1, 2006
    #9
  10. On 2006-08-01, Andrew Poelstra <> wrote:
    > On 2006-08-01, <> wrote:
    >> Hello all,
    >>
    >> I have 2 qs about statements and their meanings in the C language.
    >>
    >> first If i have an array named
    >>
    >> int a[256]
    >>

    >
    > You mean "if I have an array named `a'...", right? Because you can't
    > have `int', spaces or brackets in a identifier name.
    >
    >> and if i use a staement like
    >>
    >> for(i=0;i<256;i++)
    >> If(a)
    >> {
    >> }
    >> how will this loop work? when will the IF loop be true and when will it
    >> be false
    >>

    >
    > Nope. It won't work.
    > a) You don't have an "IF loop" in that code. There's no "IF loop" in C.
    > b) What you do have, a "If" construct, isn't in C either.
    > c) What you /should/ have, a "if statement", is not part of C.

    Ack! I meant "is not a loop". Sorry for the confusion.

    > d) If you /did/ have a "if statement", it may never evaluate to false,
    > because you haven't initialized the array. Reading an uninitialized
    > array is Undefined Behavior, my friend.
    > e) Where did you define i? It isn't a char, is it?
    >
    >> the next qs is have is

    >
    > Please say `question'. It takes two seconds.
    >
    >>:
    >> if i have a statement array[j++]=3*bar;
    >> what exactly is happ to j
    >> does each time the loop increments and a new row of j is beaing
    >> assigned a new value or what is happ
    >>

    >
    > What loop? Where are array, j, bar, and i defined? "happ" is not a word.
    >



    --
    Andrew Poelstra <website down>
    To reach my email, use <email also down>
    New server ETA: 42
    Andrew Poelstra, Aug 1, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ed
    Replies:
    24
    Views:
    981
    Dimitri Maziuk
    Mar 27, 2006
  2. DaveInSidney
    Replies:
    0
    Views:
    402
    DaveInSidney
    May 9, 2005
  3. pabbu
    Replies:
    8
    Views:
    711
    Marc Boyer
    Nov 7, 2005
  4. Shravani
    Replies:
    8
    Views:
    780
    Bartc
    Mar 16, 2008
  5. Replies:
    28
    Views:
    1,148
Loading...

Share This Page