C/C++ Beginner's Guide and Post Basic Questions here

potatoboy

Broken In
Greetings, coders!

I'm pursuing my Bachelor of Computer Applications degree(BCA) in Bangalore, and boy was I shocked when I read the first few posts on this thread. I knew the Turbo C compiler was old, but WOW.Why do colleges and universities still insist on teaching on such an outdated platform?
On a side note, I want to stray away from Turbo CPP after becoming enlightened. I've learnt the nuances of C++ and have some moderate knowledge of C, but I NEED to become better. Maybe even start learning Python. Any help would be greatly appreciated! TIA!

On another side note, my teacher even told us at one point that TC was old as hell, but he taught us with it merely because it was in the syllabus. *facepalm* Shows that most teachers merely want to teach students for the sake of marks.

EDIT : now I'm really starting to feel like sheep in a herd. I was led all these days to thinking that integers have 2 bytes of memory where it is 4, and we used conio.h ALL these days in the lab(guess which compiler!) I really want to start over again from scratch :(
 
Last edited:

Desmond

Destroy Erase Improve
Staff member
Admin
Greetings, coders!

I'm pursuing my Bachelor of Computer Applications degree(BCA) in Bangalore, and boy was I shocked when I read the first few posts on this thread. I knew the Turbo C compiler was old, but WOW.Why do colleges and universities still insist on teaching on such an outdated platform?
On a side note, I want to stray away from Turbo CPP after becoming enlightened. I've learnt the nuances of C++ and have some moderate knowledge of C, but I NEED to become better. Maybe even start learning Python. Any help would be greatly appreciated! TIA!

On another side note, my teacher even told us at one point that TC was old as hell, but he taught us with it merely because it was in the syllabus. *facepalm* Shows that most teachers merely want to teach students for the sake of marks.

EDIT : now I'm really starting to feel like sheep in a herd. I was led all these days to thinking that integers have 2 bytes of memory where it is 4, and we used conio.h ALL these days in the lab(guess which compiler!) I really want to start over again from scratch :(

There is nothing wrong with learning on Turbo C++ as long as you are able to distinguish between it and modern C++ variants.

It also helps if you code on gcc as well.
 

v.Na5h

Off Hook!
What ide do you guys use..

I used to use code blocks.. But found out that netbeans 8.0.2 is way better
 

priyar

Banned
I have learnt C,C++ can i create any software with this technology and learning Cloud Computing is also very useful.
 

TheSloth

The Slowest One
in windows you cannot create a file/folder with a same name as a existed file/folder. You must give a different name. like there is one folder called MyPics. In that folder there is a file named pic1.jpg. Now if you try to rename another file as pic1.jpg, windows will not allow. But you can have a file named pic1.jpg in some other folder.

Similarly, when you have two or more functions of same name then the compiler will face ambiguity problem, i.e. which one to call. Suppose if you create a function called getsize() then you cannot have another function as getsize(). Compiler will say "getsize() is already defined" .
So you create two namespaces, viz. namespace one and namespace two. and define getsize() function in each as per your requirement.
to use that particluar function you type as one::getsize();
OR
you just declare the namespace you are goin to use before writing the code
using namespace one;
int main()
{
getsize();
return 0;
}

the statement you have given is :
using namespace std; // here std is the name of the directory you are goin to use

PS:Wait for others to comment, I am not 100% sure as I studied C++ 3 yrs back
 

aaruni

The Linux Guy
in windows you cannot create a file/folder with a same name as a existed file/folder. You must give a different name. like there is one folder called MyPics. In that folder there is a file named pic1.jpg.
Now if you try to rename another file as pic1.jpg, windows will not allow. But you can have a file named pic1.jpg in some other folder.

