Some separated questions...

This is a discussion on Some separated questions... within the C Programming forums, part of the General Programming Boards category; Hello. I have some questions.... I think that is not something important but if someone knows we can discuss it. ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    147

    Some separated questions...

    Hello. I have some questions.... I think that is not something important but if someone knows we can discuss it.

    The questions are :

    1) I have a signed type of char in my system.... Since the underlying sets of characters may vary from one system to another.... what is the values between -127... 0 ? I tried to print some of them -> printf("%c" , -127 ); but it doesn't work (I didn't get a meaningful output... I get a black '?' for example.

    2) A book says that we can perform arithmetic on boolean variables... but it doesn't give an example ( it is also not advisable to perform arithmetic on boolean variables as it writes...) . this -> printf(" %d " , false + 2 ) is an arithmetic operation with booleans ???

    3)
    Code:
    #include<stdio.h>
    
    int main(void)
    {
    	
    	int i , a[3] = {0};
    	
    	for(i = 0; i < 4 ; a[i++] = 2 )
    		printf(" %d ", i );
    
        
        for(i = 0; i<3; i++)
        printf(" %d ", a[i]);
       
         
    return 0;
    }
    I take infinite loop here and I can't understand why.... If I modify a[3] to a[4] and i<5 inside for I take no infinite loop :S I am confused. I believe that I have understand how for works....

    and the last one :P

    4)

    If I have an array with n elements
    like int a[n] (Variable length array) and a function

    int sum_array( int a[static 4] , int n)

    I think that It doesn't make any sense if I give 2 elements in my array.
    I have read that a presence of static is merely a hint that may allow a C compiler to generate faster instructions for accessing the array.

    Thank you in advance.....

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    1,940
    About question no.3 .I run the code and it was ok,by chance.You have an array a[3] and you have the i to get value 3.You must remember-learn that in arrays we start counting by zero,so a 3 position array,have cells in position 0,1,2

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    147
    @std10093 Thank you

    I know that. ( 0.... n-1) in arrays. But I didn't expect that it will produce infinite loop (in my system at least)
    I know about overflow.... a[3] is an overflow element... on this example.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    4,157
    Quote Originally Posted by Mr.Lnx View Post
    @std10093 Thank you

    I know that. ( 0.... n-1) in arrays. But I didn't expect that it will produce infinite loop (in my system at least)
    I know about overflow.... a[3] is an overflow element... on this example.
    Yes, it's an overflow. It may be that, by chance, overflowing your array into a[3] actually overwrites the value of i, making it 2, which is less than 4 and creates the infinite loop. In std10093's case, it appears that overflowing a[3] did not overwrite i, thus no infinite loop. You can't guarantee the order of variables on the stack (and some compilers randomize them or insert "canaries" specifically to protect against buffer overflow attacks), and even if you could guarantee stack order, array overflows are undefined behavior, so anything is allowed to happen.

    As for your other questions:
    1. This probably has something to do with how printf treats the %c conversion specifier, and integer type promotion. Don't have time to read through the standard right now, but you can, here: C Draft Standards. The first link on there should suffice.
    2. Hard to say exactly what is going on without seeing the example from your book. It's common however that boolean variables are simply declared as some type of integer value. C uses zero as false and non-zero as true. Thus, if you declare int bool_var; and set bool_var = 0; for false, you can do bool_var += 2; and get a value of 2 (true) in there. The book is right, it is not recommended to do this, it's too easy to screw up. Stick to the logical operators ! && ||, and teh comparators == != < <= > >=.
    4. I'm not even sure that it allows faster generation of instructions. In function parameters, a 1-d array is always treated as a pointer to the first element, even if you pass it a size. At least, I think so. Seems supported by this: Question 6.21
    Last edited by anduril462; 08-29-2012 at 11:55 AM.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    147
    Quote Originally Posted by anduril462 View Post
    Yes, it's an overflow. It may be that, by chance, overflowing your array into a[3] actually overwrites the value of i, making it 2, which is less than 4 and creates the infinite loop. In std10093's case, it appears that overflowing a[3] did not overwrite i, thus no infinite loop. You can't guarantee the order of variables on the stack (and some compilers randomize them or insert "canaries" specifically to protect against buffer overflow attacks), and even if you could guarantee stack order, array overflows are undefined behavior, so anything is allowed to happen.
    I understand ! Thank you.


    Quote Originally Posted by anduril462 View Post

    2. Hard to say exactly what is going on without seeing the example from your book. It's common however that boolean variables are simply declared as some type of integer value. C uses zero as false and non-zero as true. Thus, if you declare int bool_var; and set bool_var = 0; for false, you can do bool_var += 2; and get a value of 2 (true) in there. The book is right, it is not recommended to do this, it's too easy to screw up. Stick to the logical operators ! && ||, and teh comparators == != < <= > >=.
    I understand what you want to say but
    I was thinking something like that ->

    Code:
    #include<stdio.h>
    #include<stdbool.h>
    
    int main(void)
    {
    	
    	_Bool tmp = 1;
    	
    	/* tmp += 2;
    	 
    	 printf(" %d " , tmp);// always 1 for this case. Not 3  */
    	 
    	 tmp = false + 1;
    	 
    	 printf(" %d " , tmp); 
          
    return 0;
    }
    The book has no some example in which explains what does it mean with the arithmetic on boolean variables....
    I simply wanted to see what it means :P maybe it means what you say before....

    Quote Originally Posted by anduril462 View Post
    4. I'm not even sure that it allows faster generation of instructions. In function parameters, a 1-d array is always treated as a pointer to the first element, even if you pass it a size. At least, I think so. Seems supported by this: Question 6.21
    Well , I think is not sure the fact that compiler will produce faster instructions.....

    The book is King's Book.... PAGE 85 for boolean and 200 for the presence of static .

    C programming a modern approach second edition

    It is also writen that is not sure ... compiler may generate faster instructions... and it has no effect of the behaviour of the programm...
    I can understand that if you have an VLA with 4 elements from the user (in the runtime) and you have declared [static 10] in the function as we saw before there is no effect.
    Last edited by Mr.Lnx; 08-29-2012 at 12:53 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,234
    +2 has no meaning on Boolean (maybe futility?). Bools are either 1 or 0 (by convention), and more specifically, any number (even a negative one), or a zero. So +2 changes nothing on their status - still TRUE.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,036
    1) The symbol associated with the character value depends on the font, the character encoding that is in use and the code page etc. If your system prints them as '?' then that's fine. If you needed it to print something else then we'd probably be having a different conversation.

    2) Basically anything that talks about boolean arithmetic sounds pretty dodgy. You need at least C99 to have a bool type, and then such code will compile, but it's nonsense to add say 2 to false. So I would not waste time doing it.

    3) You have a buffer overrun, so expect the unexpected.

    4) 'static' is never merely a hint, but it has nothing to do with generating faster code for accessing an array. Please show us what you're read because you seem to have gotten it completely wrong. I can't see how you've gotten such mixed up information. What you're doing isn't valid syntax.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,807
    I concur with the uses of 'boolean' variables. It's just a concept in plain 'C'. 'true' and 'false' are represented by non-zero and zero respectively. In other languages you may have a dedicated 'bool' type which restricts the values the variable can take on.
    So given that any integer in C can mean true or false depending on its non-zero vs. zero value, yes you can do math on it. You can name a vaiable 'false' if you like but it's just another integer.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    19,420
    Quote Originally Posted by Mr.Lnx
    It is also writen that is not sure ... compiler may generate faster instructions... and it has no effect of the behaviour of the programm...
    I can understand that if you have an VLA with 4 elements from the user (in the runtime) and you have declared [static 10] in the function as we saw before there is no effect.
    Checking the (previous version of the) standard:
    Quote Originally Posted by C99 Clause 6.7.5.3 Paragraph 7b
    If the keyword static also appears within the [ and ] of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as many elements as specified by the size expression.
    So yes, it does look like an optimisation hint.

    Quote Originally Posted by iMalc
    4) 'static' is never merely a hint, but it has nothing to do with generating faster code for accessing an array. Please show us what you're read because you seem to have gotten it completely wrong. I can't see how you've gotten such mixed up information. What you're doing isn't valid syntax.
    This is a rather obscure C99 feature.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    1,011
    Quote Originally Posted by iMalc View Post
    4) 'static' is never merely a hint, but it has nothing to do with generating faster code for accessing an array. Please show us what you're read because you seem to have gotten it completely wrong. I can't see how you've gotten such mixed up information. What you're doing isn't valid syntax.
    Here is the relevant part from the book (King: C Programming: A Modern Approach, 2nd edition, page 200:
    Using static in this way has no effect on the behavior of the program. The presence of static is merely a "hint" that may allow a C compiler to generate faster instructions for accessing the array. (If the compiler knows that an array will always have a certain minimum length, it can arrange to "prefetch" these elements from memory when the function is called, before the elements are actually needed by statements within the function.)
    So using static in the declaration of array parameters is a promise that the function will always be called with at least the specified amount of elements. Calling the function with an array containing less elements is undefined behavior.

    gcc, for example, doesn't use this hint yet.

    Bye, Andreas

  11. #11
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    147
    Thank you AndiPersti. I didn't know that it can produce Undefined behaviour..... (I'm talking about a[static 3 ] and you if you give 2 elements ).

    iMalc yes but I feel there is no meaning from this print it is little strange on the appearance :P anyway ....
    Last edited by Mr.Lnx; 08-30-2012 at 12:17 PM.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,036
    Quote Originally Posted by AndiPersti View Post
    Here is the relevant part from the book (King: C Programming: A Modern Approach, 2nd edition, page 200:

    So using static in the declaration of array parameters is a promise that the function will always be called with at least the specified amount of elements. Calling the function with an array containing less elements is undefined behavior.

    gcc, for example, doesn't use this hint yet.

    Bye, Andreas
    Holy crap!
    AH so I've never seen it before because MSVC is not C99 compliant and GCC doesn't use the hint yet. So I guess practically nobody uses it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read inputs separated by space
    By acpower in forum C Programming
    Replies: 2
    Last Post: 04-10-2012, 09:54 AM
  2. Multiple inputs separated by EOF
    By juice in forum C Programming
    Replies: 35
    Last Post: 12-28-2011, 01:56 AM
  3. pthread heap separated like stack??
    By mynickmynick in forum C++ Programming
    Replies: 1
    Last Post: 08-25-2008, 11:11 AM
  4. Reading a comma separated file
    By nwr in forum C Programming
    Replies: 11
    Last Post: 08-06-2006, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21