# I learnt C without realising it....how the?

This topic is 4588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm going through my c++ beginner's tutorial and I get kinda confused....because I'm really bored I go on a C beginner's tutorial and zip through and realise I accidentally learnt C.... what's the difference? printf() and cout<<, same thing really...I don't believe it! Now that I learnt the basics of C, maybe I should extend my knowledge (which came from nowhere) and go for more advanced things.... And I need you guys to tell me how I can extend my basic knowledge to smething more advanced....maybe game development (OpenGL or DirectX)!? Or should I simply continue learning c++ because I'll get a better result in the end.... RECENTLY EDITED: Obviously, these are only the basics so I need to stay on the lines...I'm not some big professional C programmer...I don't think too big of myslef....

Learn C++.

##### Share on other sites
Well.....there is something called a GOTO command and there are such things called memory pointers which allow me to access blocks of memory and even change them...I don't necessarily have to learn OOP to be able to re-use and change blocks of data.

##### Share on other sites
what header files do you use.

##### Share on other sites
Quote:

I don't understand what a header file is (I'm gonna get flamed for this).

##### Share on other sites
There are many things that C and C++ can both do, so from that respect there isn't a lot of difference.

However, whilst some things are easier and you'll find yourself using some C functions in your C++ programs (sprintf() being one I use), I personally find C cumbersome, clumsy and confusing when compared to the new streamlined way of doing things as introduced by C++.

C++ also offers more advanced features such as classes and templates, OO concepts which C doesn't really know. C does run faster than C++ due to it being at a slightly lower level, but this probably won't concern a beginner much.

C is easier imho, but C++ will probably prove to be more useful. I use C samples to get the basic logic nailed down (like sorting algorithms), and then migrate the code to C++.

I hope this has helped, just go with what you feel is best, and good luck!

ukdeveloper.

##### Share on other sites
whats the first line you use in your programs

##### Share on other sites
It has helped, UKdeveloper!

I will continue with both C and C++...I hope you guys will still reply to my c++ help topic...I personally think that c++ is very complicated so I'll use C mainly until I truelly finish learning c++!

##### Share on other sites
Quote:
 Original post by theadamSGTwhats the first line you use in your programs

In C?

#INCLUDE <STDIO.H>

In c++:

#include <iostream>

okay.

##### Share on other sites
Quote:

And what? Now you know...how are you gonna help?

##### Share on other sites
just a chance that you might have been learning c++ because c++ can use printf too
if you want to learn c++ here it is http://www.gamedev.net/community/forums/topic.asp?topic_id=333728

##### Share on other sites
Quote:
 Original post by theadamSGTjust a chance that you might have been learning c++ because c++ can use printf tooif you want to learn c++ here it is http://www.gamedev.net/community/forums/topic.asp?topic_id=333728

Cool...I'll try using it myself....(why do they always make you use cout<< in c++?)

##### Share on other sites
i dont have to its just because thats what i learned

##### Share on other sites
Quote:
 Original post by theadamSGTi dont have to its just because thats what i learned

The world is full of some dumb, dumb people....

##### Share on other sites
Quote:
Original post by ReneGade RG dev
Quote:
 Original post by theadamSGTjust a chance that you might have been learning c++ because c++ can use printf tooif you want to learn c++ here it is http://www.gamedev.net/community/forums/topic.asp?topic_id=333728

Cool...I'll try using it myself....(why do they always make you use cout<< in c++?)

I'm a newb myself when it comes to C++ (and other programming languages) but my response is probably more useful than the previous too.

<< Is a operator, similar to +,-,/,*,^ etc. and both cout and "Hello World" are operands.

