473,396 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how printf() statement executes internally

thanks for your time

Any body please explain the actual mechanism performed while executing the printf()( with the help of stack if possible )

eg: printf("%d%d",a++,--a,a--);
Jul 22 '08 #1
3 4090
Banfa
9,065 Expert Mod 8TB
The actual mechanism is implementation and therefore platform dependent. Many embedded platforms have at least 2 available implementations one with and one without floating point support as this adds rather a lot of code to the binary image.

If you would like to see an implementation of printf then try Googling printf source code and looking at one.

In general printf uses a pointer to scan down the format string copying characters to the output stream. When it encounters a format code (%...) then it executes additional code to extract a value from the stack format it in the correct manor according to the format code and send it to the output stream.

It is not possible to say what your code example prints because it invokes undefined behaviour because if modifies the value of a more than once between sequence points. As soon as undefined behaviour is invoked all bets are off, anything could happen.
Jul 22 '08 #2
newb16
687 512MB
thanks for your time

Any body please explain the actual mechanism performed while executing the printf()( with the help of stack if possible )

eg: printf("%d%d",a++,--a,a--);
(let's suppose there are 3 %d's, not 2 ) first of all, it's bad code and result of it is undefined - it depends on how compiler decide to evaluate argument for function - left to rigth or right to left.
googling pattern - sequence point

As for this code ( besides 'what a is equal to after all this' ) it looks like the following( assuming all arguments are passed on stack that is not always true )
it places, say, address of string, then a, a-1, a in 4 consecutive say, 4-bytes locations on stack and calls printf. Then printf, knowing address of first argument (char*) iterates over 3 other using va_arg knowing arguments' size given in format string ( %d -say, 4 bytes ) I.e. address of second argument is (address of first) - 4.
Jul 22 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
The law is that a variable cannot be modified more than once in the same sequence point. That is, more than once in the same expression. When you do, the results are indeterminate (that is, rthey vary by compiler).

Don't ever do this and you will go craze trying to make a rule for indeterminate behavior.
Jul 22 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Minti | last post by:
Is std::cout slower than printf When we call printf e.g. in printf(20 format conversion specifications, 20 arguments); Is it faster than the std::cout << { 20 redirections to the output...
5
by: Peter Ammon | last post by:
It's my understanding that the printf() function is declared as int printf(const char * restrict format, ...); in stdio.h. And loosely speaking, if a parameter is declared as restricted, then...
10
by: Sorawit Sharp | last post by:
My app executes C# user scripts via dynamic code execution. Is it possible to execute the scripts single step wise like a debugger ?
10
by: lovecreatesbeauty | last post by:
Is parameter type conversion required for the 2nd argument on printf("%p", (void *)&i); ? But one would never call memcpy like: memcpy((void *)pi, (void *)pj, sizeof *pj); /*memcpy((void *)pi,...
6
by: ray.webster | last post by:
Should the following work *if* I have a c99 compiler please? #include <stdio.h> int main(void) { size_t t = 42; # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
36
by: Debaser | last post by:
I've recently read in one of my old C books that puts() is a better function call with regard to performance than printf() in the following situation: puts("Some random text"); vs. ...
34
by: Old Wolf | last post by:
Is there any possible situation for printf where %hd causes a different result to %d, and the corresponding argument was of type 'short int' ?
0
ADezii
by: ADezii | last post by:
One frequently asked question at TheScripts is "Should I use a Stored Query or an SQL Statement in those situations that require a Query (RecordSets, RecordSources, Append, Delete, Update Operations,...
8
by: jamil | last post by:
While browsing MSDN, I found an article related to performance in VB.NET programs. The article is dated 2002, and I am not sure if the information below is still correct: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.