GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Dec-2005, 11:55
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough
Arrow

Today's C/C++ Programming Tip :-)


Friends,
Hope this thread will be very useful for all of us!

Here is a start:

Language: C
For people using the GNU Compiler:
Linux: GNU gcc compiler
Windows: Dev C++, Code::Blocks, MingW compiler

Ever think this is a C program?
buggy.c
CPP / C++ / C Code:
#include<stdio.h>
int main() {
    int x=10;
    printf("x = %d\n", x);
    int y=20;
    printf("y = %d\n", y);
    int z = x + y;
    int array[z];
    // What the heck??
    printf("z = %d\n", z);
    return 0;
}
If you compile this without any flags in linux, such as gcc buggy.c, dont expect any errors or warnings.
The compiler is so happy about it!
In Windows too, the program compiles fine, and the user thinks that the program is correct!

But Alas! The program is not a valid ANSI C program.
C is a procedure oriented language, and one has to declare the variables at the start. You cannot declare them in the middle.
Comment style: There are NO // comment in ANSI C standard.
And you cant use variable types for array initialization!.

So, we have to configure the compiler to identify and correct the errors in the program.
For that, there are three compiler flags:
-ansi This flag tells the compiler to enforce ANSI C standards
-pedantic More pedantic ansi, warnings for stuff you probably didn't mean.
-Wall Show all reasonable warnings (there are more).

So, you should make sure that it is running as a standard C compiler and not a GNU C compiler.
* gcc -ansi -pedantic makes it run as a standard C compiler
* gcc -ansi -pedantic -Wall makes it run as a standard C compiler and check for several types of errors

If you use an IDE such as Dev C++, then you can go to
Tools->Compiler Options and add the commands when calling the compiler. (See the attachment.)

Now, the following warnings and error messages appear:
Quote:
buggy.c: In function `main':
buggy.c:5: warning: ISO C90 forbids mixed declarations and code
buggy.c:7: warning: ISO C90 forbids mixed declarations and code
buggy.c:8: warning: ISO C90 forbids variable-size array `array'
buggy.c:9: error: parse error before '/' token
buggy.c:8: warning: unused variable `array'

Now, correct the errors and make the ANSI compiler happy.

Cheers,
Paramesh.
Attached Images
File Type: gif DevCpp.GIF (30.8 KB, 258 views)
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #2  
Old 16-Dec-2005, 19:49
alcoholic's Avatar
alcoholic alcoholic is offline
Member
 
Join Date: Nov 2005
Posts: 170
alcoholic is on a distinguished road
Quote:
Originally Posted by paramesh
But Alas! The program is not a valid ANSI C program.
C is a procedure oriented language, and one has to declare the variables at the start. You cannot declare them in the middle.
Comment style: There are NO // comment in ANSI C standard.
And you cant use variable types for array initialization!.
All of them are allowed in new ISO stnadard for C(C9X)

I guess ur using an old compiler urself
Last edited by cable_guy_67 : 27-Mar-2006 at 10:16. Reason: Similar comments blocked by moderated forum
  #3  
Old 16-Dec-2005, 20:19
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Today's C/C++ Programming Tip :-)


Language: C/C++
Level: Beginner

Dont use 0 before numbers.
Instead of using 010, use 10.

For example consider this example:
CPP / C++ / C Code:
 #include<stdio.h>
 int main() {
     int a[8] = { 000, 001, 010, 011, 100, 101, 110, 111 };
     int i;
     for(i = 0; i < 8; i++ ) {
         printf("%d\n", a[i]);
     }
     return 0;
 }

Expected output:
Code:
0 1 10 11 100 101 110 111

Actual Output:
Code:
0 1 8 9 100 101 110 111
Because Numbers that start with 0 are taken as Octal, so 010 is taken as 10-octal or 8-decimal.

So, we should be careful in using numbers.

Another example:
CPP / C++ / C Code:
 #include<stdio.h>
 int main() {
     int a[8] = { 0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111 };
     int i;
     for(i = 0; i < 8; i++ ) {
         printf("%d\n", a[i]);
     }
     return 0;
 }


As expected, the output was:
Code:
0 1 8 9 64 65 72 73

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #4  
Old 17-Dec-2005, 09:37
netnut netnut is offline
Member
 
Join Date: Dec 2005
Location: India
Posts: 174
netnut will become famous soon enough
Question

Re: Today's C/C++ Programming Tip :-)


Thanx for the tip Paramesh, I didn't know that...., maybe I thought that compuetrs these days have that much of IQ to handle numbers beginning with a zero.
Jokes apart, suppose at runtime a user enters a number beginning with a zero would the compiler take it as a hex/oct number? Then how can I "decode" them are there any standard library features to use or do i need to develop my own code (P.s. I use Turbo C++ 3.0)
  #5  
Old 12-Jan-2006, 02:54
lian1238 lian1238 is offline
New Member
 
Join Date: Sep 2005
Location: Thailand
Posts: 28
lian1238 is on a distinguished road

Re: Today's C/C++ Programming Tip :-)


if you need to use before numbers, use a double. right?
  #6  
Old 12-Jan-2006, 02:56
lian1238 lian1238 is offline
New Member
 
Join Date: Sep 2005
Location: Thailand
Posts: 28
lian1238 is on a distinguished road

Re: Today's C/C++ Programming Tip :-)


if you need to use 0 before numbers, use double instead of int.
  #7  
Old 11-Feb-2007, 00:46
UMLseek UMLseek is offline
New Member
 
Join Date: Feb 2007
Posts: 1
UMLseek is on a distinguished road
Thumbs up

Re: Today's C/C++ Programming Tip :-)


Quote:
Originally Posted by Paramesh
Language: C/C++
Level: Beginner

Dont use 0 before numbers.
Instead of using 010, use 10.

For example consider this example:
CPP / C++ / C Code:
 #include<stdio.h>
 int main() {
     int a[8] = { 000, 001, 010, 011, 100, 101, 110, 111 };
     int i;
     for(i = 0; i < 8; i++ ) {
         printf("%d\n", a[i]);
     }
     return 0;
 }

Expected output:
Code:
0 1 10 11 100 101 110 111

Actual Output:
Code:
0 1 8 9 100 101 110 111
Because Numbers that start with 0 are taken as Octal, so 010 is taken as 10-octal or 8-decimal.

So, we should be careful in using numbers.

Another example:
CPP / C++ / C Code:
 #include<stdio.h>
 int main() {
     int a[8] = { 0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111 };
     int i;
     for(i = 0; i < 8; i++ ) {
         printf("%d\n", a[i]);
     }
     return 0;
 }


As expected, the output was:
Code:
0 1 8 9 64 65 72 73

Regards,
Paramesh.

Paramesh,
You English borders on to flawless. How come?
You command over the basics is so refreshing. Keep up the good work.
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Read/ Write EXCEL files using C/C++ programming confused_pig C++ Forum 4 25-Nov-2005 01:27
[Tutorial] GUI programming with FLTK dsmith FLTK Forum 10 03-Oct-2005 16:41
Network programming pointer C++ Forum 2 20-May-2005 15:36
GUI programming crystalattice C++ Forum 5 14-Sep-2004 13:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:38.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.