It is currently Sun May 26, 2013 1:12 am

All times are UTC - 5 hours [ DST ]




 Page 3 of 5 [ 50 posts ]  Go to page Previous  1, 2, 3, 4, 5  Next
#21) 
Author Message
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Tue Apr 17, 2012 4:56 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1828
Location: India
Indentation is very important as it improves the readability,maintainability of your programs dramatically.Consider this eg

if(i>=3)
if(j>=4)
if(k>=5)
m=6;

This is very difficult to read.Now consider this one
if(i>=3)
if(j>=4)
if(k>=5)
m=6;

This is now very easy to understand.There is no hard and fast rules about indentation as various organizations follow their own standards.But u should always be consistent,pick one style and stick to it.Normally we follow one tab to indent our code.My own examples sometimes don't follow that :D But please don't bother about it ;)
Nowadays editors indent the code by themselves but u can customize it.


Last edited by jaydip on Tue Apr 17, 2012 5:19 am, edited 2 times in total.

Offline
 Profile  
 
#22) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Tue Apr 17, 2012 4:57 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1828
Location: India
6.
Another Gem :grin:

a= b/*p ;//*p is the divisor

what will be the output of a? Common sense implies it should be the result of division of b and *p. But there's a catch which we are going to find out. Before proceeding, from the c reference manual
"‘‘If the input stream has been parsed into tokens up to a given character, the next token is taken to include the longest string of characters which could possibly constitute a token.’’ Thus, if a "/" is the first character of a token, and the / is immediately followed by a *, the two characters begin a comment, regardless of any other context. In fact, /*begins a comment, so the compiler will simply gobble up the program text until the */appears. In other words, the statement just sets a to the value of b and comments the rest and doesn't bother with p at all . Rewriting this statement as

a = b / *p ; //*p is the divisor, Note a white space between / and *

or even

a = b/(*p) ; //*p is the divisor

would cause it to do the division the comment suggests. But its always preferable to use the second version as it clearly indicates our preference.


Last edited by jaydip on Sun Apr 22, 2012 2:11 am, edited 1 time in total.

Offline
 Profile  
 
#23) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Tue Apr 17, 2012 1:52 pm 
User avatar

Joined: Mon May 10, 2010 3:46 pm
Posts: 3864
Location: Earth
jaydip wrote:
BoFox wrote:
One more guideline is to add a space after commas and decimal points in sentences. Otherwise, this would be bad programming in English language.

:tease: (ROFLMAO)


:good: :amen:

Hehe.. you have an interesting style, but it's just easier to read with the spaces in between!



_________________
What is this thing right now?
Put your arms up on one side of the horizon, put them up into the sky and twist them across, meeting unto the other side of the horizon. That is a sign symbol of life.
Face the goodness in life.


Offline
 Profile  
 
#24) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Wed Apr 18, 2012 1:06 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1828
Location: India
BoFox wrote:
jaydip wrote:
BoFox wrote:
One more guideline is to add a space after commas and decimal points in sentences. Otherwise, this would be bad programming in English language.

:tease: (ROFLMAO)


:good: :amen:

Hehe.. you have an interesting style, but it's just easier to read with the spaces in between!


Yeah i agree,thanks for the feedback :good: I will try to add some more spaces in the future topics :grin:


Offline
 Profile  
 
#25) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sun Apr 22, 2012 2:40 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1828
Location: India
7.
Another C/C++ pearl :P

It would be a gross injustice in leaving any discussion of syntactic pitfalls without mentioning the Dangling else problem. Although it is not unique to C/C++, it has bitten C/C++ programmers with many years of experience.

Consider the following example:

if (a == 0)
if (b == 0)
//do something;
else {
m = a + b;
}
The programmer’s intention for this fragment is that there should be two main cases: a= 0 and a≠0. In the first case, the fragment should do nothing at all unless b = 0, in which case it should "do something". In the second case, the program should set m = a + b.
However, the program fragment actually does something quite different. The reason is the rule that an else is always associated with the closest unmatched if. If we were to indent this fragment the way it is actually executed, it would look like this:

