Begining C noob questions...

Please don't post here unless its in response to an existing topic. New discussion is to be shared in the other forums.

Begining C noob questions...

Postby madgernader » Wed Dec 06, 2006 5:08 pm

Whoo, I'm rocking the noob questions threads :P


First Question: In Microsoft Visual C++ 2005 EE how do I compile .c files and not .cpp, I know that I could just use .cpp but I want to follow the tutorials of the book more faithfully and not skip anything on my path to learning it...

Second Question: What command do I need to use in C to have the command prompt window that appears after the compile to stay up so i can read it and make sure its doing what I want, right now it flashes up and closes.

Third Question: I know that C++ is a superset of C but is C# also a superset? and what can it do that C cant?

Fourth Question: Whenever I create a new win 32 application in C++ 05 EE it gives me the header, the .cpp file I edit, then another file with the same name as the header file, but with a .cpp extension, what is the point in that? since the file I edit has the header file included, there isn't much else that the other could do...

And thats it for now, but Ill just add to this thread if I get some more C questions...
madgernader
New Blood
 
Posts: 24
Joined: Tue Nov 28, 2006 12:55 pm
Location: United States

Postby Bob » Wed Dec 06, 2006 9:55 pm

Great questions!

Here we go!

1) MSVS is a C++ compiler in that it assumes that C is dead, but as you said it does allow you to compile your code as if it were C. The reason it does this is the superset/subset idea. You can use C syntax in C++ freely. If you want to explicitly create a .c file you are up a creek with some sort of paddle but not one you would expect. Since MSVS assumes for CPP and doesn?t allow for you to create say... a C application, you are forced to change the name of it by hand. I?m sure that there are other fixes but you should be alright with just that.

If you are tired of changing your file extensions and just want MSVS to build it as a c application you can use the properties of the project, right click it and you will see it down at the bottom. What you are looking for is under the following tree structure:

Configuration Properties > C/C++ > Advanced

In this menu you will see an option called Compile As which allows you to change the way that MSVS handles the building of the program from C++ to C code explicitly, thereby making it so you don?t have to worry about changing the file extensions. You can change it in your preferences as well, for defaults, but i find that its useful to leave that shit alone.

2) Stopping the window from closing is one of the things that absolutely drove me nuts when i was first starting, which I'm sure it is you as well. The easiest way to do this is with a break point, but you have to "debug" your application every time to get it to actually work. If you want to know a very quick fix to this you should use one of the means of getting input from the user, like getc(stdin); in order to get a character from the user to signal your exit.

There are some dopey functions that i have seen over the years but for the most part you should just keep things simple. getc(stdin); will allow you to pause until you hit enter.

Code: Select all
#include "stdafx.h"


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


3) C# is a lie. C# is basically C++ syntax for Visual Basic programmers. Of course they have added pointers and a more java like class description/definition tree, but for the most part it is not much of a c subset. I would actually argue otherwise for the express reason of it not being friendly to pointers, which all C languages have depended upon. In order to use pointers in C# you have to declare the function you are using as unsafe and compile your app with that option turned on as well, in effect the C# language is saying that programming in C or C++ is unsafe which is what should be directed at languages like java which lead to brain tumors and aneurysms.

4) the reason for the multiple files is actually one of the best features of C++ & C: Modularity . You are able to put your declarations in the .h and the definitions in the .cpp, allowing you to work on teams and distribute/agree upon a header file that would describe input and output of a function while not worrying about or sharing the actual code. When you work on large projects this is actually very common. For the record, you don't have to develop in this fashion, you should, however, if you are working with lots of files and or need to worry about inheritance (C++ exclusively).

Thank you for the great questions, please continue to post them as they come. As most of you know, I have internet access ~ 20/7 and I hover around my forum most of the day.

Take care
Bob
Bob
Site Admin
 
Posts: 252
Joined: Mon Nov 20, 2006 12:24 am
Location: San Jose California

Postby madgernader » Thu Dec 07, 2006 3:17 pm

Thanks for the answers, your responce to the second question made me remember another thats been on my mind...

Code: Select all
#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{

      return 0;
}


Thats pretty much what you get when you make a new C++ program, what does the int _tmain(int argc, _TCHAR* argv[]) do as opposed to the regular int_main(void) like I use in staring tutorials in the book?
madgernader
New Blood
 
Posts: 24
Joined: Tue Nov 28, 2006 12:55 pm
Location: United States

Postby Bob » Thu Dec 07, 2006 4:54 pm

