Increment and Decrement Operators in C

By: Baski Emailed: 1641 times Printed: 2109 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

C provides two unusual operators for incrementing and decrementing variables. The increment operator ++ adds 1 to its operand, while the decrement operator -- subtracts 1. We have frequently used ++ to increment variables, as in
   if (c == '\n')
       ++nl;
The unusual aspect is that ++ and -- may be used either as prefix operators (before the variable, as in ++n), or postfix operators (after the variable: n++). In both cases, the effect is to increment n. But the expression ++n increments n before its value is used, while n++ increments n after its value has been used. This means that in a context where the value is being used, not just the effect, ++n and n++ are different. If n is 5, then
   x = n++;
sets x to 5, but
   x = ++n;
sets x to 6. In both cases, n becomes 6. The increment and decrement operators can only be applied to variables; an expression like (i+j)++ is illegal.

In a context where no value is wanted, just the incrementing effect, as in

   if (c == '\n')
       nl++;
prefix and postfix are the same. But there are situations where one or the other is specifically called for. For instance, consider the function squeeze(s,c), which removes all occurrences of the character c from the string s.
   /* squeeze:  delete all c from s */
   void squeeze(char s[], int c)
   {
      int i, j;

      for (i = j = 0; s[i] != '\0'; i++)
          if (s[i] != c)
              s[j++] = s[i];
      s[j] = '\0';
   }
Each time a non-c occurs, it is copied into the current j position, and only then is j incremented to be ready for the next character. This is exactly equivalent to
   if (s[i] != c) {
       s[j] = s[i];
       j++;
   }
Another example of a similar construction comes from changing the following lines of code
   if (c == '\n') {
       s[i] = c;
       ++i;
   }
by the more compact
   if (c == '\n')
      s[i++] = c;
As a third example, consider the standard function strcat(s,t), which concatenates the string t to the end of string s. strcat assumes that there is enough space in s to hold the combination. As we have written it, strcat returns no value; the standard library version returns a pointer to the resulting string.
   /* strcat:  concatenate t to end of s; s must be big enough */
   void strcat(char s[], char t[])
   {
       int i, j;

       i = j = 0;
       while (s[i] != '\0') /* find end of s */
           i++;
       while ((s[i++] = t[j++]) != '\0') /* copy t */
           ;
   }
As each member is copied from t to s, the postfix ++ is applied to both i and j to make sure that they are in position for the next pass through the loop.

C Home | All C Tutorials | Latest C Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Baski
Compiling multiple source files and specifying classpath using javac
The equals() Method example in Java
Default Values for Data Types in Java
JSP Example to connect to MS SQL database using Tomcat Connection Pool
<convertNumber> and <convertDateTime> in JSF
Enable/Disable Scripting Elements in JSP
Using calloc() Function in C
lseek() sample program in C
Arrays of Structures example program in C
The Basic Syntax Expression Language in JSP
Initialization of Pointer Arrays in C
Functions returning non-integer values in C
Increment and Decrement Operators in C
Getting Started with C
What is JSF (JavaServer Faces)?

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
Using memset(), memcpy(), and memmove() in C
Open, Creat, Close, Unlink system calls sample program in C
lseek() sample program in C
Using Bit-field in C
UNIX read and write system calls sample program in C
Find square and square root for a given number in C
Basics of C
The for statement in C
Symbolic Constants using #define in C
Line Counting sample program in C
Character Arrays in C
Data Types and Sizes in C
Increment and Decrement Operators in C
goto and labels in C
register Variables in C
Most Emailed Articles (in C)
Multi-dimensional Arrays in C (Explained using date conversion program)
The for statement in C
Initialization of Variables in C
Pointers vs. Multi-dimensional Arrays in C
Using Bit-field in C
UNIX read and write system calls sample program in C
Open, Creat, Close, Unlink system calls sample program in C
Formatting with printf in C
Passing double value to a function in C
Printing a simple histogram in C
Basics of C
Arrays sample program in C
Character Arrays in C
else if statement in C
register Variables in C