1,105,427 Community Members

usage of new keywords in c99

Member Avatar
Iam3R
Junior Poster
110 posts since Oct 2009
Reputation Points: 24 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

hi ,
i have read that c99 introduced:
inline,_Bool,restrict,_Complex,_Imaginary

i have used _Bool here but do no how to use remaining all
please any one used the above can help me.

int main()
{
    _Bool b;
      b=true;// giving error  true undeclared ,cant i use that way 
                 // with out using enums.
       b =1;
       if ( b )
           printf (" ok\n");
        else
           printf("No\n");
return 0;
}

Thanks,
Danian.

Member Avatar
dkalita
Posting Pro in Training
402 posts since Sep 2009
Reputation Points: 110 [?]
Q&As Helped to Solve: 61 [?]
Skill Endorsements: 0 [?]
 
1
 

include stdbool.h

The type is "bool"

e.g.

#include<stdbool.h>

bool b= true;

this works in my compiler(cc).

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
2
 

inline: Similar to C++'s inline keyword. It gives you the option of telling the compiler that you want inline expansion rather than real function object code. Inline functions are a safer and easier to use alternative to function macros:

static inline swap ( int *a, int *b )
{
  int save = *a;
  *a = *b;
  *b = save;
}

I'd recommend avoiding this feature. Not only are the rules somewhat tricky, the compiler isn't required to honor a hint for inlining. Further, if the compiler does honor the hint, it could be counter-productive and actually hurt performance instead of help it by increasing the memory footprint and causing thrashing.

_Bool: C99's boolean type. Using _Bool directly is only recommended if you're maintaining legacy code that already defines macros for bool, true, or false. Otherwise, those macros are standardized in the <stdbool.h> header. Include that header and you can use bool just like you would in C++.

#include <stdio.h>
#include <stdbool.h>

int main ( void )
{
  bool b = true;

  if ( b )
    printf ( "Yes\n" );
  else
    printf ( "No\n" );

  return 0;
}

restrict: The restrict keyword is an optimization hint. It says that a pointer is not aliased in the current scope, which means the underlying object is not accessed by another pointer. This hint gives the compiler extra leeway in making assumptions about the pointer and can result in better object code. I'd recommend avoiding this feature too. A lot of people have trouble understanding restrict, and that confusion isn't worth the potential for optimization, in my opinion.

/* src and dst won't alias each other */
void b_copy ( void *restrict src, void *restrict dst, size_t nbytes );

_Complex and _Imaginary: These are keywords for working with complex numbers in C99 (obviously). You can safely ignore them until you need complex numbers (which also saves me the trouble of typing up a tutorial).

Member Avatar
Iam3R
Junior Poster
110 posts since Oct 2009
Reputation Points: 24 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

inline: Similar to C++'s inline keyword. It gives you the option of telling the compiler that you want inline expansion rather than real function object code. Inline functions are a safer and easier to use alternative to function macros:

static inline swap ( int *a, int *b )
{
  int save = *a;
  *a = *b;
  *b = save;
}

I'd recommend avoiding this feature. Not only are the rules somewhat tricky, the compiler isn't required to honor a hint for inlining. Further, if the compiler does honor the hint, it could be counter-productive and actually hurt performance instead of help it by increasing the memory footprint and causing thrashing.

_Bool: C99's boolean type. Using _Bool directly is only recommended if you're maintaining legacy code that already defines macros for bool, true, or false. Otherwise, those macros are standardized in the <stdbool.h> header. Include that header and you can use bool just like you would in C++.

#include <stdio.h>
#include <stdbool.h>

int main ( void )
{
  bool b = true;

  if ( b )
    printf ( "Yes\n" );
  else
    printf ( "No\n" );

  return 0;
}

restrict: The restrict keyword is an optimization hint. It says that a pointer is not aliased in the current scope, which means the underlying object is not accessed by another pointer. This hint gives the compiler extra leeway in making assumptions about the pointer and can result in better object code. I'd recommend avoiding this feature too. A lot of people have trouble understanding restrict, and that confusion isn't worth the potential for optimization, in my opinion.

/* src and dst won't alias each other */
void b_copy ( void *restrict src, void *restrict dst, size_t nbytes );

_Complex and _Imaginary: These are keywords for working with complex numbers in C99 (obviously). You can safely ignore them until you need complex numbers (which also saves me the trouble of typing up a tutorial).

thanks for you replies

Question Answered as of 4 Years Ago by Narue and dkalita
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article