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

Explain me the output of this:

Code:
void main()
{
float a=1.22; 
if (a==1.22) 
printf("C");
else 
printf("cD");
getch();
}

Thats a classic! It's just one of many codes to represent the complex world of floating point numbers. So, this is what happens:

> 1.22 base 10 is (0.0011100001010001111010111000010100011110101110000101...) base 2.

> The bold part is the binary pattern that repeats till infinity.

> As we do not have infinite memory, so to store it in a memory, it is truncated and rounded off to (1.22000003) base 10.

> Since 1.22000003 is NOT equal to 1.22 (which is stored as 1.22000000 in a float variable), so the program executes the false parte instead of the true part of if-else code (debug the program to see the actual values).

> Have a look at this page: 2.5 — Floating point numbers « Learn C++

> Float numbers are beyond any webpage's scope!
 

velociraptor

Broken In
hey i wrote a prog in c++ using notepad and i cant get how to compile and execute it please tell in detail .i m using windows 8 and i have got the MinGW compiler installed
 

nightcrawler

Broken In
Check out the MinGW wiki for First Time Users and the HOWTO guide on their site. It should solve all your problems and queries easily. Unless you are having problems in compiling itself.
 

Vyom

The Power of x480
Staff member
Admin
hey i wrote a prog in c++ using notepad and i cant get how to compile and execute it please tell in detail .i m using windows 8 and i have got the MinGW compiler installed

You can either install Code::blocks, which after integration with MinGW, will give you a full fledge IDE. Or compile and run program from command line.

  1. Press Windows + R.
  2. Open Command Prompt.
  3. With CD command navigate to the "bin" folder where MinGW is installed.
  4. Compile the program:
    g++ cppfile.cpp -o output.exe
  5. And then run the output file by just entering the name of output file and pressing enter.
    output.exe


You could have also used c++ command as an alternative to g++.

PS: Of course you have to keep program in bin folder, or give full path of the cpp filename.
 

gameranand

Living to Play
hey i wrote a prog in c++ using notepad and i cant get how to compile and execute it please tell in detail .i m using windows 8 and i have got the MinGW compiler installed

If you don't want any trouble and don't want to go the conventional way then you can use Code::Blocks. :)
 

nightcrawler

Broken In
In projecteuler.net, do they give solutions too?
No not directly. They give you a problem and ask you for the output. If it is correct you have solved the puzzle. I believe once that is done you could access the forum to get the answer or something. Although not sure. Not been on projecteuler for last 2 years or so.
 

kunalht

In the zone
Hii
I have just completed 12th sci.
Now I have vacation.
I want to learn programming in this vacation.
Which shold I learn HTML or C or C++ or JAVA ????
I want to learn this at home(without any classes)
can i learn it without classes??
& also plz suggest me some website or books or ebooks for learning that....
 

rohitshubham

