Technical Training
C TutorialsTable of Contents
C Programming - Operators
C Logical Operators
C Assignment Operators
C Conditional Operator
C Bitwise Operator
C Special OperatorsC Special Operators
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];
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>
- #include <stdio.h>
- void main()
- {
- struct person
- {
- char *firstname;
- char *lastname;
- int age;
- };
- struct person a = { "Bob", "Smith", 30 };
- struct person * b = &a;
- printf( "a.firstname = %sn", a.firstname );
- printf( "b->firstname = %sn", b->firstname );
- }
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.
- #include <stdio.h>
- void main()
- {
- int a = 1;
- float b;
- b = ( a++, a * 3.1 );
- printf( "a = %d, b = %fn", a, b );
- }
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:
- #include <stdio.h>
- void main()
- {
- unsigned int a = 3;
- struct record
- {
- char string[30];
- long num1;
- short num2;
- } arec;
- struct record * recptr = &arec;
- printf( "a char takes %d bytesn", sizeof( char ));
- printf( "an int takes %d bytesn", sizeof( a ));
- printf( "a double takes %d bytesn", sizeof( 3.14159 ));
- printf( "a float takes %d bytesn", sizeof( 3.14159f ));
- printf( "a record structure takes %d bytesn", sizeof( arec ));
- printf( "a pointer takes %d bytesn", sizeof( recptr ));
- }
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.
C Tutorials
- C Programming - An Overview
- C Programming - Data Types : Part 1
- C Programming - Data Types : Part 2
- C Programming - Constants and Identifiers
- C Programming - Operators
- C Programming - Expressions
- C Programming - Managing Input and Output Operations
- C Programming - Decision Making - Branching
- C Programming - Decision Making - Looping
- C Programming - Arrays
- C Programming - Handling of Character String
- C Programming - Functions (Part-I)
- C Programming - Functions (Part-II)
- C Programming - Structures and Unions
- C Programming - Pointers
- C Programming - Dynamic Memory allocation
- C Programming - Linked Lists
- C Doubly Linked Lists
- C Circular Linked Lists
- C Programming - File management in C
- C Language - The Preprocessor
- Call by Value and Call by Reference
- Concept of Pixel in C Graphics
- TSR in C - An Introduction