Since << looks to the right of it (this may seem vague but I think you've just got to accept this, it's how it's programmed), i.e.

cout << "Hello World"

<< looks to the right of itself and it applies what it finds ("Hello World") to the function cout. cout stands for console output.

Since I'm new and haven't really used it thoroughly I don't see why they don't just have a function like the one in C#: Console.Write("Hello World"); which is much neater. I assume it's for flexibility in terms of memory management.

##### Share on other sites
If you're continuing with C++ you'll learn why cout is better than printf.

But for now just accept that the c++ stream-model is typesafe while the c stream-model isn't.

##### Share on other sites
Quote:
 Original post by TomXSince I'm new and haven't really used it thoroughly I don't see why they don't just have a function like the one in C#: Console.Write("Hello World"); which is much neater. I assume it's for flexibility in terms of memory management.

In the end, the difference between:
a + b;

and:
a.add(b);

is just style.

shmoove

##### Share on other sites
Quote:
 C does run faster than C++ due to it being at a slightly lower level, but this probably won't concern a beginner much.

C++ is not inherently slower that C.

Quote:
 Since I'm new and haven't really used it thoroughly I don't see why they don't just have a function like the one in C#: Console.Write("Hello World"); which is much neater. I assume it's for flexibility in terms of memory management.

It's nothing to do with memory management, as shmoove said the difference between the two is just style. Now consider this code:

cout.write("Hello world here's a number ").write(number).write(" here's a string  ").write(string);cout << "Hello world here's a number: " << number << " here's a string " << string;

Personally I far prefer the second option. Note that type-safe variable length argument lists are not available so using the same style as the .Net class library's Console.Write is not an option.

##### Share on other sites
Quote:
 Original post by shmooveIn the end, the difference between:a + b;and:a.add(b);is just style.
a.add(b.add(c.sub(d.add(e.add(f)))));
I'd say there's more than style to it.

Quote:
 Original post by ukdeveloperC does run faster than C++ due to it being at a slightly lower level, but this probably won't concern a beginner much.
False.

Quote:
 Original post by ReneGade RG dev(why do they always make you use cout<< in c++?)
First off, "they" don't make you use anythng. Second, it's cout, not cout<<: cout is an object (it encapsulates a representation of the console output stream and inherits a set of generic stream properties), with operator << overloaded to serve as its insertion mechanism. The insertion operator is overloaded for all intrinsic and standard library types with meaningful textual representation (it doesn't make any sense to print a vector, but it makes sense to print the strings in the vector) and can be overloaded by the developer for user-defined types (you can create an insertion operator for your vector3d type and then be able to write cout << v3d;).

The insertion operator is typesafe, unlike "the incredible exploding printf" in C (pass data with the wrong format specifier and enjoy the possible fireworks), as well as marginally less vulnerable to buffer exploits. These are just a few of the advantages of cout over printf, but most of them won't become meaningful to you until you start to do more than just print text to the screen. Give it time and you'll come to see that it's a Good Thing™.

Happy hacking.

##### Share on other sites
First, C++ is really a very different language from C. It just happens to be built upon C, and have bent over backwards to provide compatibility with existing "legacy" C code - but proper, real-world C++ code looks quite different from proper, real-world C code. Any tutorial which claims otherwise is at best misguided and outdated.

Second, it's generally higher-level languages that are "slower"; however, any speed comparison between two languages is generally just plain wrong. The way the code is written can always make up the difference. :) In particular, C++ really is higher-level because it offers a greater range of abstractions; it is further away from the hardware and lets you think in more abstract concepts as opposed to what is actually happening on the hardware - whereas C was designed as a sort of 'super-assembly language'. However, it is also designed to use the C syntax for the most part, and only "automatically generate things" as far as it can without adding overhead.

For example, C++ makes you mark class member functions as "virtual" explicitly, simply because if there are none, it won't have to generate a vtable, such that the data layout becomes identical to what a C compiler would produce. But if the virtualness is needed, the compiler will do all the work there and build it in, at the low low cost of four extra bytes (typically) per object and two indirections per function call (only for the ones that actually are virtual).

IMHO, there's really no good reason to restrict yourself to using C any more, because 99% or so of C is valid C++, and the rest can be easily translated without any performance hit. However, real C++ programmers should be making use of C++ features as well, and there are a lot of them - but they will save you lots of grief and aggravation.

Third ... read what Oluseyi said about cout and printf. :) They are *very much* not the same thing.

##### Share on other sites
Quote:
 Original post by ZahlmanIMHO, there's really no good reason to restrict yourself to using C any more, because 99% or so of C is valid C++, and the rest can be easily translated without any performance hit.
heh. [smile]

##### Share on other sites
Quote:
Original post by Oluseyi
Quote:
 Original post by shmooveIn the end, the difference between:a + b;and:a.add(b);is just style.

a.add(b.add(c.sub(d.add(e.add(f)))));

I'd say there's more than style to it.

[grin]
But that's still style. The operator overloading way is more stylish, but in the end both ways do the same thing.

shmoove

##### Share on other sites
Quote:
Original post by shmoove
Quote:
 Original post by Oluseyia.add(b.add(c.sub(d.add(e.add(f)))));I'd say there's more than style to it.
[grin]
But that's still style. The operator overloading way is more stylish, but in the end both ways do the same thing.
Readability? Maintainability? Comprehensibility? That's an incredibly simple example, and it's already much more challenging to understand. Create one that's an order of magnitude more complex and you have the potential for real damage - including having one misplaced parenthesis potentially alter the entire expression (imagine having the argument to one of those add operations be a compound expression, then misplacing an internal parenthesis).

style is substance, and we should employ every stylistic convention that improves code quality - and, where appropriate, codify them in the language spec.

##### Share on other sites
Using << notation for stream output has two main benefits from my point of view, and they are closely linked.

1. It provides a concise and consistent interface for stream output. That means the syntax is EXACTLY the same for any object supporting stream output. You don't have to worry about calling the right function on an object if you implemented your operator correctly, nor do you have to worry about misplaced parentheses.

2. Once you have got used to the syntax, it emphasises the action, i.e. stream output, rather than how we got there, i.e. a function call because the << operator is used pretty much exclusively for text mode stream output. (i.e. any programmer worth his salt can spot it from a mile off)

As I said the two are closely tied.

EDIT: My spelling left much to be desired