working of printf()


 
Thread Tools Search this Thread
Top Forums Programming working of printf()
# 1  
Old 08-23-2010
working of printf()

hello all, i came accross an aptitude question ..
Code:
 
int
main()
{
    int a = 10, b = 20, c = 30;
    printf("%d\t%d\t%d");
}

it gives output
Code:
 
30    20    10

what is the reason of such behaviour of printf().
thank you.
# 2  
Old 08-23-2010
This is undefined behavior, it is probably caused by the fact that a, b, and c values are on the stack.

Undefined behavior means:
1. it may or may not work
2. its behavior will probably change at random
3. you can never depend on it, and it may crash the system.

Basically: never do that kind of thing.
# 3  
Old 08-23-2010
Let me tell you what I got in 4 different runs at my RHEL5 machine :

Code:

 -1081779332    10      20


 -1075902180    10      20


 -1080081124    10      20


-1078653316     10      20

// And the code was -- almost same!!! :)

#include <stdio.h>

int
main()
{
    int a = 10, b = 20, c = 30;
    printf("\n %d\t%d\t%d\n");
}

# 4  
Old 08-23-2010
The stack is the program's "blackboard". It's the place local variables and function call stack frames get stored, so anything that does anything in your program is going to leave junk scribbled all over it. Make a few function calls and printf's output may change... Calling printf without giving it any arguments to print just causes it to read the "top" of it no matter what's on it. Which bits? That's undefined -- but can include local variables and function call frames. Really, your output is not so amazing.

Last edited by Corona688; 08-23-2010 at 12:52 PM..
# 5  
Old 08-24-2010
Thank you all for your reply. Thanks.

Last edited by zius_oram; 08-24-2010 at 02:44 AM..
# 6  
Old 08-28-2010
I think the way the variables are declared could have influence on what you get.
like:
int a, b, c
is different from
int a;
int b;
-----
here you store all variables sequencially on the stack, so there'd be high probability that printf return these recently stored variables
# 7  
Old 08-28-2010
Quote:
Originally Posted by saman_glorious
I think the way the variables are declared could have influence on what you get.
like:
int a, b, c
is different from
int a;
int b;
-----
here you store all variables sequencially on the stack, so there'd be high probability that printf return these recently stored variables
There is no difference in the two ways and has no effect at all on the address location each variables get. They all are created on the stack only.

Just print the address and notice the same. Also for your better understanding you may look the segregation of the process memory layout at /proc/<pid>/mmap and map their addresses printed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Disk Space Utilization in HTML format working in one environment and not working on the other

Hi Team, I have written the shell script which returns the result of the disk space filesystems which has crossed the threshold limit in HTML Format. Below mentioned is the script which worked perfectly on QA system. df -h | awk -v host=`hostname` ' BEGIN { print "<table border="4"... (13 Replies)
Discussion started by: Harihsun
13 Replies

2. Shell Programming and Scripting

Automating pbrun /bin/su not working, whenever manually it is working using putty

I am trying to automate a script where I need to use pbrun /bin/su but for some reason it is not passing thru the pbrun as my code below. . ~/.bash_profile pbrun /bin/su - content c h 1 hpsvn up file path I am executing this from an external .sh file that is pointing to this scripts file... (14 Replies)
Discussion started by: jorgejac
14 Replies

3. Shell Programming and Scripting

Printf %T not working

Dear All, this script was working well enough few days back. Now it started acting up. Could anyone please throw some lights about what may be the reason for it's not working. ts=$( printf "%(%s)T" "now" ) under debug mode this is what I get printf "%(%s)T" "now" ++ printf '%(%s)T'... (4 Replies)
Discussion started by: manas_ranjan
4 Replies

4. Shell Programming and Scripting

Crontab is not working in printf

hi, i have a script as: printf '%s -> %s\n' "$(date '+%Y-%m-%d %H:%M')" "$(/opt/gcsw/status -ne | fgrep 'State:2' | wc)" which gives output as: 2013-01-18 13:00 -> 80 480 6529 and it is working fine. now I want to put this into cronjob and write the output to a file in every 5... (7 Replies)
Discussion started by: gc_sw
7 Replies

5. Shell Programming and Scripting

"find . -printf" without prepended "." path? Getting path to current working directory?

If I enter (simplified): find . -printf "%p\n" then all files in the output are prepended by a "." like ./local/share/test23.log How can achieve that a.) the leading "./" is omitted and/or b.) the full path to the current directory is inserted (enclosed by brackets and a blank)... (1 Reply)
Discussion started by: pstein
1 Replies

6. UNIX for Dummies Questions & Answers

Qsub and printf

Hello, I have some issue with qsub and the standard output : I launch a script that "echoes" the string "abc" and then executes a C program in which I print informations with the printf function ; the .o file contains "abc" but not the information displayed by printf. I also tried... (0 Replies)
Discussion started by: Shaderw
0 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 problem

I have the following code: $ awk '{ printf "%-10s %s\n", $1, $2, $3, $4, $5, $5, $6 }' file i can only print the first 2 elements ($1,$2). How can i print all the elements to appear like this: aardvark 5555553 jhfjhfjkg efiigig ejkfjkej wjkdjk alpo-net 5553412 ... (2 Replies)
Discussion started by: DDoS
2 Replies

9. 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

10. 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
Login or Register to Ask a Question