Alright, so, a few here know I'm starting myself an adventure into learning some C. I figured it would be a good place to start with learning a wider variety of languages and will help push me a bit away from TI-BASIC.

Anyways, at the suggestions of benryves and KermMartian, I've downloaded Visual Studio C++. This program, when starting a new project, has a few options available to me, and I'm attempting to figure out what options C programmers here that use VS use to write C?

I believe KermM mentioned selecting 'Win32 Console Application' as the first option, which would give me


Code:
#include studio.h

<more code here I forget what it was>


Starting out with this option, gives me


Code:
// Test3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
   return 0;
}


So, I turn to here, asking if I've messed up with an option somewhere, since I know several members here know C, or is the above correct and I can just start typing in C code and get what I want out of it?
The above is correct. Some confusion may arise as Visual Studio is creating what it believes are sensible defaults, but which will differ to the most tutorials aimed at absolute beginners will use.

stdafx.h is related to precompiled headers which can speed up compilation time but make things a little complicated until you're familiar with the way they work. You can remove that #include line, though you should also check that precompiled headers are switched off for your project (right-click the project in the Solution Explorer, go into Properties->C/C++->Precompiled Headers->Precompiled Header=Not Using Precompiled Headers).
Tifreak, very nice to see you're expanding your horizons Very Happy

As for the first example, it's stdio.h, not studio.h Razz stdio.h is the standard I/O header, and is extremely often used, perhaps the most used piece of the standard library. To complete the example, so you know what it looks like (you should know):


Code:
#include "stdio.h"

int main(int argc, char**argv) {
   printf("Hello, chumps!");
   return EXIT_SUCCESS;
}
Yes, I know, I had very strict English teachers in school, and sometimes autocorrect in my head fixes words as I see them, so I saw the u in that. benryves had already corrected me.

Right now, I am just trying to get VS Cpp2010 to work for me, when I have time I am going to compile some example code from The C Book.
There is no need to return anything from main (it's implicit in C99/C++). For that matter, int is implicit in C too, and you don't need to accept any arguments, so the following is perfectly legal C:


Code:
#include <stdio.h>

main() {
   printf("Hello, World");
}


That won't compile as C++, though, so explicitly specifying a return type (even if it's int) is generally a good idea. Smile

Visual C++ makes use of main() (ANSI) and wmain() (Unicode) functions. _tmain() will be redefined as the appropriate one according to your project's character set settings (Properties, Configuration Properties, General, Character Set) and TCHAR will also map to either char or wchar according to your settings; that's why the default program generated by Visual Studio looks a little different to the (typically ANSI-only) example programs you see. For more information about main in Visual Studio, see main on MSDN.

Edit: Though I should probably say it's best to ignore this waffle for the time being and just get into programming. Worry about specifics later. Razz
You all are making me sad by omitting the \n at the end of your printf lines, although I suppose mentioning it with no explanation would be slightly confusing.


Code:
#include <stdio.h>
main() {
   printf("Hello, World\n");
}


The include line tells the compiler about a few assorted standard functions that most programs use, including I/O functions like open(), read(), write(), close(), get[s|c](), and all those exciting things. The main() function is always the one executed when the program is run, and can run any code and call any other functions. The printf line prints formatted text to the screen; this means numbers and strings inserted into the string to be displayed, not formatting like bold and italics. The program automatically ends when the end of the main() function is reached, even if you don't explicitly return; or exit();. And you'll notice that all commands end in a semicolon.

While we're on the subject, an example of what printf can do (don't worry too much about the char[] and int datatypes; you'll get to that).


Code:
int myage = 24;
char myname[] = "Kerm";
printf("I am %d years old and my name is %s.\n",myage,myname);


This will substitute 24 for the "%d" (which means "insert a decimal number here") and Kerm for the "%s" (which means "insert a string here"). The \n is a newline.
That all seems very self explanatory and well explained Very Happy

