473,508 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Ten common C (silly) mistakes

13,262 MVP
There are a lot of common C mistakes. I have put together the ones I consider to be the silliest made by C programmers. These are usually the source of the popular C programming tears and remembering the items on this page will brighten your C programming days somewhat.

(1) Values returned by mathematical functions on domain errors are implementation defined.

The best way of handling such domain errors is to ensure that they never arise at all. e.g Test for positive value before taking square root. Assuming that the compiler will return a certain value when the number is negative is a silly mistake.

(2) The minimum guaranteed value for the char type is zero.

This means that it is up to the implementation to have chars behaving as signed or unsigned char. To obtain the minimum value for your chars use CHAR_MIN defined in limits.h.

(3) Side effects are also silly


It is unsafe to use the same variable more than once in an expression if evaluating that expression changes the variable and the new value could affect the result of the expression.
Be careful when writing long expressions involving increment/decrement operators or expressions which call functions by reference.
Even x = x++; is unsafe.

(4) Don't skip the precedence tables.

Precedence tables are often skipped by people when reading introductory books but sometimes they can come back and smack you with the silly tag. If you miss that == (and !=) have higher precedence than ?: then you might expect c = (1?0:1 == 1?0:1); to assign the value 1 to c. Your reasoning would probably be that whatever is to the left of == is the same as what is to its right so the result should be true and c be one. That kind of reasoning is of course quite silly. The precedence table shows that the expression is evaluated as c = (1?0:(1 == 1)?0:1); with the inner brackets evaluated first then from left to right leaving c with a value of 0.
Association is also important e.g while ++ and * have equal precedence, they associate right to left meaning that *p++ will be evaluated as *(p++).


(5) Always use function prototypes.


If you don't then they are assumed to be taking the default type which is int. You might start to think that if you know the defaults then you should be able to assume them and not have to explicitly specify them. Those are very silly thoughts and should be immediately dismissed from the mind. Also cast any function arguments to the expected type if they are not that type already.



(6) The order of evaluation of function arguments is not defined.



That means that in

Expand|Select|Wrap|Line Numbers
  1. int a () {
  2.   printf("a\n");
  3.   return 1;
  4. }
  5. int b () {
  6.   printf("b\n");
  7.   return 2;
  8. }
  9. int c () {
  10.   printf("c\n");
  11.   return 3;
  12. }
  13. int sum (int a, int b, int c) {
  14.   return a + b + c;
  15. }
  16. int main(int argc, char** argv) {
  17.   printf("%d", sum(a(), b(), c()));
  18.   return (EXIT_SUCCESS);
  19. }
  20.  

the only guaranteed output is 6 (the sum) not the order in which a,b or c are printed.



(7) sizeof pointer to type T is the size of only one T element.


In particular, in

Expand|Select|Wrap|Line Numbers
  1. T arr[10];
  2. T *p = arr;
  3.  
sizeof(*p) != sizeof(arr). Rather sizeof(arr) = 10 * the size of one T element while sizeof(*p) is only the size of one T element.
The size of sizeof is also implementation defined and size_t (defined in stdlib.h) or unsigned long should be used to hold its value.

(8) main must return an int.

You don't call main in your programs, do you? So who calls it? How does he know how you have defined your main if he is calling it? The answers are simple. The guy who calls your main assumes that your main returns an int. Usually the caller will use that returned value to determine the success status of the program. If you don't give him that, he is going to get garbage and use that for whatever reed dancing he does with the returned value.

(9) Mixing signed and unsigned types with closed eyes.

The man who declares an unsigned type must slap himself awake every time he uses it in an expression with other types. Indeed anyone who writes any expression involving mixed types must be on guard. The stories about goblins lurking around programs collecting bits from expression evaluations are true and should not be deemed as silly.

(10) Array bounds. Need I say more?

