What is the correct output of this statement? [duplicate] - c

This question already has answers here:
Turbo C++: Why does printf print expected values, when no variables are passed to it?
(5 answers)
Closed 9 years ago.
int a=9,b=6,c=3;
printf("%d%d%d");
I executed this in code blocks 10.05. I got some garbage values. But in a website the output was given as 3 6 9. What is the correct one?

You will get garbage values, because you're not providing any arguments to the printf() call.
The correct code would be
printf("%d%d%d",c,b,a);
(to get the numbers in the order quoted)

The correct one is neither of the two you described. Since no values were passed to printf, only the formatters, whatever was on the stack at that moment (which is undefined) is passed.

What is the correct output of this statement?
This code invokes undefined behaviour and so there is no correct output. The output is undefined.
The code invokes undefined behaviour because the format string you passed to printf requires you to pass more parameters (3) than you supplied (0).

Related

what is the use of 'return 0' in C [duplicate]

This question already has answers here:
What is the purpose of the returning of a value from main() in C/C++? [duplicate]
(5 answers)
Closed 7 years ago.
Why do we have to use 'return 0' statement in C?
in the end of main function. I know This means all things are going on way. But what does it mean? What exactly are there at the background of this ?
It returns a number to the environment that started it... Every process begins with a set of arguments and returns an int indicating success or failure
see So what does "return 0" actually mean?
Potentially, a calling process can read the result and use it as information on what the execution of the program did.

segmentation fault in printf [duplicate]

This question already has answers here:
What is the behavior of printing NULL with printf's %s specifier?
(4 answers)
Closed 7 years ago.
Recently I came across this interview question to determine the output of the following printf statements:
printf("test %s\n",NULL);
printf("test %s\n",NULL);
printf("%s\n",NULL);
printf("%s\n",NULL);
test (null)
test (null)
Segmentation fault (core dumped)
I am not able to figure out why does it have a segmentation fault in the last 2 printf's and not for the first 2 cases.
The behavior is undefined.
Standard says
C11- 7.21.6/9
[...] If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.
%s in printf expects an argument that should be a pointer to the initial element of an array of character type.
What is the behavior of printing NULL with printf's %s specifier?
Have a look at the above link,i hope it helps!
This is an interview question? The only valid answer is "it will paint your cat green, or maybe do something completely different like travelling to the end of the universe". If they don't accept this as an answer, you don't want to work there.
To be a little more serious here, the output you see is probably created with the GNU C library?
Passing 0 for a %s conversion is undefined behavior and the most probable result of it is a crashing program. glibc has some safety measures built in that replace the string (null) automatically for 0 pointers. You could argue whether this is a great idea, but it's legal because the behavior is undefined -- an implementation can do whatever it wants. Including a crash like you experience it later.

Difference between scanf and scanf_s in C [duplicate]

This question already has answers here:
Difference between scanf and scanf_s
(3 answers)
Closed 8 years ago.
Although my program gives the required output. There are many warnings showing scanf() shouldn't be used and try using scanf_s() instead.
Possible cause of this warning??
This is happening to all the programs which are using the scanf function. Even simple addition of numbers.
With scanf, with some format arguments it is possible to crash your program if you use unlimited size inputs. scanf_s requires to provide size of output buffers, thus limiting possibility of buffer overflows (provided you specify output buffer sizes correctly).

fflush(stdin) before gets() in c [duplicate]

This question already has answers here:
Using fflush(stdin)
(7 answers)
Closed 8 years ago.
Okay , i was solving a problem in code chef (very easy).
It briefly states that :
-A question as a string will be given , and another string has to be produced which does not have any letter used in question string. Uppercase and lowercase are assumed to be same.
-If all alphabets have been used just print ~ sign.
My whole logic is correct except i caught error in my input and this was caused by using fflush(stdin) before gets(). Please explain why was this an error ? Ignore the rest of the code.
Link to problem :http://www.codechef.com/problems/NOLOGIC/
Link to wrong solution :http://www.codechef.com/viewsolution/3881817
Link to corrected solution :http://www.codechef.com/viewsolution/3881827
Pls Note :In corrected code i have only made change in not using fflush(stdin) and using getchar()
Being that the behavior of fflush(stdin) is implementation specific; and being that the implementation (for this question) is unknown, the behavior will also be unknown.
You may safely expect the behavior of fflush(stdin) to be unpredictable.

C Programming Increment and Decrement Operator Problem [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
$void main()
{
int a=10,c;
c= ++a + ++a;
printf("%d",c);
}
this program Actualy Print Value Of c=24 but By Calculation we can say it should be
c=23 ,how it possible?
Your program has a bug -- you modify the same variable twice without an intervening sequence point. Fix the bug and the mystery will go away.
A very deep understanding of not just how the language works but how compilers work is required to understand why buggy code happens to do what it happens to do. I would just suggest not writing buggy code and, when you find a bug, simply fix it instead of trying to understand precisely why and how it broke.
My advice to you is to stop. You learned the right lesson -- code that triggers undefined behavior is unpredictable and frequently doesn't do what you might expect it to do. That's all you need to know about UB until you're an expert at using the language correctly.
'++' > '+'
Here post increment operation is done before.Since you gave it two times if does post increment two times so the value of 'a' becomes 12 and adds it up (12+12).So the final value is 24.

Resources