Do you happen to know what is so special about the include that VS tries to use from the get go? Is it something that is generally used by C++ over C?
I would have been almost positive that that's something Windows-specific, but BenRyves indicated:
Quote:
stdafx.h is related to precompiled headers which can speed up compilation time but make things a little complicated until you're familiar with the way they work.
It's nothing to do with C++ vs. C.
Ah, good to know, I didn't quite fully understand that. Very Happy And now I do.
tifreak8x wrote:
Ah, good to know, I didn't quite fully understand that. Very Happy And now I do.
Excellent. Smile Although it's a massive oversimplification, in a very real sense C++ is just C with some fancy wrappers, in that there's nothing you can program in C++ that you can't program in C. The object-orientation of C makes some tasks a lot easier and a lot more straightforward, though, which is why you might want to use it.
I had every intention of doing so, since once I learn C, I can learn what is needed to program C on the Prizm and on the 68K. It seemed to give more options than the other C variants.
tifreak8x wrote:
I had every intention of doing so, since once I learn C, I can learn what is needed to program C on the Prizm and on the 68K. It seemed to give more options than the other C variants.
C++, you mean? Which other C variants are you talking about, C#? I wouldn't really consider C++ and C# variants of C, even though as I said C and C++ are very closely related. C# in particular is quite distant from C, though it shares one letter of its name. Wink Sorry, this isn't the most clearly-worded post; I think either more coffee or more sleep is in order.
It's fine Very Happy I need all the explaining on these things that I can get.

Would you happen to know how one can edit the project to not look at precompiled headers? I forgot to un-check it when setting it up. :/

It would be good to know, anyways, in case I pull a stupid in the future.
Quote:
You can remove that [stdafx.h] #include line, though you should also check that precompiled headers are switched off for your project (right-click the project in the Solution Explorer, go into Properties->C/C++->Precompiled Headers->Precompiled Header=Not Using Precompiled Headers).

Sorry of that's not clear enough!
Oh, crap, sorry about that ben, I totally read right over that and didn't see it. :<

Thanks for the reminder. lol

Edit:


Code:
// Test2.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include <stdlib.h>


/*
* Tell the compiler that we intend
* to use a function called show_message.
* It has no arguments and returns no value
* This is the "declaration".
*
*/
void show_message(void);
/*
* Another function, but this includes the body of
* the function. This is a "d
*/
main(){
   int count;
   count = 0;
   while(count < 10){
      show_message();
      count = count + 1;
   }
   exit(0);
}
/*
* The body of the simple fun
* This is now a "definition"
*/
void show_message(void){
   printf("hello\n");
}


This only has one error

test2.cpp(20): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

This is more for me to see it, remember to look it up, and figure out what is up with it tomorrow, when I get some time to sleep in. :p
tifreak8x wrote:
This only has one error

test2.cpp(20): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

This is more for me to see it, remember to look it up, and figure out what is up with it tomorrow, when I get some time to sleep in. :p
You need to put int before main() - so int main(). C supports "default int" (i.e. if you don't specify a return type, int is assumed) but C++ does not. You are compiling the code as C++, which is why you get an error. It's best to include int, even if targeting plain old C. Smile
Ah, alright, thanks! That got it to compile and run! Very Happy

Sadly, the window disappeared after running, got to find a way to keep that from happening.. Something to do for tomorrow.
Start without debugging (Ctrl+F5) works. Alternatively, set a breakpoint on the closing brace of main() by clicking in the grey gutter to the left of it (a maroon blob should appear).
Glad to hear that you got a first program more complex than Hello World running, TIFreak8x! I look forward to your next forays into C/C++; will you be doing random programs as you learn concepts, or trying to aim for a specific application or two, and learning what you need to along the way to making that happen?
I've been thinking about that, and I honestly have no clue what to do. I -need- to set a goal of some sort, I just don't know what. :<
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 2
» All times are UTC - 5 Hours
 
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 vote in polls in this forum

 

Advertisement