int _tmain(int argc, _TCHAR* argv[])

Unicode compliancy. You will come across it at some point in your life, and its alot of fun, if you dont mind craziness in your life.

int main(int argc, char* argv[])

ASCII compliancy. These are the simpler files, but they don't have the unicode and can cause errors when your programs get transferred around into other languages.

http://www.joelonsoftware.com/articles/Unicode.html
Bob
Site Admin
 
Posts: 252
Joined: Mon Nov 20, 2006 12:24 am
Location: San Jose California

Postby madgernader » Thu Dec 07, 2006 7:07 pm

Ok, another question. The getc(stdin); works good except for when I'm entering something and use enter to submit it. I did a bit of a workaround but it would be good to know if I could set to it a different key that wouldnt be used when entering information...

Code: Select all
#include <stdio.h>

int main(void)
{
   float radius = 0.0f;
   float diameter = 0.0f;
   float circumference = 0.0f;
   float area = 0.0f;
   float Pi = 3.14159265f;

   printf("Input the diameter of the table:");

   scanf("%f", &diameter);                           
   getc(stdin);                                 
   radius = diameter / 2.0f;
   circumference = 2.0f * Pi * radius;
   area = Pi * radius * radius;

   printf("\nThe circumference is %.2f", circumference);   
   printf("\nThe area is %.2f\n", area);
   
   getc(stdin);                                 
   return 0;
}

As you can see I put another break in after the scanf() so I could hit enter and continue the program without it closing right away. It would be really nice if I could set it to something less used, like TAB, or Ctrl, or the windows key for that matter.

I took out my comments since they wrapped around and made the code harder to read.
madgernader
New Blood
 
Posts: 24
Joined: Tue Nov 28, 2006 12:55 pm
Location: United States

Postby Bob » Thu Dec 07, 2006 8:51 pm

That is indeed one of the catches, getc() grabs one character, so if you put more than one, it will actually drive you crazy but you have to flush what is called the input buffer in order to grab it.

Code: Select all
while (getc(stdin)!=NULL){}


throw that into your code below each use of getc(stdin)

you can use '\t' {Tab} and you can also use other means, but i havent done console applications in a long time so i don't remember what the syntax of such a thing was.
Bob
Site Admin
 
Posts: 252
Joined: Mon Nov 20, 2006 12:24 am
Location: San Jose California

Postby madgernader » Thu Dec 07, 2006 9:13 pm

Ok so that bit of code makes it stay up and not close when I hit enter.

Could I set a if else statement so I could still use enter to close the program after it ran?

like (forgive me if i get this totally wrong)

Code: Select all
float Pi = 3.14159265f;
int lastrun = 0;

if lastrun = 0

printf("Input the diameter of the table:");

(rest of the program)

printf("\nThe area is %.2f\n", area);
lastrun = 1

getc(stdin);
while (getc(stdin)!=NULL){}

else getc(stdin);


I may have messed it up but thats my theory, I think it would work, but im not sure if i got all the terms right...
madgernader
New Blood
 
Posts: 24
Joined: Tue Nov 28, 2006 12:55 pm
Location: United States

Postby Bob » Thu Dec 07, 2006 9:43 pm

totally wrong

if you want it to work like that, i would suggest you work it differently.

Ill get back you you on this, but that is definitely not going to work.
Bob
Site Admin
 
Posts: 252
Joined: Mon Nov 20, 2006 12:24 am
Location: San Jose California

Postby madgernader » Thu Dec 07, 2006 9:47 pm

Yea i figured, especially since in my book I'm just learning variables, and i haven't even reached double yet, So its not that big of deal to me if that wont work since once I get to sections on if, else, while and such it'll probably explain a good way of doing it, but that way is just what had came into my head...
madgernader
New Blood
 
Posts: 24
Joined: Tue Nov 28, 2006 12:55 pm
Location: United States

Postby Bob » Fri Dec 08, 2006 12:22 am

what you could do is something like the following

Code: Select all
 char buffer[1024];
do
{
// Code in here ...
} while (printf("Enter 'quit' to exit: "), fgets(buffer, 1024, stdin), 0 == strcmp(buffer, "quit"));

This will allow you to keep looping while you input anything other than quit. It works really well for menu systems, but it becomes quite irritating if you dont know what the kill string is.
Bob
Site Admin
 
Posts: 252
Joined: Mon Nov 20, 2006 12:24 am
Location: San Jose California


Return to Unsorted Old Posts

Who is online

Users browsing this forum: No registered users and 1 guest

cron