Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 11th, 2005, 8:00 PM   #1
Havoc_101
Newbie
 
Join Date: Apr 2005
Posts: 19
Rep Power: 0 Havoc_101 is on a distinguished road
Rate this plz...

Hey Im really new to the whole C thingy and I was wondering if there was anything I should change in my first newbie program :o .

Oh, um when i do the float c = 6.00; and try to printf it, when it outputs it, it shows "6.0000000" whats up wit dat ???
#include<stdio.h>
#include<stdlib.h>

int x;
main() {
       int n[5], i=6, f=75;
	   char name[20], adress[20];
       int *prt;
	   double d = 6.2;
	   char ch = 'K';
	   int x = (int) d; // the double variable d is casted into an integer
	   float c = 6.00;
       prt = &n[0];
       *prt = 3;
       printf("What is your favorite number?");
       scanf("%d", &x);
       if(x>5)
                {
                printf("The number you chose is greater than 5!\n");
                }
	   getchar();
       printf("\nWhats your Adress?");
       fgets(adress,20,stdin);
       printf("%s\n", adress);
       printf("%d\n", *prt);
	   printf("Testing output Function\n");
	   printf("\"Quotes\"\n");
	   printf("So did this work\?\n");
	   printf("Here is the Output %d, %f\n", i, c);
	   printf("%d\n", ch);
	   printf("%c\n", f);
	   printf("%d\n", x);
       system("pause");
       return 0;
       }
Havoc_101 is offline   Reply With Quote
Old Apr 11th, 2005, 8:45 PM   #2
Mad_guy
Hobbyist Programmer
 
Mad_guy's Avatar
 
Join Date: Oct 2004
Location: Sandstorm, Techno Club
Posts: 239
Rep Power: 4 Mad_guy is on a distinguished road
Send a message via AIM to Mad_guy Send a message via MSN to Mad_guy
It's definately a good start, and it's much less of a workout than looking at beginning C++ programs that are virtually identical, seeing how cout; is truly an eyesore, and iostream is the spawn of Satan.


And for the floats part, it prints out that many decimal places because you use floats for exact values normally, the more exact, the better. So it keeps those reserved.

Last edited by Mad_guy; Apr 11th, 2005 at 8:47 PM.
Mad_guy is offline   Reply With Quote
Old Apr 11th, 2005, 8:55 PM   #3
Havoc_101
Newbie
 
Join Date: Apr 2005
Posts: 19
Rep Power: 0 Havoc_101 is on a distinguished road
So if 6.55 would be an example of a exact value ??
Havoc_101 is offline   Reply With Quote
Old Apr 11th, 2005, 10:22 PM   #4
Mad_guy
Hobbyist Programmer
 
Mad_guy's Avatar
 
Join Date: Oct 2004
Location: Sandstorm, Techno Club
Posts: 239
Rep Power: 4 Mad_guy is on a distinguished road
Send a message via AIM to Mad_guy Send a message via MSN to Mad_guy
You can't get rid of those zero's, unless of course you typecast it to a basic integer, but then you lose your decimal value.
Mad_guy is offline   Reply With Quote
Old Apr 11th, 2005, 10:25 PM   #5
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 555
Rep Power: 5 Benoit is on a distinguished road
Just do

printf("%.2f");

The 2 being how many decimal places you want
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4
Benoit is offline   Reply With Quote
Old Apr 11th, 2005, 10:30 PM   #6
Mad_guy
Hobbyist Programmer
 
Mad_guy's Avatar
 
Join Date: Oct 2004
Location: Sandstorm, Techno Club
Posts: 239
Rep Power: 4 Mad_guy is on a distinguished road
Send a message via AIM to Mad_guy Send a message via MSN to Mad_guy
I always forget about the power of format strings.

ALWAYS.
Mad_guy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:51 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC