C Tutorial

C Tutorial C Language Environment Setup Execution flow of C program C printf and Scanf C Data type C Token Variable in C Operators in C Comments in C Escape Sequence in C C – Storage Classes C Decision control statement Loop Statement in C Break, continue and goto statement in C Type Casting in C Function in C Recursion in C String in C C Array Pointer in C Dynamic memory allocation C –Structure Nested Structure in C Union in C File Handling in C C pre-processor Static Function In C Sizeof In C Selection Sort In C Scope Of Variables In C Runtime Vs Compile Time In C Random Access Lseek In C Queue Implementation In C Pseudo Code In C Prototype In C Pointer To Pointer In C Pointer Arithmetic In C Passing Array To Function In C Null Character In C Merge Sort In C Macros In C Library Functions In C Memory Leak In C Int In C Goto And Labels In C Fibonacci Series In C Fflush In C Derived Data Types In C Data Types In C Const Vs Volatile In C Character Set In C Character Class Tests In C Calloc In C C Pointers Arrays In C Include In C Clrscr In C C Vs Java String Literals In C Types Of Pointers In C Variables In C Volatile In C Why C Is A Middle Level Language Infix To Postfix Program In C Ceil function in C LCM of two numbers in C Quick sort in C Static in C function pointer as argument in C Top Array Keywords in C Add two numbers using the function in C Armstrong program in C using function Array, Declaring Arrays and Array Initialization Limitations of Inline Function in C Merge and Merge sort with example in C Do-While Loop in C For Loop in C While-Loop in C Difference between while and do-while loop in C Array Of Structures in C Data Structures And Algorithms in C Types Of Structures In C How to Avoid Structure Padding in C Use of Structure in C Do WHILE LOOP in C Programming Examples For Loop in C Programming Examples Entry Control Loop in C Exit control loop in C Infinite loop in C Nested loop in C pow() function in C String Handling functions in C Prime Number code in C Factorial Program in C using For Loop Factorial Program in C Using While Loop Fibonacci Series in C Using For Loop Fibonacci series in C using while loop Prime Number Program in C using for Loop While Loop in C programming examples Built-in functions in C Assert() Function C vs Java Strings Call Back Function in Embedded C Else If Ladder fgets() function Ftell() Function getc() function getch() function gets() function Heap Sort Nested if-else statement Pi() Function Positioning of file Write() function abs() function in C Attributes in C C program to find factorial of a number using Recursion Ferror() in c fopen() function in C Fibonacci series program in C using Recursion Formatted Input and output function in C Snake Game in C User Defined Functions in C Beep() function in C Cbrt() function in C Hook() function in C Isalnum() function in C C Program to find the Roots of a Quadratic Equation C Switch Statements Difference between rand() and srand() function in C Difference between while and for loop in C Doubly Linked list in C Example of Iteration in C How to use atoi() function in C How to use floor() function in C How to use sine() function in C How to use Typedef Struct in C Integer Promotions in C C Program to Find Largest Number Using Dynamic Memory Allocation C Program to Find the Largest Number using Ternary Operator C/C++ Program to Find the Size of int, float, double and char Find the Largest Three Distinct Elements in an Array using C/C++ Loop Questions in C Modulus on Negative Numbers in C Multiplication table program in C using For loop Nested Loops in C Programming Examples C Program for Mean and Median of an Unsorted Array Results of Comparison Operations in C and C++ Reverse a Stack using Recursion in C Simple hash() function in C strcat() Function in C Sum of N numbers in C using For loop Use of free() function in C Write a program that produces different results in C and C++

Attributes in C

Attributes are one of modern C++ key features. It is the process by which initiator can add more information to the language instead of introducing new keywords for every attribute. It helps to decorate a code accomplishment in C, and C++ languages. Some of these attributes are written in standard syntax[[at]] and some are in non-standard syntax.

Syntax:

identifier: [[indication]]

Except some specific ones, almost all the attributes can be adapted with functions, classes structures etc.

Purpose of attributes in C++

Conditions to be imposed on code:

To meet the execution needs, the constraints in the C language are referred to a condition and in the updated versions of C++, the code for describing conditions is written in this way.

int f (int i)
{
if (i>0)
           return i;
else:
           return -1;
       //code
}

It enhances the code understandability,and it avoids the misconception in the code that is written inside the code function for the checking of the arguments.

