printf quirk


 
Thread Tools Search this Thread
Top Forums Programming printf quirk
# 1  
Old 02-02-2011
printf quirk

Hi,

Could anyone explain me the logic behind the following program's output?

Code:
int main() {
    printf("%d\n", printf("%d %d", 2, 2) & printf("%d %d", 2, 2));
    printf("%d\n", printf("%d %d\n", 2, 2) & printf("%d %d\n", 2, 2));
}

Ans:
2 22 23
2 2
2 2
4
# 2  
Old 02-02-2011
Code:
printf("%d\n", printf("%d %d", 2, 2) & printf("%d %d", 2, 2));
3333333333333,11111111111111111111111,222222222222222222222,3

We start work or printf 3 but the integer argument involves two other functions and we must run those functions first to get the return codes. So printf number 1 runs and it prints "2 2" and it returns 3 (number of characters printed). The printf number 2 runs and does the same thing. So the current line looks like "2 22 2". 3 and 3 is 3. So printf number 3 prints a 3 and a newline producing the first line you show as output.

Same deal for the second line, but this time there are new line characters printed each time so we get a separate line for each printf. The new line character bumps the return codes up to 4.
These 2 Users Gave Thanks to Perderabo For This Post:
# 3  
Old 04-07-2011
nice i had same confuson too
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using printf in bash

printf "%5.5\n" "1234567890" will print 12345 . How do I get it to print 67890 Essentially, I just want the last 5 characters rather than the first 5. (4 Replies)
Discussion started by: lavender
4 Replies

2. UNIX for Dummies Questions & Answers

printf when used for different processes

I am playing with function fork() (creating few processes)and trying to put some output (with process id,child id and so on) to the shell and sometimes i get output for 2 proccesses mixed up on same line. sometimes I would get something like this... myShell$ ./a.out proccess1 ... (2 Replies)
Discussion started by: joker40
2 Replies

3. UNIX for Dummies Questions & Answers

Odd quirk with xargs and telnet

For a variety of strange reasons, I've set up an arrangement by which a server responds with "Hi there" if I connect on a specified port. This is a typical transaction: # telnet example.com 3334 Trying 123.456.789.101... Connected to example.com. Escape character is '^]'. Hi there... (4 Replies)
Discussion started by: treesloth
4 Replies

4. Shell Programming and Scripting

printf problem

In one of the scripts I am using pintf function as following printf "%s%s%s\n" "$f1" "$f2" "$f3" f3 variable contains a string of 10 characters. However it has value first 7 character and last 3 characters are empty. Example Aaaaaaa<3 spaces> bbbbbbb<3 spaces> ccccccc<3 spaces>... (4 Replies)
Discussion started by: varunrbs
4 Replies

5. Shell Programming and Scripting

Printf problem

I am having a major problem with printf, The more I pad it, the less I see :( The problem is in the first function, report Am I ruining output somewhere? I wont print out the names propely, it cuts them off or deletes them completely :( #!/bin/bash report() { printf "%-10s" STUD# ... (2 Replies)
Discussion started by: L0ckz0r
2 Replies

6. UNIX for Dummies Questions & Answers

The meaning of %s in printf

I have this command like that has %s in it, I know %s calls a column, but I am not sure I understand which column (I mean for my case I can check the input file, but I want to know how is this %s used, how comes tha same symbo; gives different columns in one command line: {printf "grep %s... (22 Replies)
Discussion started by: cosmologist
22 Replies

7. UNIX for Dummies Questions & Answers

Need help with printf

Hi, I have just completed my first script (:D) and now i just need to format it with printf. This is what I have: #!/bin/ksh TOTB=0 TOTF=0 TOTI=0 HOST=`hostname` echo " FSYSTEM BLKS FREE INUSE MOUNTEDON" df -m | grep -v ":"|grep -v Free|grep -v "/proc"| while read FSYSTEM... (2 Replies)
Discussion started by: compan023
2 Replies

8. Shell Programming and Scripting

printf

How to print output in following format? A..................ok AA................ok AAA..............ok AAAAAA........ok "ok" one under one (4 Replies)
Discussion started by: mirusnet
4 Replies

9. Programming

printf

What is the output of the following program considering an x86 based parameter passing sequence where stack grows towards lower memory addresses and that arguments are evaluated from right to left: int i=10; int f1() { static int i = 15; printf("f1:%d ", i); return i--; } main() {... (2 Replies)
Discussion started by: arunviswanath
2 Replies

10. Solaris

Daylight Savings Time Quirk

I am running a SUN E450 on solaris (5.7). I have applied the DST patch and the system time is correct. However when users login the get the time wrong (+4 hours) (I am in EDT Zone). Does anyone know where a system wide variable for this could be set. (Root user gets the right time) Frank (3 Replies)
Discussion started by: frankkahle
3 Replies
Login or Register to Ask a Question