If any of your functions take an array as argument, then pass that array's length as well. Avoid any functions which accept an array without also taking its length. In particular never use gets().
Nov 5 '08 #1
10 61521
freelance programmer
11 New Member
Eleventh one could be using = in place of == .
Feb 23 '09 #2
NotVeryClever
1 New Member
Why aren't they numbered 0-9? Then you could fit in =/==. Oh that would be eleven mistakes. Ooops. Anyone for Spinal Tap?
Mar 28 '09 #3
aatif
28 New Member
@freelance programmer
To avoid this logical mistake while making comparison, use constant on the left hand and the variable on the right. i-e if u want to compare x and 2;

Expand|Select|Wrap|Line Numbers
  1. if(x == 2)
  2. ...
  3.  
will obviously work but to be on safe side, write
Expand|Select|Wrap|Line Numbers
  1. if(2 == x)
  2. ...
  3.  
By doing so, your otherwise logical error by using '=' instead of '==' will be caught.
Expand|Select|Wrap|Line Numbers
  1. if(2 = x)
  2. ...
  3.  

--Aatif
Mar 30 '09 #4
Cuthbert
2 New Member
The size of a pointer to type T is the size of a pointer. The size of the type pointed to is irrelevant.
May 13 '09 #5
aatif
28 New Member
@Cuthbert
Yes, and the size of pointer (to whatever) is equal to the size of an int.
May 14 '09 #6
Cuthbert
2 New Member
@aatif
That may be the case for a particular implementation but it isn't true in general.

The size of an int is implementation dependant but is at least 16 bits.

The size of a pointer is implementation dependent. It's possible that pointers to different types have different sizes.
May 14 '09 #7
freelance programmer
11 New Member
@aatif
Now compiler gives a warning for
Expand|Select|Wrap|Line Numbers
  1. if(x = 2)
  2.  
but most programmers ignore them
Jun 3 '09 #8
felixp
2 New Member
As per the 9th commandment, can someone explain why the below line gets printed

unsigned int i = 5;
int j = -20;

if(i+j > 5)
printf(">5=%d\n",i+j); //why this gets printed
else
printf("<5=%d\n",i+j);
Sep 2 '09 #9
donbock
2,426 Recognized Expert Top Contributor
@felixp
I must be one a consequence of the integer promotion rules when you have an expression involving an unsigned int and a signed int. It looks like the rule must be to evaluate that expression using unsigned arithmetic. My copy of the Standard isn't handy so I can't confirm that.
Sep 2 '09 #10
felixp
2 New Member
yes i believe thats the right answer. Thank you
@donbock
Sep 3 '09 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

3
6774
by: S.W. Rasmussen | last post by:
With the risk of being accused of multi-posting I would like to draw the attention to a serious visual basic/windows issue discussed in the microsoft.public.vb.bugs newsgroup. As pointed out below...
15
12702
by: Jim | last post by:
This is probably a common question - What is the best CSS editor? I'm an old HTML dinosaur that just getting into CSS. My HTML editor from way back is Homesite. They (Macromedia) tout Topstyle Pro...
53
3916
by: KraftDiner | last post by:
I've spent hours trying to find a bug that was a simple spelling mistake. in an init method I declare a variable self.someLongName later in a different method of the class I use...
9
1920
by: Juan T. Llibre | last post by:
This article by Jeff Prosise is a must-read for ASP.NET programmers: http://msdn.microsoft.com/msdnmag/issues/06/07/webappfollies/default.aspx Juan T. Llibre, asp.net MVP aspnetfaq.com :...
9
1678
by: bwaichu | last post by:
I am starting this thread after having run into two separate programming problems, where the compiler offered no help. The compiler did not even warn. The programs compiled fine. And the...
29
2266
by: Cylix | last post by:
I am newbie in PHP, Thanks for any advise.
0
1518
by: pagates | last post by:
Hello All, Is there a way to show common "stuff" in the LoginView for all logged in users when using <RoleGroups>? In other words, I have a number of links that all users that log in should...
2
2075
by: rdemyan via AccessMonster.com | last post by:
Is there a way to set a common size for any form, when the user clicks on the middle button in the form control box the restore button for a maximized form. I'd like to have any maximized form,...
7
2358
by: primeSo | last post by:
I did C in UNIX environment. These are some of the common mistakes that always trapped me in my work. I need help from you guys to clarify them with me. 1) int intArray = {1,2,3,4,5}, i; ...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7133
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.