Why the following code gives "hello5" as output in C? [duplicate] - c

This question already has answers here:
what is the return type of printf [closed]
(2 answers)
Closed 7 years ago.
Follwing code in C gives the output "hello5" ...how?
#include<stdio.h>
int main(){
int f = fun();
printf("%d",f);
return 0;
}
void fun(){
printf("hello");
}

What you see is undefined behavior.
The value f is never initialized and you are printing uninitialized variable which will lead to undefined behavior.
printf() returns number of characters successfully printed out so the count here is 5(hello) for printing hello.
You need to return this value if you want then you have defined behavior because you are initializing the variable f in main()
int func()
{
int j;
j = printf("hello");
return j;
}

Related

Why is segmentation fault given? C [duplicate]

This question already has answers here:
How to access a local variable from a different function using pointers?
(10 answers)
Undefined, unspecified and implementation-defined behavior
(9 answers)
Closed 1 year ago.
I've tried writing this code and the two printf statements appear to give segmentation fault.
why isn't the output 5? what went wrong? and how to edit this code in a way such that it prints 5?
#include <stdio.h>
int* fun(int x){
x=5;
return &x;
}
int main() {
int x = 3;
int* p = fun(x);
printf("%d",*(fun(x)));
printf("%d",*p);
return 0;
}

why the function returns value to the previous function when it is not returned in the code? [duplicate]

This question already has answers here:
Function returns value without return statement
(6 answers)
Closed 4 years ago.
why the following code is returning the output even if I am not returning the output of variable a to the previous function.
int fact(int n)
{
int a;
if (n <= 1)
return 1;
else
a = n*fact(n-1);
}
int main()
{
int c=fact(5);
printf("%d",c);
}
Quoting C11, chapter §6.9.1
If the } that terminates a function is reached, and the value of the function call is used by
the caller, the behavior is undefined.
So, your program exhibits undefined behavior.

How can I specify the correct 'return type'? [duplicate]

This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 4 years ago.
I have a C program which produces the error message:
p3-static-store-class.c:5:1 warning: return type defaults to 'int' [-
Wimplicit-int] main()
with warning highlighted and a arrow highlighting the m in main(). The code is:
#include <stdio.h>
/* function declaration */
void func(void);
static int count = 5; /* global variable */
main()
{
while(count--)
{
func();
}
return 0;
}
/* function definition */
void func(void)
{
static int i = 5; /* local static variable */
i++;
printf("i is %d and count is %d\n", i, count);
}
My attempt at specifying the return type produced a list of error messages, and C syntax is nothing like what I'm used to - so what is the remedy for this?
You need to specify the return type for main()
int main()
It's also a good idea to explicitly state that main() takes no arguments
int main(void)

Declaration of character pointer as global and local variable when using pipe() varies [duplicate]

This question already has answers here:
Printf printing garbage after read() call. The offset is always printed as 0
(3 answers)
Why is my simple C program displaying garbage to stdout?
(5 answers)
Closed 4 years ago.
#include<stdio.h>
#include<unistd.h>
char *msg1="HELLLO",*msg2="NONONO";//global declaration prints without any garbage value
int main()
{
/*char *msg1="HELLLO",*msg2="NONONO";"global declaration prints with garbage value */
char buf[6];
int file[2],i;
if(pipe(file) < 0)
printf("\nyou are out");
write(file[1],msg1,6);
write(file[1],msg2,6);
for(i=1;i<=2;i++){
read(file[0],buf,6);
printf("\n%s",buf);}
return 0;
}
Output:
As Global Variable:
HELLLO
NONONO
As local variable:
HELLLO▒▒▒
NONONO▒▒▒

Simple cube and square sum function won't run (C) [duplicate]

This question already has answers here:
What will happen if '&' is not put in a 'scanf' statement?
(6 answers)
Closed 4 years ago.
Learning C, trying to code a program that outputs the sum of the cube and square of an inputted number.
#include <stdio.h>
main()
{
int a;
scanf("%d",a);
printf("%d",cube(a)+sqr(a));
}
cube(int x)
{
return(x*x*x);
}
sqr(int arg)
{
return(arg*arg);
}
When I run the program it outputs some seemingly random string of numbers after I input a number. Any way to fix it without changing the usage of returns to assign variables?
scanf needs a pointer:
scanf("%d",&a);
instead of
scanf("%d",a);
int a;
scanf("%d",a);
^
must be &a. d conversion specifier for scanf expects a pointer to int argument.
You need to define a return type to your functions !
#include <stdio.h>
int main()
{
int a;
scanf("%d",a);
printf("%d",cube(a)+sqr(a));
return 0;
}
int cube(int x)
{
return(x*x*x);
}
int sqr(int arg)
{
return(arg*arg);
}
You have not given address of "a" in scanf function.
Please use:
scanf("%d", &a);

Resources