if (a == 0) {
if (b == 0)
//do something;
else {
m = a + b;
}
}

In other words, nothing at all will happen if a≠0. To get the effect implied by the indentation of the original example, write:
if (a == 0) {
if (b == 0)
//do something;
} else {
m = a + b;

}


Last edited by jaydip on Sun Apr 22, 2012 2:49 am, edited 4 times in total.

Offline
 Profile  
 
#26) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sun Apr 22, 2012 2:42 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1828
Location: India
The indentation has been messed up,if u r confused feel free to raise questions and as always feed-backs are greatly appreciated.;)


Offline
 Profile  
 
#27) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sun Apr 22, 2012 3:13 am 
User avatar

Joined: Sat Apr 24, 2010 11:19 am
Posts: 5014
jaydip wrote:
The indentation has been messed up,if u r confused feel free to raise questions and as always feed-backs are greatly appreciated.;)

use the code tags to show the indentation properly.



_________________
This is such total Horse-S**t!
"At NVIDIA we know that all shredders are green." --Jensen Huang
Adam knew he should have bought a PC, but Eve fell for the marketing hype. >:)
Online
 Profile  
 
#28) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sun Apr 22, 2012 3:14 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1828
Location: India
Thanks (thy)


Offline
 Profile  
 
#29) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sat Apr 28, 2012 9:15 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1828
Location: India
8.
#include<stdio.h>
#include<conio.h>
#include<limits.h>
int main()
{
   int i=1,j=2,k ;
   k=i+++j;
   printf("k=%d\n",k);
   printf("i=%d\n",i);
   printf("j=%d\n",j);
   _getch();
   return 0;

}

What will be the output of the above code on compilation?
The confusing part is the second statement
k=i+++j;
which can mean
k=i++ + j;
or
k=i+ ++j;

both are valid syntax.According to C reference manual when we have more than one possibility for the next token the compiler will choose the longest one i.e. the statement will be treated as
k=i++ + j;

So our output will be
k=3
i=2
j=2

If we need different evaluation we will need to supply parenthesis.Thank u grstanford for advising me to use the <code> tag.


Offline
 Profile  
 
#30) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sun May 13, 2012 5:14 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1828
Location: India
9.
Ok it has been some time,i guess its time for another C/C++ gem :grin:
#include<stdio.h>
#include<conio.h>
#include<limits.h>
int main()
{

   int i=-1;
   unsigned int j=1;
   if (i>j)
      printf("I is greater than J\n");
   else
      printf("I is smaller than J\n");
   if (i>(int)j)
      printf("I is greater than J\n");
   else
      printf("I is smaller than J\n");
   if ((unsigned)i>j)
      printf("I is greater than J\n");
   else
      printf("I is smaller than J\n");
   _getch();
   return 0;

}


The output:-
I is greater than J
I is smaller than J
I is greater than J

First thing that comes to mind that my compiler may be faulty,how on earth can be "-1" greater than "1"?This is one of the very weird rules of C/C++. if u mix an unsigned and signed type the result is of the unsigned type.If u convert "-1" to a positive integer it becomes a huge number which is definitely greater than "1". The third "If...else" shows what is happening behind the scenes by means of a cast.Moral of the story ,never mix an unsigned and signed type.We tend to declare unsigned types for some variables which can't be negative for e.g. Age,but this is a bad choice.We should only declare unsigned types for bitfields (more on this later). Now cppcheck throws a waring when u compile this code,

warning C4018: '>' : signed/unsigned mismatch

It shows the static analyzers can be god sent at times if we look carefully at the warnings.


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 3 of 5 [ 50 posts ]  Go to page Previous  1, 2, 3, 4, 5  Next

All times are UTC - 5 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: