How Output of this program comes out to be this [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Its C program which is running x86_64 machine ,Wanted to know how output is coming like this way
main()
{
int *mess;
mess=malloc(1);
mess[0]=1;
//mess[1]=2;
printf("%d",mess);
}
Now output here is 6295568
How is it??

You're printing the address where your int is stored. You need
printf("%d",*mess);
to print its value.
You are also allocating too little space for your int, you should do:
int *mess = malloc(sizeof(int));
instead of
int *mess = malloc(1);

Related

C string based programming code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
Can anyone please provide me the code in c programming language of problem in which we enter "ABCD" as an input in run time and we get "ZYXW" as output and I tried to solve this using ASCII Code
Hope this will help you . Read about ascii code of character . Also you have to gain knowledge of pointer and string .
#include<stdio.h>
int main(){
char ch[20];
scanf("%s",ch);
for(const char *s = ch; *s; ++s)
putchar('Z'-(*s-'A'));
return 0;
}

sprintf giving out the wrong numbers Arduino [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
trying to add the float to the link below.
float val1 = 51.48
sprintf(buffer, "/xxx/xxx.php?load=%d", val1);
when I print val1 it show as 51.48 but when i use sprintf and then print the buffer it show -6501 i dont understand what is going on.
use %f instead of %d in sprintf. %d is for printing integer so its truncating the digits after decimal point.
float val1 = 51.48
sprintf(buffer, "/xxx/xxx.php?load=%f", val1);

Not understanding a C program [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have following C program and I am not understanding some point of this program
#include <stdio.h>
int main()
{
char ara[100];
while(NULL != gets(ara))
{
printf("%s\n", ara);
}
return 0;
}
If I input some string like Hello World, this code return me the output same as input. But, what is NULL and gets?? Are they from C library? Why their colour not changed when I compile them?
Please read description of function gets()!
This function reads string from stdin. It returns NULL if found end of line or end of file before any characters.

C prog that generates/output 3 numbers (any combination from 1 - 20) when added is equal to 20 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
C prog that generates/output 3 numbers (any combination from 1 - 20) when added is equal to 20. Hope you could help me with this one. Thanks in advance.
int main(){
int num1,num2,num3,sum;
do{
printf("%i+%i+%i=%i\n",num1,num2,num3,sum);
} while(sum=20); getch();
}
Try this one. You should be able to modify it according to your needs.
#include<stdio.h>
int main()
{
int i,j,k;
for(i=1;i<=20;i++)
for(j=1;j<=20;j++)
for(k=1;k<=20;k++)
if(i+j+k==20)
printf("%d %d %d\n",i,j,k);
return 0;
}

how do I iterate through all the entries in /etc/passwd using c? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I would like to iterate through all the record entries in /etc/passwd using just c api calls.
How can I do this?
Note: I've seen lots of examples of how to do this in other languages, but not found any examples in c.
This small program worked for me:
#include <stdio.h>
#include <pwd.h>
int main(int argc, char **argv) {
struct passwd *pw;
setpwent();
while ( (pw = getpwent()) != NULL )
{
printf("%s\n", pw->pw_name);
}
endpwent();
return 0;
}

Resources