Similarly, when you have two or more functions of same name then the compiler will face ambiguity problem, i.e. which one to call. Suppose if you create a function called getsize() then you cannot have another function as getsize(). Compiler will say "getsize() is already defined" .
So you create two namespaces, viz. namespace one and namespace two. and define getsize() function in each as per your requirement.
to use that particluar function you type as one::getsize();
OR
you just declare the namespace you are goin to use before writing the code
using namespace one;
int main()
{
getsize();
return 0;
}

the statement you have given is :
using namespace std; // here std is the name of the directory you are goin to use

PS:Wait for others to comment, I am not 100% sure as I studied C++ 3 yrs back

I'm not sure that this is the reason, but it works this way. You can either use

Code:
using namespace std;

at the beginning of the program, or you can type

Code:
std::cin
std::cout
....

throughout your program.

When asked to comment in my C++ project, why I had to write
Code:
using namespace std;
, I commented the following

Code:
using namespace std;	//to avoid std::cin, std::cout, std::fstream, etc.
 

TheSloth

The Slowest One
[MENTION=134449]aaruni[/MENTION] That actually make more sense and really summed up my post. std is a library, and cin, cout and fstream are declared inside std. Thanks man. I hope OP will understand better now.
 

sampada

Right off the assembly line
Moving and permissions. If you move a file from one location to another, the file generally retains its permissions settings. Thus, if you move an application from the Applications folder to your Home directory, it will retain system and admin as its owner and group.

Conversely, if you move a file from your Home directory to the Applications folder, it will retain the permissions settings it had in your Home directory. Depending on what those settings were, nonadministrative users may not be able to launch the file due to insufficient access. In such a case, the solution is to modify the file so that it acquires the system and admin attributes of most other files in the Applications folder.

Copying and permissions. If you copy a file from one location to another, the copy generally acquires the attributes of its enclosing folder. Thus, if you copy a file from Applications to your Home directory, it will acquire your user name as the owner and staff as the group.

