-3

I am a c# programmer but today i am thinking of migrating to c++, The first thing i wonder about is that there is 2 types of projects CLR Console Application and Win32 Console Application

Also what confuses me is that in Win32 i can write line to console using either printf or cout

while in CLR i can also use the familiar Console::WriteLine

i am totally confused what is the point of having all that functions just to write a line and what is the difference between the CLR and Win32 Applications

Roman Ratskey
  • 5,101
  • 8
  • 44
  • 67
  • `printf` is C, `cout` is C++, `WriteLine` is C++-CLI. – chris Nov 09 '12 at 17:45
  • am i able use C in C++ ?! Also what is the difference between C++ and C++CLI !? – Roman Ratskey Nov 09 '12 at 17:46
  • Don't forget about [WriteConsole](http://msdn.microsoft.com/en-us/library/windows/desktop/ms687401(v=vs.85).aspx) – Mooing Duck Nov 09 '12 at 17:47
  • 1
    @RuneS, You can use most C in C++, but there's usually a better alternative. C++-CLI is an extensive addition that includes .NET and is not considered C++ or addressed in the C++ standard. – chris Nov 09 '12 at 17:48
  • So using C++CLI i can combine .Net Functions with the speed of C++ or i just have to stick with C++ ?. what is the point of using .NET in C++ while i can simply use C# ? – Roman Ratskey Nov 09 '12 at 17:50
  • @RuneS, I never fully understood why C++-CLI existed myself. I guess a .NET language that still allows for C++ syntax is something, but there was something about it being a good mediator between a C# dll and a C++ executable. – chris Nov 09 '12 at 17:51
  • 3
    nah C# CLI is pretty rubbish - it gives you the disadvantages of both C++ over C# and .net over native. – jheriko Nov 09 '12 at 17:53
  • No i meant instead of using .NET codes in C++ i just use .Net because it does not make sense to use .Net codes in c++ ! so i needed an answer for that – Roman Ratskey Nov 09 '12 at 17:54
  • 1
    @RuneS: According to Herb Stutter (on behalf of Microsoft), C++CLI should be used primarily for interfacing C++ with the .Net library. It shouldn't be used for "real" code. – Mooing Duck Nov 09 '12 at 17:57
  • 3
    Actually, RuneS, @Griwes makes a very valid point. While you probably will be able to hop along, t is very unlikely that you learn to write good C++ without at least one [good book](http://stackoverflow.com/q/388242/140719). – sbi Nov 09 '12 at 17:58
  • Where did my "RTFM" comment go? – Griwes Nov 09 '12 at 18:54
  • @Griwes: It was probably moderated out. I agree with the sentiment that OP would be better served by studying the basics on their own, but this suggestion can be made more effectively and less adversarialy than "RTFM." [RTFM comments are frowned upon by the community & moderators](http://meta.stackexchange.com/questions/23628/how-to-deal-with-rtfm-comments) – John Dibling Nov 09 '12 at 19:54
  • Well, even if Jon Skeet thinks otherwise, "RTFM" is perfect way to say that - heck, it was invented to simplify saying that! – Griwes Nov 09 '12 at 20:11

2 Answers2

4
  • printf is the C way. It isn't typesafe, but it's fast. It runs on the standard C library. (C++ can do this, but it's not normal.)
  • cout is the C++ way. It's typesafe, and is the normal way in C++. It runs on the standard C++ library.
  • WriteLine is the .Net way, and thus the C++/CLI way. It runs on the .Net library.
  • WriteConsole is the Windows way. It's a raw operating system call, so is fast, but it's hard to use and not portable. Don't use this.

As for comparing the languages, C is a less complicated language (making your code more complicated), but C++ has templates, so they're roughly equivalent in speed, depending on the task. They're normal Win32 applications, which means they're pre-complied to raw machine code, and directly run by the processor. C++/CLI runs atop the .Net framework, which means it's compiled as it's being run by the .Net framework, and it tends to run slower than C or C++.

Take all this with a grain of salt. WriteConsole can do things the others can't easily do. The .Net framework is amazingly fast at certain things. But in general, use the version associated with your lanugage.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • Is it really slower? Don't forget `sync_with_stdio(false)`. – Griwes Nov 09 '12 at 17:52
  • 1
    @Griwes: a lot of testing has shown that under many normal conditions, even with `sync_with_stdio`, it's slightly slower. I'd rather not get into that debate. – Mooing Duck Nov 09 '12 at 17:53
  • 1
    I'm just wondering whether this "slightly slower"-ness is worth mentioning. – Griwes Nov 09 '12 at 17:54
  • @Griwes: Popular stream implementations are calling `sprintf()` under the hood and, thus, are slower. I agree, though, that it's probably not worth mentioning here. – sbi Nov 09 '12 at 17:55
  • I doubt WriteConsole is "hard to use" and certainly is not more "non portable" than WriteLine. cout and printf are portable. But try to write colored text! Moral: before say "don't use", (ahem ... how do printf works on Windows ?!?) less religion and more pragmatism should make you more fair before saying "fast" "slow" "super" "shit" and the like. None of us is actually in the position to define the tradeoffs the OP is in. – Emilio Garavaglia Nov 09 '12 at 18:32
  • @EmilioGaravaglia: It's not "hard to use" so much as "slightly more complicated than all other methods". It _is_ less portable than WriteLine, since WriteLine _works on linux and mac_. I'm not sure what you're saying about printf. All speed measurements I mention are generally accepted. The only value judgement I make here is not to use the WinAPI directly, because 99% of the time, the other functions are better/simpler/more normal. I added a statement to the end to clarify that there are tradeoffs. – Mooing Duck Nov 09 '12 at 18:38
  • @MooingDuck: yes, said in this way it is fair and "normal". So boringly normal ... Don't misundertand me: in general sense I agree. But my point is that "portability" is not always the most important thing. The key point -here- is understand what the problem domain is. As per my perception is, the OP itself knows the existence of all the layers. Hence he will problay liek to know what each layer gives more than the ohther, and what it loose. How do you write colored text on a windows console without native API? Here is where all the iostream fuss dramatically falls. – Emilio Garavaglia Nov 09 '12 at 20:12
1

CLR, Common Language Runtime, is used for building .net applications. If you don't need the .net library, just use the Win32 console.

Cout is a C++ command where printf is a C command. C++ still supports a lot of C commands. Most of the time I use the C++ variant but because there are differences to how they get the job done you may want to use the old C commands in some situations.

Another note: Just a little helpful advice for someone moving from C# to C++: You will need to manage memory on your own. New objects that you create will need to be deleted, there isn't a built in memory manager like there is in C#.

madeFromCode
  • 721
  • 7
  • 15
  • "syntax" and "way" are two completely different and not really related things. – Griwes Nov 09 '12 at 17:51
  • 4
    "memory manager" is not the right term. "garbage collector" is what you meant. Also, modern C++ is about staying as far as possible from manual memory management, and use containers and smart pointers instead of raw `new` and `delete`. – Etienne de Martel Nov 09 '12 at 18:10