Exforsys

Home arrow Technical Training arrow C Tutorials

C Special Operators

Page 6 of 6
Author: Exforsys Inc.     Published on: 30th Mar 2006    |   Last Updated on: 20th Jun 2011

C Programming - Operators

C Special Operators

There are a few other operators that are used in C. The [] operator is used to access elements of an array. The syntax is like this:

int a[5] = { 1, 2, 3, 4, 5 };
int b = a[3];

Ads

On the second line the a[3] accesses the fourth element of the array and assigns b to the value 4. Arrays will be covered in another tutorial.
The unary * operator is used to dereference pointers. For example:

int a = 1;
int *b = &a;
*b = *b + 1;

In this example b points to a. Using *b accesses the value stored in a. After these statements are run a will have the value 2. Pointers will be covered in another tutorial.
The '.' and '->' operators are used to access members of a structure or union. The '.' is used when the structure is accessible directly and the '->' is used when the structure is accessible through a pointer.

 #include <stdio.h>

Sample Code
  1.  #include <stdio.h>
  2.  
  3.  void main()
  4.  {
  5.          struct person
  6.          {
  7.                  char *firstname;
  8.                  char *lastname;
  9.                  int   age;
  10.          };
  11.          struct person a = { "Bob", "Smith", 30 };
  12.          struct person * b = &a;
  13.  
  14.          printf( "a.firstname = %sn", a.firstname );
  15.          printf( "b->firstname = %sn", b->firstname );
  16.  }
Copyright exforsys.com


a.firstname = Bob
b->firstname = Bob

Structures and unions will be covered in another tutorial.
Another little used operator is the ',' (comma) which takes two operands. The left side is evaluated first, then there is a sequence point then the right side is evaluated. The return value and type of the ',' operator is the value and type of the right side of the comma.

Sample Code
  1.    #include <stdio.h>
  2.    
  3.    void main()
  4.    {
  5.            int a = 1;
  6.            float b;
  7.    
  8.            b = ( a++, a * 3.1 );
  9.            printf( "a = %d, b = %fn", a, b );
  10.   }
Copyright exforsys.com


a = 2, b = 6.200000

On line 8, a is postfix incremented and because of the sequence point the updated value is stored back in a. Then the right side is evaluated and assigned to b.

The sizeof operator returns the size in bytes of that its operand takes up. The operand can be any expression that is not a function or bitfield, which means it can be an expression like 20 + 1.34 or a type name or a variable name or a constant. The size is returned as an int. Here are some examples:

Ads

Sample Code
  1.  #include <stdio.h>
  2.  
  3.  void main()
  4.  {
  5.          unsigned int a = 3;
  6.          struct record
  7.          {
  8.                  char  string[30];
  9.                  long  num1;
  10.                  short num2;
  11.          } arec;
  12.          struct record * recptr = &arec;
  13.  
  14.          printf( "a char takes %d bytesn", sizeof( char ));
  15.          printf( "an int takes %d bytesn", sizeof( a ));
  16.          printf( "a double takes %d bytesn", sizeof( 3.14159 ));
  17.          printf( "a float takes %d bytesn", sizeof( 3.14159f ));
  18.          printf( "a record structure takes %d bytesn", sizeof( arec ));
  19.          printf( "a pointer takes %d bytesn", sizeof( recptr ));
  20.  }
  21.  
Copyright exforsys.com


The output is:

a char takes 1 bytes
an int takes 4 bytes
a double takes 8 bytes
a float takes 4 bytes
a record structure takes 40 bytes
a pointer takes 4 bytes

Line 14 uses sizeof with a type name. Line 15 uses a variable and line 16-17 use a constant. Line 18 shows that sizeof works on user defined types like structures and unions too.



 
This tutorial is part of a C Tutorials tutorial series. Read it from the beginning and learn yourself.

C Tutorials

 

Comments