int f (int i) [[expects: i>0]]
              {
                    //code
}

To provide extra information to the compiler for expansion purposes:

Compilers are good at expansion but compare to humans; they lag at some places and propose a code which is insufficient. The main reason behind this the information which is to get on has been lacked off. about the problem which humans have. To overcome this problem, the C++ has introduced a new way with adding new attributes to it and the code for the implementation of the attributes is:

int f (int i)
{
      switch(i) {
      case 1:
           [[fall through]];
           [[likely]] case 2: return 1;
       }
       return -1;
  }

The overall performance of the code totally depends on the compiler. When the statements are headed by the compiler, the performance automatically improves. Examples of some such attributes are:

For example:

[carries_ dependency], [likely], [unlikely]

Overcoming some of the warnings and errors that the users have in the code

When a user deploy a code, in some cases, the user intentionally overwrites the wrong code with some mistakes and it results in errors which is identified by the complier and one such case arises in the switch and break statements

For example:

#include<iostream>
#include <string>


int main ()
{
       //setting debugging mode in compiler or ‘R’
       [[maybe unused]] char mg_brk = ‘D’;


      //compiler does not release errors on unused variables
}

List of standard attributes in C++

C++11

1. no return:This indicates function that doesn’t return value

Usage:

[[no return]] void f ();

If a function contains void type, it returns to caller without any value but in the case of an infinity loop, it does not return to caller but adding a no return provides hints to compiler to expand code.

Following is an example code which provides clearcut information,

#include<iostream>
           #include<string>
           [[no return]] void f ()
           {


}
void g ()
             {
std: count << “code is intended to reach here”;
}
               int main ()
               {
                     f ();
                      g ();
                 }

Warning:

main.cpp: In function ‘void f ()’ :

     main.cpp: warning: ‘no return’ function does return

           }

C++14

2. deprecated: It indicates that name declared with this attribute became antiquated and must not be used for specific reason. It can be applied to namespace, functions, and classes.

Usage:

[[deprecated (“reason for deprecation”)]]


           // for class/struct/union
struct [[deprecated]] s;


//for functions
           [[deprecated]] void f ();


           //for namespaces
Namespace [[deprecated]] ns {}


//for variables 
           [[deprecated]] int x;

Example:

#include<iostream>
                 #include<string>
                 [[deprecated (“Susceptible to buffer overflow”)]] void gets(char* str)
                 {


                  }
void gets-n(std::string& str)
{
                          char st[100];
std::cout<< “Successfully Executed”;
std::cin.getline(st, 100);
str= std:: string(st);
                     }
                     int main ()
{
                              char a [100];
                               gets (a);
                     }

Warning:

main.cpp: in function ‘int main ()’ :

                      main.cpp: warning: ‘void gets(char*)’ is deprecated:

         Susceptible to buffer overflow [-Wdeprecated-declarations]

                                             gets (a);

C++17

3. no discard: The difference between the no discard with function and no discard with class is that, in case of function no discard is applied to function which declares as no discard but in class, it is applied to each function which return no discard entity by value.

Usage:


// functions
      [[no discard]] void f ();


      //class/struct declaration
       Struct [[no discard]] my -struct {};

Example:

#include<iostream>
#include<string>


       //value which returns must be used by caller
       [[no discard]] int f ()
        {
                      return 0;
 }  


            class [[no discard]] my _ class {};
            my _ class fun ()
            {
return my _ class ();
             }


int main ()
              {
                        int x {1};


                        // as value is used no error
                       // x=f();


                      // error: value is not used
                       f ();
                       // value not used error
                       // fun ();


                       return x;
}

Warning:

prog.cpp:warning: ‘no discard’ attribute ignores [-Wattributes]

          [[no discard]] int f ()

    prog.cpp: warning: ‘no discard’ attribute directive ignores [-wattributes]

            class [[no discard]] my _ class {};

4. maybe _ unused: It isused to summarize warnings on any unused object.

    Usage:

//variables
          [[ maybe_ used]] bool log _ var = true;
         //functions
         [[maybe_ unused]] void log_ without_ warning ();


         //function arguments
         Void f ([[maybe_ unused]] int a, int b);

Example:

#include<iostream>
          #include<string>
          int main ()
          {
[[maybe_ unused]] char mg_brk = ‘D’;


           }



ADVERTISEMENT
ADVERTISEMENT