ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
First of all, I'm not sure if this is the right section (Or even forum) to post this question. But since you guys look pretty helpful and good people I decided to try my luck here. I assume it's a simple question so the answer should be pretty straight forward too.
I've recently started learning C using as a reference a book called "The C Programming Language" by Ritchie and Kernighan. This book has some exercises at the end of each chapter and to help myself and practice, I coded a simple program that gives me the integer value of each character on a table. Here is the code:
Code:
#include <stdio.h>
main(){
int c, d;
#define START 32
#define END 128
#define COLNUM 5
c = START;
d = 1;
while ( (c) <= END){
while ( d <= COLNUM && c <= END){
printf("%d\t", putchar(c));
++d;
++c;
}
printf("\n");
d = 1;
}
}
Now here is the question: When I run the code on the command line the character appears before the number why is that? Why does it appear before and not after? I understand this might sound like a dumb question but I would really like to understand
Now here is the question: When I run the code on the command line the character appears before the number why is that?
In the call to printf() you use as second argument the return value from another function-call, to putchar(). That is about the whole answer... Because: To have the value returned by putchar(), putchar() must have been executed first. So what happens is this:
1. putchar() does what it is best at and prints out the given value as unsigned char. In the end, it returns the int-value of the same character.
2. printf() prints this return-value formatted as you asked for.
Quote:
I understand this might sound like a dumb question but I would really like to understand
Not a dumb question at all. But the man-pages could help you understand both functions better. These phenomena appear to be typical for the first few attempts with printf() or other functions from that category, maybe all those which use format-strings. I would not call this a “side-effect”, but maybe others do.
Last edited by Michael Uplawski; 08-03-2016 at 04:16 PM.
Thank you guys for your time answering my question. A special thanks to Michael Uplawski because the explanation really made me understand what goes behind the function printf()
Now it all makes sense in my head : D
The %d is asking for a value to the argument, which in this case is a function, so it will run the function first before giving it a value... And since the function putchar() also prints a char to the screen it will appear before the integer,
#include <stdio.h>
int main(){
int c, d;
#define START 32
#define END 128
#define COLNUM 5
c = START;
d = 1;
while ( (c) <= END ) {
while ( d <= COLNUM && c <= END ) {
// printf("%d\t", putchar(c));
printf("%d\t", c);
putchar(c);
printf("\t");
d++;
c++;
}
printf("\n");
d = 1;
}
return 0;
}
Probably more along the lines of what you wanted. Although probably not the most efficient way.
#include <stdio.h>
int main(int argc, char** argv) {
unsigned col = 0;
for(int i = 32; i < 128; ++i) {
if (++col < 5) {
printf("%3i - %c\t", i, i);
}
else {
col = 0;
printf("\n");
}
}
return 0;
}
Improvements:
* Constants are superfluous.
* Format-string imposes a right-aligned field of fixed width for the integer.
* Incrementation is defined in the head of the loop.
(* discipline is bad, automatisms are rarely bad, int main(int argc, argv** char)is always right)
Last edited by Michael Uplawski; 08-04-2016 at 10:10 AM.
Reason: You shall try the code that you wish to publish, before you do.
Thank you guys for your time answering my question. A special thanks to Michael Uplawski because the explanation really made me understand what goes behind the function printf()
Now it all makes sense in my head
The %d is asking for a value to the argument, which in this case is a function, so it will run the function first before giving it a value... And since the function putchar() also prints a char to the screen it will appear before the integer,
Man, this is fun
It sure is!
Now, just to be clear, the printf() function takes a variable number of arguments, the first of which is a "format string" that, among other things, tells printf() how many arguments to expect and what sort of data they must be. (The function is utterly trusting on this point, and the program will crash if the format-string and the arguments don't agree.
Before any function can be executed, all of its arguments must be evaluated. If any of these are functions, they are called, and their result is used. If those functions happen to do something ("side effects"), as putchar() does, then those side-effects will take place as a result of the function being called. And they will necessarily occur before the function for which they are a parameter is called, because they're being called in order to obtain their result.
The programming language simply assumes that you know what you are intending to do.
Last edited by sundialsvcs; 08-04-2016 at 09:00 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.