![]() |
%d confusion
After attempting QBasic, and being deterred whilst getting help here ( not a bad thing, just stating the facts ) I decided to make an attempt at C. Following my same routine, I sought out an online tutorial.
For the record, I got back to college in spring, and would like to be prepared for the programming courses I will inevitably be taking. Anyway, found one on howstuffworks dot com. It is rather simple to follow, but there is one point, I do not quite understand. I gather the meaning of %d. It's a placeholder for a variable value. such as :
int a;This is the same thing as :
a = 10Well, in one example: :
#include <stdio.h>they use %4d. Im not quite sure I understand this. They just explain that you use %d to print a variable into a line of text. So, does %4d express a 4 number INT, or is used because of some other reason they just neglected to leave out of the tutorial? I also see that the printf line is slightly different than in previous uses. a, (a - 32) * 5 / 9); is just a continuation of the printf line if I am not mistaken. So, I suppose %4d is used rather than %d because there are commands at the end of the line? |
First of all, a simple visit to the printf documentation would answer your questions. "%d" is one form of one format specifier. The "%" is the clue. "d" indicates an integer, as "s" would indicate a string. There are several others. The things between the "%" and the "d" have various meanings, such as width, precision, and so forth. The first argument is the format string. The following arguments specify the values to be formatted, one for each specifier. These values can be expressions. Again, just man printf, or visit your help file, or Google. It will repay you handsomely.
|
I am using Bloodshed Dev-C++. Yes, it does have documentation on printf, but nothing that my apparantly not-so-keen eyes could find about %xd.
|
Quote:
YMMV, of course. I was rather young when I started, so you may be able to grasp the fundamentals with more ease than I did. You may also want to look at Ruby; whilst a little more quirky than Python, there are some very unconventional and amusing tutorials around for it. It's also well suited for problems that require an Object Orientated solution, such as an RPG. |
Im not really having as much trouble as I thought with C. It's just a matter of recognising what does what in C and comparing it to what does what in BASIC. If I can point out the differences, I can mentally do the work with minimal confusion.
I have had a look at RUBY. I suppose a big part of the problem is, I dont see the point in learning something that is going to be unbeneficial to me. As with BASIC being a rarity, and the way things are done with basic not being used, I have a hard time putting my full attention forth. |
If your compiler's documentation is pitiful, and you're not on a *nix system with man pages, very extensive documentation can be found via Google. cplusplus.com, for instance, says:
:
printf |
Quote:
Incidentally, your approach at learning C - comparing it to a language you already know - is a common and effective way of learning new programming languages. Indeed, the more languages you learn, the more points of reference you have to learn more languages. Quote:
The larger the viewpoint shift, the harder the programming language is to learn, but the more beneficial the results. Haskell, for instance, is the language I'm currently learning, and it has concepts in it that are almost completely alien to the programming I have done before. It took me a long time to understand monads enough to be confident with their use; they're really unlike anything I had encountered before. On the other hand, learning C# took hardly any time at all, because it's so similar to C++ and Java, two languages which I already knew very well. As for learning Ruby, you may want to take a look at Why's (Poignant) Guide to Ruby. It's really unlike any other programming tutorial I read, and rather more amusing. Chapter 6, for instance, revolves around Rabbit objects fighting monsters by use of overloaded mathematical operators and metaprogramming. |
Don't worry. printf is not very intuitive.
Someone correct me as I'm probably wrong, but... %a.bx a is the number of characters to the left of the decimal b is the number to the right and x is a character representing the data type: s - string d/i - integer ul - unsigned long etc. a and b mean different things when you are talking about strings. I'm pretty unclear on this, but I think they are the maximum and minimum number of characters or something like that. printf("jk %s als%.5fhl;khas %df", a, b, c); The %s corresponds to 'a'. 'a' will be read as a string and pasted into the original string, replacing %s The %f corresponds to 'b'. 'b' will be read as a floating point variable and pasted into the original string... It will also be limited to 5 places after the decimal point. The %d will be turned into the decimal that is stored in 'c'. There can be pretty much an unlimited number of % arguments in a printf statement, but you need to have a variable (a,b, or c in this case) for each one. Hopefully, this will help you a little. |
Aa a former fan, let me say that Ruby is fun. However, with time, I realized that Python is a much better language if you're planning to build anything on a large scale. It's much more readible, and the multitude of different ways to do one thing in Ruby tend to get confusing when reading other code.
When I see code in Ruby, it often seems like a contest of who can fit the most clever code in the smallest number of lines. |
Ruby has its moments. It's class system is more expressive and more elegant than Python's, and, let's face it, fitting the most clever code in the smallest number of lines is pretty fun ;)
|
| All times are GMT -5. The time now is 1:36 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC