Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Reply
LinkBack Thread Tools Display Modes
Old 14-10-2010, 05:31 PM   #1 (permalink)
Sami Hyypiä, LFC legend
 
Liverpool_fan's Avatar
 
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
Default C/C++ Beginner's Guide and Post Basic Questions here


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), 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:
Spoiler:







Mac OS X

Mac OS X users need to install Xcode which you can either get from here along with Mac Dev membership or you can install it from the Applications DVD which you received with your Mac. It's free. You can only choose to install gcc and g++ from the installation wizard.

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 Code:
#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 Code:
#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:
Spoiler:






Steps for Mac OS X users:
Spoiler:





Steps for Linux users:
Spoiler:





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'. There are other few books such as 'C Programming : A Modern Approach', among others. 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.
__________________
Experience true education in Computer Science - http://www.udacity.com | http://www.coursera.org

Spoiler:
Read before asking / messaging any moderator for any query: FAQ + answers for new members

Read all the sticky threads before asking any type of query. Most basic questions are answered in those.
Don't use forum for chatting. Visit http://webchat.freenode.net/?channels=krow, enter nick and connect.

Last edited by ico; 23-10-2010 at 05:32 AM. Reason: Mac OS X update
Liverpool_fan is offline   Reply With Quote
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 15-10-2010, 07:46 AM   #2 (permalink)
Who
Guess Who's Back
 
Who's Avatar
 
Join Date: May 2004
Location: Bangalore
Posts: 352
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

It might be hard to believe (LFC_fan can do something besides trolling) but LFC_fan is right lads,

I however would like to elaborate why exactly we should avoid books by Yashavant P. Kanetkar (Let Us C,Data structures through C,etc) or most indian authors for that matter.

First of all C was actually had invented for Unix by K&R which anyone would know about , TC was made for DOS (DOS compiler) ,it wasn't even intended to be a drop-in sub for ansi-c/gcc/clang/etc, i would say it was a good compiler/IDE Dos however.It is still used in embedded system which use DOS but that's pretty rare nowadays.

So if you are using TC you are wasting time on something so outdated which won't be used beyond your class room, if you are giving interview for a standard company & have no clue of ansi-c/gcc/clang/etc then good luck finding a job.

anyway in summary i would like list out the points why you shouldn't use Yashavant P. Kanetkar or any other indian books for that matter,

1. Like i said before C was written for Unix , yet the author thinks that C was written for Windows DOS, thus your codes are non-portable.

2.Concept like Malloc , Calloc, Free are poorly explained (2 lines actually).

