473,320 Members | 2,112 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,320 software developers and data experts.

output of printf("%d %s"); ??

Can u explain the output of the following program

void main()
{
printf("%d %s");
}

Regards,
Arvind.
Mar 26 '07 #1
5 2169
Banfa
9,065 Expert Mod 8TB
No, it invokes undefined behaviour because main is not declared properly.
Mar 26 '07 #2
I think main is defined properly but anyways I wan't to know the outcome of the print statement (as this is a valid statement).
Mar 26 '07 #3
Banfa
9,065 Expert Mod 8TB
I think main is defined properly but anyways I wan't to know the outcome of the print statement (as this is a valid statement).
Trust me your main is not defined properly, main has to return int; read this.

As long as your program invokes undefined behaviour the output could be anything.

Assuming you corrected the definition of main then your program could still output anything because it is still invoking undefined behaviour.

In your printf statement you have told the printf function that 2 parameters follow the format string on the stack, and integer and a pointer to a string. However you have not provided that data. The printf function will try to access this data but since you have not provided it there is not even any guarantee that the next addresses on the stack are actually allocated. Attempting to access unallocated memory cause undefined behaviour.

Even if those addresses on the stack are available then the integer will have some random value, the pointer will also have some random value. printf will try to access this pointer as though it points to valid memory. Since it is random it is unlikely that it points to valid memory so printf will try to access an invalid memory address invoking undefined behaviour.

it is not possible to predict the behaviour of a program exhibiting undefined behaviour so no I can not explain to you what the output will be. I suggest you correct your code and ask again.
Mar 26 '07 #4
%d is a place holder, you need to have a variable to send to the place holder.
like printf("%d",x);.
Mar 26 '07 #5
Can u explain the output of the following program

void main()
{
printf("%d %s");
}

Regards,
Arvind.
Hey your program can be correct in some of the compilers but main() should not be void...
any way the output is also undefined...
it some times print the stack contents coz printf() uses the stack to print its value and the output may be the garbage values or the contents of the stack...
Mar 26 '07 #6

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

Similar topics

12
by: sugaray | last post by:
does the expression int a=printf("%d\n",a); implementation dependent ? I supposed it would produced the result 2, but i got 4206596 (result may vary on your machine), i wonder if the value produced...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
12
by: baumann | last post by:
hi all, printf("%c",b) doesn't work properly. #include <stdio.h> int a , b; char d, e; char * p; float f; int main(int argc, char* argv) {
19
by: v4vijayakumar | last post by:
why the following statement dumps the core(Segmentation fault)? printf("%s\n", __FILE__);
12
by: Zero | last post by:
Hi everybody, i want to write a small program, which shows me the biggest and smallest number in dependance of the data type. For int the command could be: ...
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,...
7
by: Rajesh S R | last post by:
printf("%hhd",89);/*Assume char has 8 bits and is signed*/ Is it valid? I know that it has been discussed in comp.std.c. ...
29
by: candy_init | last post by:
Hi all, I just came across the following program: #include <stdio.h> int main() { float a = 12.5; printf("%d\n", a); printf("%d\n", *(int *)&a); return 0;
27
by: sophia | last post by:
Dear all, why in the following program #include<stdio.h> #include<stdlib.h> int main(void) {
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.