1. Computing

C Programming Quick Tips

Useful Tips in C Programming

By

This page is for your C programming tips. Add them by clicking the link under Readers respond and I'll update this article with them. Include a url to your site as well as name/country. Please note, we do moderation but it's only filtering bad language, spam or off-topic posts.

Nested Local Variables

One difference between C and C++ is that in C++ you can declare a local variable anywhere. C only lets you declare at the start of a block. As soon as executable code is found in that block, you can no longer declare any more variables.

However you can start a new block inside a block. This example has been tested in MS VC++ and GCC. The innermost j only exists in the block so assigning 4 does not affect the loop variable j in the for loop. The j++ however does, so the value printed out is 13.

 int main(int argc, char * argv[])
 {
   int j;
   j=0;
   for (j=9;j<12;j++)
   {
     {
       int j;
       j=4;
     }
   }
   {j++;}
   printf("Value of j = %d",j) ;
   return 0;
 }
 

Your tips Here!

Related Video
Indoor Home Video Tips
Outdoor Home Video Tips
  1. About.com
  2. Computing
  3. C / C++ / C#
  4. C Programming
  5. How To Do Things in C
  6. C Programming Quick Tips

©2014 About.com. All rights reserved.