3.Concepts like preprocessor,macros,libraries,memory management , defensive coding etc author thought was too much for our teachers so he decided to skip it (can't hold against him , after all he is a teacher in someways).

4.The books have no history or functionality why the stuff was introduced in the first place, directly jumps to some idiotic fill the blanks , multiple choice, true or false questions (maybe he realize students here need marks so why put details in the first place ? )

5. graphics.h for the ancient turbo c/c++ compiler which enables you to make DOS graphics. programmers had moved on from it 20 years ago. the world is now using opengl and sdl for graphics.

i could go on but i hope by now you guys understand why this books are so bad , it has so many factual errors.


Lastly , i would like to mention a IDE which uses gcc compiler for windows , which served me quite well , called Code::Blocks but again like LFC_fan said it's advisable that you don't(use IDE) at least in the beginning & just stick with Notepad++.
__________________
Stay Hungry. Stay Foolish.

Last edited by Who; 15-10-2010 at 08:18 AM.
Who is offline   Reply With Quote
Old 15-10-2010, 12:32 PM   #3 (permalink)
Excessive happiness
 
furious_gamer's Avatar
 
Join Date: Jun 2008
Location: Bangalore
Posts: 2,974
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

Nice initiative LFC_Fan.

When i first create a new thread for Java related queries i didn't explained anything like this. You did a great job there.

And @Who, yes you are right. Yashwant Kanetkar, the one of the most worst author for C. I never suggest anyone this book, but in colleges some professors suggest this for students.

I faced the same and bought this book but never read it for exams and to learn. I always use online resource and it was more than enough to learn stuff to pass in exams.
__________________
My First Android phone : Samsung Galaxy SL i9003 - Rooted & Gingerbread XXKPQ
Updated : superteekz_V2 ROM for XXKPQ.

PS Request
furious_gamer is offline   Reply With Quote
Old 12-12-2010, 09:30 PM   #4 (permalink)
Wise Old Ghoul
 
Piyush's Avatar
 
Join Date: Feb 2010
Location: GZB.
Posts: 4,428
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

i have installed minGW
but i cant find any launching icon of the compiler
also i need the details of setting variable path
plz do help
thanks
__________________
Steam:http://steamcommunity.com/id/alien007
http://webchat.freenode.net/?channels=krow

"What kind of tomorrow should I dream about tonight"
--Get Backers
Piyush is offline   Reply With Quote
Old 13-12-2010, 12:05 AM   #5 (permalink)
In The Zone
 
cute.bandar's Avatar
 
Join Date: Jan 2010
Posts: 408
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

Can someone suggest an alternative solution to puzzle number 24 from this page . there is a solution given already, but me is wondering if there is another way of solving it . anybody up for the challenge ?
Edit: nvmind i figured out a solution myself , using the toggle technique.

Last edited by cute.bandar; 13-12-2010 at 02:08 PM.
cute.bandar is offline   Reply With Quote
Old 13-12-2010, 03:58 PM   #6 (permalink)
Alpha Geek
 
Join Date: Jan 2007
Location: In your hearts
Posts: 828
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

Quote:
Originally Posted by Who View Post
It might be hard to believe (LFC_fan can do something besides trolling) but LFC_fan is right lads,

I however would like to elaborate why exactly we should avoid books by Yashavant P. Kanetkar (Let Us C,Data structures through C,etc) or most indian authors for that matter.

First of all C was actually had invented for Unix by K&R which anyone would know about , TC was made for DOS (DOS compiler) ,it wasn't even intended to be a drop-in sub for ansi-c/gcc/clang/etc, i would say it was a good compiler/IDE Dos however.It is still used in embedded system which use DOS but that's pretty rare nowadays.

So if you are using TC you are wasting time on something so outdated which won't be used beyond your class room, if you are giving interview for a standard company & have no clue of ansi-c/gcc/clang/etc then good luck finding a job.

anyway in summary i would like list out the points why you shouldn't use Yashavant P. Kanetkar or any other indian books for that matter,

1. Like i said before C was written for Unix , yet the author thinks that C was written for Windows DOS, thus your codes are non-portable.

2.Concept like Malloc , Calloc, Free are poorly explained (2 lines actually).

3.Concepts like preprocessor,macros,libraries,memory management , defensive coding etc author thought was too much for our teachers so he decided to skip it (can't hold against him , after all he is a teacher in someways).

4.The books have no history or functionality why the stuff was introduced in the first place, directly jumps to some idiotic fill the blanks , multiple choice, true or false questions (maybe he realize students here need marks so why put details in the first place ? )

i could go on but i hope by now you guys understand why this books are so bad , it has so many factual errors.


Lastly , i would like to mention a IDE which uses gcc compiler for windows , which served me quite well , called Code::Blocks but again like LFC_fan said it's advisable that you don't(use IDE) at least in the beginning & just stick with Notepad++.
yes, i agree with u.
I read Let Us C, but didn't liked that book. This book was preferred by our teachers in my college. But I studied Programming with C by Gottfried. Although, I already have a good background of programming in Python and Visual Basic. So, studying C was not difficult for me. But if you dont have any programming background and C is your first programming language. Then dont go with Let Us C, as you cannot find out approach of solving a problem. Instead refer foreign authors.
abhijangda is offline   Reply With Quote
Old 18-12-2010, 11:05 PM   #7 (permalink)
Wise Old Ghoul
 
Piyush's Avatar
 
Join Date: Feb 2010
Location: GZB.
Posts: 4,428
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

can someone help me out?
post #5
__________________
Steam:http://steamcommunity.com/id/alien007
http://webchat.freenode.net/?channels=krow

"What kind of tomorrow should I dream about tonight"
--Get Backers
Piyush is offline   Reply With Quote
Old 19-12-2010, 01:02 AM   #8 (permalink)
Sami Hyypiä, LFC legend
 
Liverpool_fan's Avatar
 
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

^ Click on the Show buttons with the Spoilers. Those illustrate those very questions. And oh this is just a compiler, no launching IDE,etc. If you want IDE, Geany will couple well with MinGW - Geany - FOSS Powered Wiki
__________________
Experience true education in Computer Science - http://www.udacity.com | http://www.coursera.org

Spoiler:
Read before asking / messaging any moderator for any query: FAQ + answers for new members

Read all the sticky threads before asking any type of query. Most basic questions are answered in those.
Don't use forum for chatting. Visit http://webchat.freenode.net/?channels=krow, enter nick and connect.
Liverpool_fan is offline   Reply With Quote
Old 19-12-2010, 09:28 PM   #9 (permalink)
Wise Old Ghoul
 
Piyush's Avatar
 
Join Date: Feb 2010
Location: GZB.
Posts: 4,428
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

thanks^^
env variables done
__________________
Steam:http://steamcommunity.com/id/alien007
http://webchat.freenode.net/?channels=krow

"What kind of tomorrow should I dream about tonight"
--Get Backers
Piyush is offline   Reply With Quote
Old 26-01-2011, 02:23 PM   #10 (permalink)
Alpha Geek
 
Join Date: Jan 2007
Location: In your hearts
Posts: 828
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

Can any one tell me, how i can perform file operations copy, move in C??
abhijangda is offline   Reply With Quote
Old 31-01-2011, 04:13 PM   #11 (permalink)
Apprentice
 
shiwa436's Avatar
 
Join Date: Oct 2010
Posts: 52
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

I am unable to download the minGM compiler, please help me in this regard..
shiwa436 is offline   Reply With Quote
Old 31-01-2011, 04:56 PM   #12 (permalink)
Excessive happiness
 
furious_gamer's Avatar
 
Join Date: Jun 2008
Location: Bangalore
Posts: 2,974
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

^^ google it buddy, google it
__________________
My First Android phone : Samsung Galaxy SL i9003 - Rooted & Gingerbread XXKPQ
Updated : superteekz_V2 ROM for XXKPQ.

PS Request
furious_gamer is offline   Reply With Quote
Old 31-01-2011, 09:09 PM   #13 (permalink)
Alpha Geek
 
mohityadavx's Avatar
 
Join Date: Nov 2010
Location: Gurgaon (Delhi NCR)
Posts: 574
Lightbulb Re: C/C++ Beginner's Guide and Post Basic Questions here

I am a student and can't understand why the author of this post is telling us not to use turbo C++ thats the compiler they teach us in school and college
mohityadavx is offline   Reply With Quote
Old 01-02-2011, 09:25 AM   #14 (permalink)
XLr8
 
arpanmukherjee1's Avatar
 
Join Date: Sep 2008
Posts: 637
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

^^ because tc++ is ::>
a dinosaur compiler,
does not meet current compiling standards,
lousy memory management,
does not generate 32 bit or 64bit exe's
will provide help that has bunch of deprecated and harmful functions
the teacher(s) in colleg dont know what actual industry standards are

u on the other hand must know proper coding techniques and actually understand the standards

Standard ECMA-372
__________________
Quote:
There are more things in heaven and earth, Horatio,
Than are dreamt of in your philosophy.
arpanmukherjee1 is offline   Reply With Quote
Old 01-02-2011, 11:03 AM   #15 (permalink)
Excessive happiness
 
furious_gamer's Avatar
 
Join Date: Jun 2008
Location: Bangalore
Posts: 2,974
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

Quote:
Originally Posted by mohityadavx View Post
I am a student and can't understand why the author of this post is telling us not to use turbo C++ thats the compiler they teach us in school and college
If its been used by your staffs in college doesn't mean that TC++ is a good compiler. It has lot of loop-holes which was mentioned by @arpanmukherjee1 very well.

Still there are lot of compilers out there which literally tear TC++ into pieces by its performance and features, and whatever fact there is. That's why @OP suggested not to use TC++.

So better get MinGW or any other equivalent compiler.
__________________
My First Android phone : Samsung Galaxy SL i9003 - Rooted & Gingerbread XXKPQ
Updated : superteekz_V2 ROM for XXKPQ.

PS Request
furious_gamer is offline   Reply With Quote
Old 07-02-2011, 05:45 PM   #16 (permalink)
Ron
||uLtiMaTE WinNER||
 
Ron's Avatar
 
Join Date: Nov 2006
Location: Kathmandu,Nepal
Posts: 698
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

Bookmarked!

Why does the memory allocation of data type in changes with platform? What role Win 32/64 bits plays in size of data type.

In Sumita Arora the size of int is decalred as 2 bytes however in walter savich its 4 bytes. What is the cause of differnce?

I m using Win 7 64 bit window where i m able to cout the int value greater than 65535? Any reason for that
__________________
||uLtiMaTE WinNER||
Ron is offline   Reply With Quote
Old 08-02-2011, 01:39 PM   #17 (permalink)
XLr8
 
arpanmukherjee1's Avatar
 
Join Date: Sep 2008
Posts: 637
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

Quote:
Originally Posted by Ron View Post
Why does the memory allocation of data type in changes with platform? What role Win 32/64 bits plays in size of data type.
that`s a very good question.
int itself is mapped acc. to compiler. 32 bit compiler generates _int32 and 64 bit generates _int64. but after compilation it is not changed. _int32 will not run as _int64 on 64 bit machine and _int64 will be truncated on 32 bit machine.

to avoid confusion use ::> C++ Sized Integer Types

also note not all data types work in this fashion
see :
int on a 64-Bit machine
How INT_PTR datatype works between 64Bit and 32Bit
BigArray<T>, getting around the 2GB array size limit - SpankyJ - Site Home - MSDN Blogs

Quote:
Originally Posted by Ron View Post
In Sumita Arora the size of int is decalred as 2 bytes however in walter savich its 4 bytes. What is the cause of differnce?
Data Type Ranges (C++)

int is 4 bytes. forget sumita arora.

Quote:
Originally Posted by Ron View Post
I m using Win 7 64 bit window where i m able to cout the int value greater than 65535? Any reason for that
65535 is max for unsigned 16bit int. default is int32 hence the greater range.

__________________
Quote:
There are more things in heaven and earth, Horatio,
Than are dreamt of in your philosophy.

Last edited by arpanmukherjee1; 08-02-2011 at 01:44 PM.
arpanmukherjee1 is offline   Reply With Quote
Old 20-02-2011, 03:25 PM   #18 (permalink)
BIOS Terminator
 
nims11's Avatar
 
Join Date: Apr 2008
Location: Ranchi
Posts: 816
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

Why to avoid global variables? any major reason other than code clarity and avoiding any confusion. i find it tempting to use global variables as it becomes quite easier for my functions to access and change it without the use of pointers but i try resist this temptation because people saying "global variable are bad".
nims11 is offline   Reply With Quote
Old 20-02-2011, 03:46 PM   #19 (permalink)
Sami Hyypiä, LFC legend
 
Liverpool_fan's Avatar
 
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

Global variables are bad because every function (and class method) can change it and there may be changes to it which you didn't expect.
__________________
Experience true education in Computer Science - http://www.udacity.com | http://www.coursera.org

Spoiler:
Read before asking / messaging any moderator for any query: FAQ + answers for new members

Read all the sticky threads before asking any type of query. Most basic questions are answered in those.
Don't use forum for chatting. Visit http://webchat.freenode.net/?channels=krow, enter nick and connect.
Liverpool_fan is offline   Reply With Quote
Old 23-02-2011, 01:03 AM   #20 (permalink)
Ron
||uLtiMaTE WinNER||
 
Ron's Avatar
 
Join Date: Nov 2006
Location: Kathmandu,Nepal
Posts: 698
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

Quote:
Originally Posted by Liverpool_fan View Post
Global variables are bad because every function (and class method) can change it and there may be changes to it which you didn't expect.
Bro can u elaborate with an xample

---------- Post added at 12:56 AM ---------- Previous post was at 12:55 AM ----------

2arpanmukherjee1
thnks bro

---------- Post added at 01:03 AM ---------- Previous post was at 12:56 AM ----------

Output Problem

cout<<a++<<a++<<b--<<--b<<++b<<--b;

I m havin problem in assumin the ouput of such syntax. Usullly i get unexpected results. Any reason for tht and way to encounter it?
__________________
||uLtiMaTE WinNER||
Ron is offline   Reply With Quote
Old 23-02-2011, 07:38 AM   #21 (permalink)
BIOS Terminator
 
nims11's Avatar
 
Join Date: Apr 2008
Location: Ranchi
Posts: 816
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

Quote:
Originally Posted by Ron View Post
[/COLOR]Output Problem

cout<<a++<<a++<<b--<<--b<<++b<<--b;

I m havin problem in assumin the ouput of such syntax. Usullly i get unexpected results. Any reason for tht and way to encounter it?
let a=1 and b=2 before this 'cout' statement.
then here is what happens when "cout<<a++<<a++<<b--<<--b<<++b<<--b;" is executed.

a++
output a; a+=1;
a is now 2, b is now 2;
output status : 1

a++
output a; a+=1;
a is now 3, b is now 2;
output status : 12

b--
output b; b-=1;
a is now 3, b is now 1;
output status : 122

--b
b-=1;output b;
a is now 3, b is now 0;
output status : 1220

++b
b+=1;output b;
a is now 3, b is now 1;
output status : 12201

--b
b-=1;output b;
a is now 3, b is now -1;
output status : 122010


Final Output- 122010.
nims11 is offline   Reply With Quote
Old 23-02-2011, 09:18 PM   #22 (permalink)
Electronic.
 
Neuron's Avatar
 
Join Date: Jun 2010
Location: Elsweyr
Posts: 530
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

^^It depends on the compiler IMO.
__________________
I don't always make sense,but when I do,I don't.
Neuron is offline   Reply With Quote
Old 23-02-2011, 10:05 PM   #23 (permalink)
Sami Hyypiä, LFC legend
 
Liverpool_fan's Avatar
 
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

Quote:
Originally Posted by Neuron View Post
^^It depends on the compiler IMO.
This. It falls under the domain what we call "Undefined Behaviour".

Ron: Try compiling your C++ code with g++ -Wall flag and see the warning.

As for global variables.
c++ - Are global variables bad? - Stack Overflow
Global Variables Are Bad
__________________
Experience true education in Computer Science - http://www.udacity.com | http://www.coursera.org

Spoiler:
Read before asking / messaging any moderator for any query: FAQ + answers for new members

Read all the sticky threads before asking any type of query. Most basic questions are answered in those.
Don't use forum for chatting. Visit http://webchat.freenode.net/?channels=krow, enter nick and connect.
Liverpool_fan is offline   Reply With Quote
Old 24-02-2011, 04:01 PM   #24 (permalink)
127.0.0.1
 
mitraark's Avatar
 
Join Date: Nov 2010
Location: Kolkata / Durgapur
Posts: 769
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

Any software all in one package i can use to write and compile C [ GCC ] >?? DevC++ or something ??? Something which can be installed on machines without Internet connection [ unlike MinGW which is downloading files ]

---------- Post added at 04:01 PM ---------- Previous post was at 03:49 PM ----------

MinGW is working great , will i be able to install it in Windows 7 6 Bit PC without Internet connection ?
__________________
Intel i5 760 || Gigabyte H55 || Corsair 4GB 1333MHz Value || XFX HD5670 1GB
WD 2TB Green + Seagate 5900RPM 2 TB + Seagate Freeagent 1 TB x2
Corsair CX400 || Zebronics Bijli || Samsung P2350 23.5" || Altec Lansing VS2621
My Speedtest :D
mitraark is offline   Reply With Quote
Old 24-02-2011, 04:09 PM   #25 (permalink)
I am the night! I am....
 
vickybat's Avatar
 
Join Date: Aug 2009
Location: Gotham City
Posts: 4,209
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

Devc++ is the best imo.
__________________
core i5 750, biostar h55 A+ (X16+X4), 4gb 1333 ddr3, corsair vx450, cm elite 335,Asus EAH 5750 FORMULA , samsung 2033 sw plus, wd green 1tb , wd 1tb my book , hp dvd writer,Apc 650 va, logitech z313
vickybat is offline   Reply With Quote
Old 24-02-2011, 05:07 PM   #26 (permalink)
ico
.
 
ico's Avatar
 
Join Date: Jun 2007
Location: New Delhi
Posts: 8,944
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

Quote:
Originally Posted by mohityadavx View Post
I am a student and can't understand why the author of this post is telling us not to use turbo C++ thats the compiler they teach us in school and college
Let me put this straightforwardly. Indians are idiots. Not even Pakistanis use Turbo C++.

Teachers are not willing to move on. The curriculum makers are sleeping. et cetera.

---------- Post added at 05:07 PM ---------- Previous post was at 05:03 PM ----------

Quote:
Originally Posted by mitraark View Post
Any software all in one package i can use to write and compile C [ GCC ] >?? DevC++ or something ??? Something which can be installed on machines without Internet connection [ unlike MinGW which is downloading files ]

---------- Post added at 04:01 PM ---------- Previous post was at 03:49 PM ----------

MinGW is working great , will i be able to install it in Windows 7 6 Bit PC without Internet connection ?
Try this out: CodeLite IDE LiteEditor/Download

Here is the direct link: http://sourceforge.net/projects/code...1.exe/download
__________________
.
ico is offline   Reply With Quote
Old 24-02-2011, 05:10 PM   #27 (permalink)
I am the night! I am....
 
vickybat's Avatar
 
Join Date: Aug 2009
Location: Gotham City
Posts: 4,209
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

^^ Good point made ico.We should move on. But ain't devc++ suffice?
__________________
core i5 750, biostar h55 A+ (X16+X4), 4gb 1333 ddr3, corsair vx450, cm elite 335,Asus EAH 5750 FORMULA , samsung 2033 sw plus, wd green 1tb , wd 1tb my book , hp dvd writer,Apc 650 va, logitech z313
vickybat is offline   Reply With Quote
Old 24-02-2011, 05:13 PM   #28 (permalink)
ico
.
 
ico's Avatar
 
Join Date: Jun 2007
Location: New Delhi
Posts: 8,944
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

Quote:
Originally Posted by vickybat View Post
^^ Good point made ico.We should move on. But ain't devc++ suffice?
It will easily suffice but Dev C++ has not seen any development since 6 years.
__________________
.
ico is offline   Reply With Quote
Old 24-02-2011, 05:17 PM   #29 (permalink)
I am the night! I am....
 
vickybat's Avatar
 
Join Date: Aug 2009
Location: Gotham City
Posts: 4,209
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

^^Okay, so whats widely used in development now?
__________________
core i5 750, biostar h55 A+ (X16+X4), 4gb 1333 ddr3, corsair vx450, cm elite 335,Asus EAH 5750 FORMULA , samsung 2033 sw plus, wd green 1tb , wd 1tb my book , hp dvd writer,Apc 650 va, logitech z313
vickybat is offline   Reply With Quote
Old 24-02-2011, 05:25 PM   #30 (permalink)
ico
.
 
ico's Avatar
 
Join Date: Jun 2007
Location: New Delhi
Posts: 8,944
Default Re: C/C++ Beginner's Guide and Post Basic Questions here

Quote:
Originally Posted by vickybat View Post
^^Okay, so whats widely used in development now?
Anything with which you are comfortable with.

Eclipse, Netbeans and Microsoft Visual C++ are excellent. But many of their features are unnecessary if you are only into basic programming.

Dev C++ comes with an old version of MiniGW by default.
__________________
.
ico is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Charan
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 03:22 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2