It is currently Sun May 19, 2013 4:22 pm

All times are UTC - 5 hours [ DST ]




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

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1795
Location: India
Same here but its a very elegant language.I enjoy it when i don't have to debug others codes for issues :D Though its nigh impossible to match the speed of a well written C/C++ program.Most games still use C/C++.


Offline
 Profile  
 
#12) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sat Apr 14, 2012 10:59 am 
User avatar

Joined: Fri Jul 04, 2008 10:30 am
Posts: 4932
jaydip wrote:
Same here but its a very elegant language.I enjoy it when i don't have to debug others codes for issues :D Though its nigh impossible to match the speed of a well written C/C++ program.Most games still use C/C++.


You know I have never looked at the coding of a game.

Hmmmm

Putting together a gaming rig soon thanks to Mark.

Perhaps I will finally gaze into game code.

Maybe I'll get a bug up my butt and write a game.

Crazy things happen like that from time to time. ;-)


Offline
 Profile  
 
#13) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sat Apr 14, 2012 11:11 am 
User avatar

Joined: Fri Jul 04, 2008 1:26 am
Posts: 19669
Location: 404 - Not Found!
Writing a game is pretty complex. i remember writing code to make the 'ball' move across the screen, 30 years ago. :P
:blush:

Of course it is a lot easier now and there are engines to choose from. Modding games seems to be a good place to start. i don't know . . .

AND you should be able to build a nice gaming system next week. Plus it is cold in Chicago for much of the year. That SuperClocked Galaxy GTX 480 will keep you warm while gaming.
:grin:


Offline
 Profile  
 
#14) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sat Apr 14, 2012 11:21 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1795
Location: India
If u are serious about game programming start right here :grin:
ftp://ftp.idsoftware.com/idstuff/source ... source.zip

I learned a lot from browsing through Quake III 's code.


Offline
 Profile  
 
#15) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sat Apr 14, 2012 1:51 pm 
User avatar

Joined: Fri Jul 04, 2008 10:30 am
Posts: 4932
jaydip wrote:
If u are serious about game programming start right here :grin:
ftp://ftp.idsoftware.com/idstuff/source ... source.zip

I learned a lot from browsing through Quake III 's code.


I will check it out.

Thanks for great link (thy)


Offline
 Profile  
 
#16) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sat Apr 14, 2012 7:11 pm 
User avatar

Joined: Mon May 10, 2010 3:46 pm
Posts: 3849
Location: Earth
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)



_________________
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  
 
#17) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sun Apr 15, 2012 12:44 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1795
Location: India
4.
This is not really an mistake but an oversight which can cause some hard to catch errors.Here is the program