Conversely, if you copy a file from your home directory to the Applications folder, its permissions change to that of system and admin. Thus, you are no longer the owner of the file you just copied. In this slightly odd situation, you may be able to modify the privileges settings (via Mac OS X's Show Info window) of the original file but not those of the copy.
 

TheSloth

The Slowest One
@shanmorkel1685 buddy use some other IDE like Eclipse or Codeblocks instead of ancient Turbo C/C++. You won't find it much difficult to use.
 

aaruni

The Linux Guy
+1 . Use something other than TC++ .

But, if for whatever reason, you absolutely need to run TC++, install it in DosBox.

TC++ Under DOSBox – Aaruni's Blo (note that the guide points to Dosbox website for dosbox install. in ubuntu, just install dosbox via apt)
 

Johnny1

Broken In
So you're willing to write your own code in C or C++, or simply compile someone else's code? This guide has been designed to get you started.

References and Source for some of the content
FAQ: Compiling your first C or C++ programs - Ubuntu Forums
Welcome to FOSS Powered Wiki - FOSS Powered Wiki

Comments and correction of errors will really be appreciated.

1. Make sure the compiler is installed

Basically if you are starting, you are strongly recommended to use standard compilers, like The GNU C Compiler (gcc), LLVM, Microsoft Visual C++, etc. Stay AWAY from antiquated compilers like Turbo C et al, with them you will NEVER learn proper programming* and will develop really horrible practices. This thread will basically cover the GNU C Compiler.
Depending on your operating system, it depends on how to set up your GNU C Compiler. I will mainly focus on the popular operating systems such as Linux and Windows.

Windows

In order to install MinGW, first of all download the executable and execute your downloaded executable in order to facilitate the installation of MinGW. Download the installer from this site.
Make sure you install the packages for gcc and g++ in the installer.
Next make sure you set up the paths properly, this can be done by going to system properties (Properties of My Computer), and set the environment variable PATH by adding the location of the compiler's path. Click on the spoiler to view:
*img4.imageshack.us/img4/8531/path3.png
*img12.imageshack.us/img12/1399/path4.png
*img11.imageshack.us/img11/6822/path5.png
*img18.imageshack.us/img18/1511/path6.png

Mac OS X

Mac OS X users need to install Xcode which you can either get from the App Store. Starting from Xcode 4.3, the Command Line tools are not bundled by default. They can be installed by opening Xcode and going to Preferences --> Downloads --> Components --> Command Line tools.

I'll suggest to use TextWrangler as your preferred text editor.

Ubuntu

Make sure that the build-essential package is installed in the system.
You can install the build-essential package by running the terminal and passing the command.

Code:
sudo apt-get install build-essential

Alternatively you can use the Software Center or Synaptic Package Manager to search build-essential and install it.

OpenSUSE

Open YAST. Go to Software Management.
Change the Filter to 'Patterns' and select C/C++ Compiler and Tools.
Click Accept. And install.

Fedora

Go to terminal as root.
For running it as root, type

Code:
su

Then pass the comand.(exact as in the quotes):

Code:
yum groupinstall "Development Tools" "Legacy Software Development"

Arch Linux

The development packages for C/C++ Programming is already bundled in Arch Linux. However the package is: base-devel. You can use the pacman package manager in case any package or library is missing.

Others

See here: Installing GCC - GNU Project - Free Software Foundation (FSF)

2(a). Write your first C Program

Kick open your IDE/text editor, make sure it is a plain text editor and not a binary editor like the Word Processors or Wordpad in Windows. Only a plain text editor will do. Windows has Notepad, which can be used however Notepad++ or Crimson Editor is recommended due to syntax highlighting and indention support.
Write the following code in it.

PHP:
#include<stdio.h>
 
int main()
{
    printf("Hello, World!");
    return 0;
}
This is a very simple C program. You can save it as an ASCII text file, say myfirst.c. Make sure of the extension it should be [dot]c, preferably lowecase c in *nix.

2(b). Write your first C++ Program
Similarly for C++ you can try the following code.
PHP:
#include <iostream>

int main()
{
  std::cout << "Hello World!" << std::endl;
  return 0;
}

You can save the file in .cpp extension.

2(c). Compiling

For C
gcc is the C compiler
Obviously, the filaname saved with .c extension is the C source file which the compiler compiles. If you want to compile several source files into a single executable, just list them sequentially delimited by a space.

You can compile a C program with the command in your terminal/Command Prompt.
Code:
gcc myfirst.c -o program.exe
You can replace myfield.c by whichever file you wish to compile.
The -o flag names the name of the executable created, program.exe in this case. If you don't pass that flag and just gcc myfirst.c, then your executable will be a.out in *nix and a.exe in Windows.
Note: Linux users don't need to append .exe at all

For C++
Similarly you can compile a C++ source file by saving it as .cpp and compiling with command:
Code:
g++ myfirst.c -o program.exe

Executing
Execution can be performed by writing the name of the executable created in your Terminal/Command Prompt. Either ./programname in *nix or programname.exe in Windows.

Steps for Windows users:
*img89.imageshack.us/img89/8343/program.png
*img26.imageshack.us/img26/893/savertz.png
*img530.imageshack.us/img530/4053/compile.png

Steps for Mac OS X users:
*img836.imageshack.us/img836/3885/sc1z.jpg
*img259.imageshack.us/img259/2444/sc2n.jpg

Steps for Linux users:
*img152.imageshack.us/img152/9222/screenshot2btw.png
*img2.imageshack.us/img2/3937/screenshot4ymv.png

3. Useful Flags
Following are a few command line switches to enable some warnings and language features (check the man page for detailed information):

-std=gnu99 (C only) This flag enforces the latest C standard (1999) i.e. C99, plus GNU Extensions (this should be explicitely specified)
-ansi This flag checks ANSI compliance
-pedantic This flag issues warnings when strict ISO compatibility is NOT met.
-Wall This flag enables most warnings (very useful, highly recommended)
-Wextra enables more warnings (very useful)
-Wwrite-strings warns when you misuse plain old C string constants (aka. deprecated cast from const char* to char*) (very useful, highly recommended)

I particularly recommend -Wall and -Wwrite-strings, to maximize warnings and common pitfalls. Also remember paranoid programming is good for your code, so never try to ignore warnings.


4. Must Reads
Things to Avoid in C/C++ -- gets() , Part 1 - GIDNetwork
comp.lang.c Frequently Asked Questions
C++ FAQ

5. Some Common Questions Answered

C

Why C?
C is a standard. It is the programmers programming language. It is the standard programming language of GNU and BSD based systems. The majority of these systems and the applications that run on them, is written in C. C was developed over thirty years ago for writing operating systems and applications. It's small, extensible design has allowed it to evolve with the computer industry. Because of it's age and popularity, C is a very well supported language. Many tools exist to make C programming easier and these tools are often very mature and of a high standard. All the software we will use in this book is written in C.
Reference: Why learn C?

C books
If you are willing to buy a printed book, it is strongly suggested to buy 'The C Programming Language by Kernighan and Ritchie' and 'Head First C'. Both are highly recommended. 'C Programming : A Modern Approach' is another fantastic book but an Indian Print is not available and the international edition costs a fortune. These are not absolute beginner guides though. If you are a total beginner, do consider a language like Python before jumping to C. Though if you want to insist with C, go ahead with 'C for dummies'. Pretty much neophyte friendly as it gets. Keep away from books with Non-Standard code, basically just flip through it and if you see void main() or conio.h in it, just keep it back in the shelf.
For online resources on C check these links:
C Programming Language - Free computer books
The GNU C Programming Tutorial
ANSI C Programming: Part 1
C programming.com - Your Resource for C and C++ Programming
comp.lang.c Frequently Asked Questions

C++

Why C++?
C++ is an Object Oriented Programming Language which incorporates the execution speed of C along with OOPS development model which makes it more suitable for large projects.

Can I learn C++ before C?
While you certainly can, however it is highly suggested to first learn sequential and procedural programming concepts in C style, before jumping to the OOPS model of C++. C to C++ is a natural path of learning.

C++ books
There are many great books for C++, 'Thinking in C++' and 'The C++ Programming language' are pretty good books and certainly worth learning. However there are many excellent online resources.
C++ Language Tutorial
Teach Yourself C++ in 21 Days
Bruce Eckel's MindView, Inc: Thinking in C++ 2nd Edition by Bruce Eckel

Editors and IDEs

Editors which offer basic syntax highlighting and indention are recommended. In Windows Notepad++ is an excellent editor, while in Linux you have gedit, kate as GUI editors, and vim, emacs as excellent text based editors.
It's advisable not to use IDEs in the beginning. Stay clear of Netbeans, Eclipse, Anjuta, Visual Studio, MonoDevelop, or any such IDE. Of course, if you are arguing on that, you probably know better and can ignore this advice. One IDE which is suggested is Geany which is basic enough and serves all purposes.


* - You can use them for your school/college, however know which bad practice you are using when you use such compilers and don't depend on them.

Its Great And very Informative Post..I have been looking for a long time.Its awesome. Really Appreciate it. Thanks For Sharing with us.
 

SaiyanGoku

kamehameha!!
Hello,
I have completed my engineering from Computer science branch.
Now I am preparing for <Link Removed>
My engineering concepts are not so clear I mean my programming is not so good.
Suggest me the books I prefer and how can I improve my programming?

You want to study programming for IBPS?
Anyway, just go to tutorials point website.
 

whitestar_999

Super Moderator
Staff member
@SaiyanGoku SO is specialist officer,different from PO & it also includes IT officer for which an IT specific exam is there(though I think it focuses more on networking & theoretical concepts & not so much on coding/application part).
 

quicky008

Technomancer
can anyone provide me with a relatively simple program that can be used to represent an array of integers as a binary tree?
 
Top Bottom