Ambassador of Buzz
Help Me with this code
Code:
[COLOR=#339933]#include<stdio.h>[/COLOR]
 [COLOR=#993333]void[/COLOR] main[COLOR=#009900]([/COLOR][COLOR=#993333]void[/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900]{[/COLOR]   
 [COLOR=#993333]long[/COLOR] x[COLOR=#339933],[/COLOR]y [COLOR=#339933];
[/COLOR] [URL="*www.opengroup.org/onlinepubs/009695399/functions/scanf.html"][COLOR=#000066]scanf[/COLOR][/URL][COLOR=#009900]([/COLOR][COLOR=#FF0000]"%l %l"[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#339933]&[/COLOR]x[COLOR=#339933],[/COLOR] [COLOR=#339933]&[/COLOR]y[COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR]   [COLOR=#B1B100]if[/COLOR][COLOR=#009900]([/COLOR]x[COLOR=#339933]>[/COLOR]y[COLOR=#009900])
[/COLOR]    [COLOR=#009900]{[/COLOR]    [URL="*www.opengroup.org/onlinepubs/009695399/functions/printf.html"][COLOR=#000066]printf[/COLOR][/URL][COLOR=#009900]([/COLOR][COLOR=#FF0000]"%l"[/COLOR][COLOR=#339933],[/COLOR]y[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]     [COLOR=#009900]}[/COLOR]  
  [COLOR=#B1B100]else[/COLOR]     [COLOR=#009900]{[/COLOR]     
[COLOR=#B1B100]       if[/COLOR][COLOR=#009900]([/COLOR]x [COLOR=#339933]%[/COLOR] [COLOR=#0000DD]5[/COLOR] [COLOR=#339933]==[/COLOR][COLOR=#0000DD]0[/COLOR][COLOR=#009900])[/COLOR]  
      [COLOR=#009900]{[/COLOR]         y[COLOR=#339933]=[/COLOR] y[COLOR=#339933]-[/COLOR]x [COLOR=#339933];[/COLOR]    
               y[COLOR=#339933]=[/COLOR]y[COLOR=#339933]-[/COLOR][COLOR=#800080]0.5[/COLOR][COLOR=#339933];
[/COLOR]               [URL="*www.opengroup.org/onlinepubs/009695399/functions/printf.html"][COLOR=#000066]printf[/COLOR][/URL][COLOR=#009900]([/COLOR][COLOR=#FF0000]"%l"[/COLOR][COLOR=#339933],[/COLOR] y[COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR]     [COLOR=#009900]}[/COLOR]     
      [COLOR=#B1B100]else[/COLOR] 
       [COLOR=#009900]{[/COLOR]            [URL="*www.opengroup.org/onlinepubs/009695399/functions/printf.html"][COLOR=#000066]printf[/COLOR][/URL][COLOR=#009900]([/COLOR][COLOR=#FF0000]"%l"[/COLOR][COLOR=#339933],[/COLOR] y[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]                [COLOR=#009900]}[/COLOR]     
[COLOR=#009900]}
[/COLOR]     [COLOR=#009900]}[/COLOR]
It's Giving me runtime error

Help Me with this code
Code:
[COLOR=#339933]#include<stdio.h>[/COLOR]
 [COLOR=#993333]void[/COLOR] main[COLOR=#009900]([/COLOR][COLOR=#993333]void[/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900]{[/COLOR]   
 [COLOR=#993333]long[/COLOR] x[COLOR=#339933],[/COLOR]y [COLOR=#339933];
[/COLOR] [URL="*www.opengroup.org/onlinepubs/009695399/functions/scanf.html"][COLOR=#000066]scanf[/COLOR][/URL][COLOR=#009900]([/COLOR][COLOR=#FF0000]"%l %l"[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#339933]&[/COLOR]x[COLOR=#339933],[/COLOR] [COLOR=#339933]&[/COLOR]y[COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR]   [COLOR=#B1B100]if[/COLOR][COLOR=#009900]([/COLOR]x[COLOR=#339933]>[/COLOR]y[COLOR=#009900])
[/COLOR]    [COLOR=#009900]{[/COLOR]    [URL="*www.opengroup.org/onlinepubs/009695399/functions/printf.html"][COLOR=#000066]printf[/COLOR][/URL][COLOR=#009900]([/COLOR][COLOR=#FF0000]"%l"[/COLOR][COLOR=#339933],[/COLOR]y[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]     [COLOR=#009900]}[/COLOR]  
  [COLOR=#B1B100]else[/COLOR]     [COLOR=#009900]{[/COLOR]     
[COLOR=#B1B100]       if[/COLOR][COLOR=#009900]([/COLOR]x [COLOR=#339933]%[/COLOR] [COLOR=#0000DD]5[/COLOR] [COLOR=#339933]==[/COLOR][COLOR=#0000DD]0[/COLOR][COLOR=#009900])[/COLOR]  
      [COLOR=#009900]{[/COLOR]         y[COLOR=#339933]=[/COLOR] y[COLOR=#339933]-[/COLOR]x [COLOR=#339933];[/COLOR]    
               y[COLOR=#339933]=[/COLOR]y[COLOR=#339933]-[/COLOR][COLOR=#800080]0.5[/COLOR][COLOR=#339933];
[/COLOR]               [URL="*www.opengroup.org/onlinepubs/009695399/functions/printf.html"][COLOR=#000066]printf[/COLOR][/URL][COLOR=#009900]([/COLOR][COLOR=#FF0000]"%l"[/COLOR][COLOR=#339933],[/COLOR] y[COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR]     [COLOR=#009900]}[/COLOR]     
      [COLOR=#B1B100]else[/COLOR] 
       [COLOR=#009900]{[/COLOR]            [URL="*www.opengroup.org/onlinepubs/009695399/functions/printf.html"][COLOR=#000066]printf[/COLOR][/URL][COLOR=#009900]([/COLOR][COLOR=#FF0000]"%l"[/COLOR][COLOR=#339933],[/COLOR] y[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]                [COLOR=#009900]}[/COLOR]     
[COLOR=#009900]}
[/COLOR]     [COLOR=#009900]}[/COLOR]
It's Giving me runtime error
 
Top Bottom