I want to see a simple code in C so i can look how it all is. This year im learning C++ and wanted to see what it looks like!
Here’s a very simple program that counts input lines.
Main() /*count input lines*/
{
int c, nl;
nl=0;
while ((c = getchar()) != EOF)
if (( c == ‘
’)
++nl;
printf(“%d
”, nl);
}
#include <stdio.h>
main()
{
for(;
{
printf ("Hello World!
");
}
}
you’ll find that C looks very similar to C++ (as it should, it is the next rev of C, hence the “C++”), although you will not be able to use some common C++ functions like “cin >>” and “cout<<”.
*Originally posted by hobbes *
**you’ll find that C looks very similar to C++ (as it should, it is the next rev of C, hence the “C++”), although you will not be able to use some common C++ functions like “cin >>” and “cout<<”. **
Saying that C++ is the next revision of C is a bit misleading. C++ is C with object-oriented extensions (as well as a few others). C is not being replaced by C++. There are many uses where C is much more appropriate to C++.
Matt
your right matt, rev was a bad choice of words.
I would argue otherwise, there are no places that c++ cannot do just as well as c, seeing as it’s almost completely backwards compatable… just because the new features are there, doesn’t mean you’ve got to use them.
*Originally posted by monsieurcoffee *
**#include <stdio.h>main()
{
for(;
{
printf ("Hello World!
");
}
} **
I’ve not done console-C in a while, but I do believe it is actually:
#include <stdio.h>
int main(int argc, char *argv])
{
for(;;)
{
printf("Hello World!
");
}
return 0;
}
Both codes will work…
Raven_Writer’s code requires two passed parameters at invocation… argc (an integer) and argv(a pointer to an array of characters (also known as a string)). However, neither is used by the function “main”.
monsieurcoffee’s code is more traditional and correct as a stand alone program. However, he needs to indent his code so that it is more readable… Without indenting, any non-trivial code is very hard to maintain.
Both Matt and djcapelis are correct but (IMHO) Matt’s mindset is more correct for this project… C is a subset of C++, however, using C++ for the PIC processor (if you could) would be like hanging a picture with a sledgehammer. It can be done if you are very careful but, chances are, you will hit your thumb a few times quite unnecessarily.
Code for the robot should be simple and elegant with static variable structures. There is absolutely no need for C++ and all the excess baggage it brings with it.
*Originally posted by Mike Betts *
**Both codes will work…Raven_Writer’s code requires two passed parameters at invocation… argc (an integer) and argv(a pointer to an array of characters (also known as a string)). However, neither is used by the function “main”.
monsieurcoffee’s code is more traditional and correct as a stand alone program. However, he needs to indent his code so that it is more readable… Without indenting, any non-trivial code is very hard to maintain…**
Not to be nit-picky about this, but if you compiled monsieur’s code, it wouldn’t work because 1, main is not actually declared, and 2 main doesn’t return a value (but that’s only if you declare main to be an integer, and not void).
*Originally posted by djcapelis *
**I would argue otherwise, there are no places that c++ cannot do just as well as c, seeing as it’s almost completely backwards compatable… just because the new features are there, doesn’t mean you’ve got to use them. **
It only really becomes an issue when you’re working with embedded controllers. C++ requires quite a bit more overhead than C does (well, many features of C++ do). This overhead is a very bad thing in a limited environment such as an embedded control. The other issue with C++ is portability. There are many more compliant C compilers out there than there are C++ compilers. These C compilers also run on many more different systems than C++ compilers. I will admit this is becoming a thing of the past but it’s still something to consider.
Matt
Eric,
I did compile and run Matt’s code under Turbo C and Quick C with no problems. His program is the simplest code which is guaranteed to run under ANSI compliant C.
see http://www.lysator.liu.se/c/simple-c
Now to correct an error I made…
Your example is valid under DOS, etc… In your code argc is a integer which is the number of command line parameters and argv is a pointer to an array of pointers to arrays of characters. These strings are the command line parameters entered by you when you run the executable.
see: http://www.lysator.liu.se/c/multiple-level
Without adding this, Matt is unable to access command line parameters which is OK because we will not be running under DOS anyway…
As I said… Your code also works… I did not mean to start a fight here.
Peace.
*Originally posted by Mike Betts *
**Eric,I did compile and run Matt’s code under Turbo C and Quick C with no problems. His program is the simplest code which is guaranteed to run under ANSI compliant C.
see http://www.lysator.liu.se/c/simple-c
Now to correct an error I made…
Your example is valid under DOS, etc… In your code argc is a integer which is the number of command line parameters and argv is a pointer to an array of pointers to arrays of characters. These strings are the command line parameters entered by you when you run the executable.
see: http://www.lysator.liu.se/c/multiple-level
Without adding this, Matt is unable to access command line parameters which is OK because we will not be running under DOS anyway…
As I said… Your code also works… I did not mean to start a fight here.
Peace. **
Sorry
I’m still stuck on PC-coding, and forgot about the point, which was to discuss the new language’s syntax.
You didn’t start a fight, it was me that forgot for about 3 hrs. what this was actually about
*Originally posted by Mike Betts *
**Eric,I did compile and run Matt’s code under Turbo C and Quick C with no problems. His program is the simplest code which is guaranteed to run under ANSI compliant C.
<snip>
Without adding this, Matt is unable to access command line parameters which is OK because we will not be running under DOS anyway…
**
Not exactly accurate, first, to comply with ANSI C Raven_Writer is correct in saying that main must return a value as ANSI C specifically says main must be declared as int.
As for command line parameters, they aren’t just for DOS… if so they’d be useless… that command line sucks. They are also the same to access command line parameters on UNIX type operating systems. (Which if these Micro controllers were running BrickOS we theorectically might care about, but then again, they aren’t.)
As posted in the Programming Help thread, this site provides many examples of C programming: http://computer.howstuffworks.com/c.htm
There are more examples and explanation there than we could possibly post.
–Damian Manda
*Originally posted by Damian Manda *
**As posted in the Programming Help thread, this site provides many examples of C programming: http://computer.howstuffworks.com/c.htmThere are more examples and explanation there than we could possibly post.
–Damian Manda **
You could also just google or yahoo for c tutorials, there’s more of that than anything really