#include<stdio.h>
#include<conio.h>
#include<limits.h>
int main()
{
int a=10,b=20;
printf("Before swap a=%d b=%d\n",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("After swap a=%d b=%d\n",a,b);
_getch();
return 0;

}
It basically swaps two variables without using a third variable.The program runs fine and prints "a=20 b=10" after swapping.So where is the problem?Lets have another look at this program but we will be using different values this time
#include<stdio.h>
#include<conio.h>
#include<limits.h>
int main()
{
int a=INT_MAX,b=20;
printf("Before swap a=%d b=%d\n",a,b);
a=a+b;
printf("After a=a+b a=%d b=%d\n",a,b);
b=a-b;
printf("After b=a-b a=%d b=%d\n",a,b);
a=a-b;
printf("After swap a=%d b=%d\n",a,b);
_getch();
return 0;

}
Here is the output from console
Before swap a=2147483647 b=20
After a=a+b a=-2147483629 b=20
After b=a-b a=-2147483629 b=214
After swap a=20 b=2147483647

I think u will find it very interring that after a=a+b the value of a becomes negative :scratch: The reason behind->INT_MAX is the max possible value of the integer(this is defined in limits.h and has this value 2147483647 for my C/C++ implementation)and when u add something to it , it can't be positive any more as it crosses the maximum limit of the positive value.So the value of "a" warps around and becomes negative.You can argue at this moment that the end result is same,but its actually implementation defined.You should never depend on implementation defined features unless absolutely necessary as it hurts portability.So lets take another look at this program

#include<stdio.h>
#include<conio.h>
#include<limits.h>
int main()
{
int a=INT_MAX,b=20;
printf("Before swap a=%d b=%d\n",a,b);
a=a^b;
printf("After a=a^b a=%d b=%d\n",a,b);
b=a^b;
printf("After b=a^b a=%d b=%d\n",a,b);
a=a^b;
printf("After swap a=%d b=%d\n",a,b);
_getch();
return 0;

}
Here is the output from console
Before swap a=2147483647 b=20
After a=a^b a=2147483627 b=20
After b=a^b a=2147483627 b=214748
After swap a=20 b=2147483647

Here u can see that the values don't change signs during their course.Using XOR(exclusiveor) is the best way to tackle this problem.I will look into XOR in the next section.


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

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1795
Location: India
The Best Book for programming period. (cooking)
The Art of Computer Programming These books are pretty hardcore and it expects a certain level of familiarity with higher mathematics , u have been forewarned :grin:

Now back to XOR:-

Now C/C++ programming guidelines have no direct connection with XOR.XOR is just a bit-wise operator in C/C++. I think a little knowledge of XOR will help us to understand the logic behind the previous example.Lets explain with a small example

#include<stdio.h>
#include<conio.h>
#include<limits.h>
int main()
{
int a=2,b=1;
printf("Before swap a=%d b=%d\n",a,b);
a=a^b;
printf("After a=a^b a=%d b=%d\n",a,b);
b=a^b;
printf("After b=a^b a=%d b=%d\n",a,b);
a=a^b;
printf("After swap a=%d b=%d\n",a,b);
_getch();
return 0;

}
Here is the output from Console
Before swap a=2 b=1
After a=a^b a=3 b=1
After b=a^b a=3 b=2
After swap a=1 b=2


Int a=2 and int b=1 results in the following bit-pattern in memory.Here i will take nibble i.e. the last four bits of a and b.For my example it will be sufficient as the higher order bits are basically all zeroes.

0010 ------- a(2 in decimal)
0001 ------- b(1 in decimal)
-------------------
0011 -------a^b(3 in decimal)

So u can see for XOR operation the bits are only true(1) when either one of bits are false(0). If both bits are true(1) or false(0) they the end result is false(0). Hence the name Exclusive.I think it is now pretty straightforward to deduce the result.The advantage is that unlike arithmetic operations u can't exceed the limit.So all of your operations will be perfectly portable across systems.

Please provide your valuable feedback unless it doesn't take much of your time :grin:


Last edited by jaydip on Sun Apr 15, 2012 3:48 am, edited 4 times in total.

Offline
 Profile  
 
#19) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sun Apr 15, 2012 3:23 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1795
Location: India
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:


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

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1795
Location: India
5.
This is pretty simple really.This is a very common mistake made by beginners.Consider the While loop in C/C++ for a moment.
While(condition)
{
statements
}

"While" is an entry control loop(means the condition must be satisfied to enter the loop).Lets take a look at some of examples of While loop
int i=1;
while ( i <= 10)
{
printf("i=%d\n",i);
i++;
}

This loop will be executed 10 times.What about this loop?
int i=1;
while ( i <= 10);
{
printf("i=%d\n",i);
i++;
}

Before u say 10 times watch the ";" after the while loop.Basically it is an infinite loop.
while ( i <= 10)
;
";" is a statement marker in C/C++.";" is a NULL statement.So after the comparison " i <= 10" the ";" statement will be executed.The scope of the while loop is the following statement unless we are using braces.So here after executing the NULL statement it will back again at the comparison " i <= 10" and it will be executed indefinitely.As there is no change in the value of "i" the condition " i <= 10" will always be true.


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 2 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: