Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to know if the format of user defined function i wrote i.e return(xxx) is correct or not .
Because when i compile my code, i have to enter the input 2 times.It might be a silly mistake because i just began learning C language
****MY CODE:****``
#include<stdio.h>
long cube(long x );
long input,answer;
int main (void )
{
printf("Enter a number:");
scanf("%ld ",&input);
answer = cube(input);
printf(" The cube of %ld is %ld",input ,answer);
return 0;
}
long cube(long x )
{
return (x*x*x);
}
****ANswer****
#include <stdio.h>
long cube(long x);
long input, answer;
int main( void )
{
printf("Enter an integer value: ");
scanf("%d", &input);
answer = cube(input);
printf("\nThe cube of %ld is %ld.\n", input, answer);
return 0;
}
long cube(long x)
{
long x_cubed;
x_cubed = x * x * x;
return x_cubed;
}
remove the space after '%ld' it will take one input.
according your code,
scanf("%ld '&input) ;
here compiler at first wants one input for '%ld' then it waits for blank space you used after '%ld'. remove it then it will go next step after one input.
you should use,
scanf("%ld%",&input);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
#include<stdio.h>
int main(){
int n,m;
int count = 0;
scanf("%d" "%d", &n, &m);
for(n=m;n>0;n=n/10);
printf("%d\n",count++);
return 0;
}
How can I do this with this main code or I just need something edit? I struggled how to can it be count like this sample:
Sample Input 1 (standard input)
1 5
Sample Output 1 (standard output)
1
2
3
4
5
It seems you want a simple counter that counts from one number to some other large number.
#include <stdio.h>
int main(){
int lowerbound, upperbound;
scanf("%d %d", &lowerbound, &upperbound); // string simplyfied
int counter;
for (counter = lowerbound; counter <= upperbound; counter++)
{
printf("%d\n",counter);
}
return 0;
}
That's the classic introductory sample of the for loop that you can find in about every beginner's C text book.
Note the new selfdescribing variable names which makes your code easier to read and to understand.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
#include<stdio.h>
double sum_1(int n)
{
int i=1;
double s;
while(n>0)
{
s=s+i/(2*i+2);
i=i+2;
n--;
}
return s;
}
int main()
{
int n=5;
double s1;
printf("Enter n:\n");
scanf("%d",&n);
s1= sum_1(n);
printf("sum = %lf",s1);
return 0;
}
The problem is
s=s+i/(2*i+2);
in the first iteration, s is used uninitialized. Since this is a type which can have trap representation, and it's address is never taken, trying to use the uninitialized value here invokes undefined behavior.
That said, the grouping of the statement
s=s+i/(2*i+2);
is same as
s = s + ( i / (2*i+2) );
^^^^^^^^^^^^^---- integer division
so, it involves integer division, which is most likely what you don't want. You need to enforce floating point arithmetic, like
s=s+i/(float)(2*i+2);
Finally, for printing a double, %f is sufficient, %lf is not needed and has no effect.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
#include <stdio.h>
struct Bank {
char name[500];
int mobile;
float balance;
};
int main() {
int i;
struct Bank a[100];
for (i = 0; i < 3; i++) {
printf("Enter the name of person %d: ", i + 1);
gets(a[i].name);
printf("Enter the mobile of person %d: ", i + 1);
scanf("%d", &a[i].mobile);
printf("Enter the mobile of person %d: ", i + 1);
scanf("%f", &a[i].balance);
}
for (i = 0; i < 3; i++) {
puts(a[i].name);
printf("His Balance is: %f", a[i].balance);
printf("His Mobile number is : %d \n\n", a[i].mobile);
}
return 0;
}
Try running this the user input requests don't come like I want them to, just run it and one would understand. What am I doing wrong?
The reason it's not working as expected is because there is a newline character left on the input buffer on the second and third iteration to gets(), presumably from the previous "enter" hit.
It works as expected (kind of, see below) if you change the gets() call to scanf("%s", a[i].name).
Please note that:
this code is badly vulnerable to buffer overflow on multiple lines (all scanf() and the gets() call) - see here, or here, or here, etc., bottom line: you should not use gets() at all, nor scanf() without a boundary
you have a typo in the third printf and it again asks for the mobile number
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm doing a project and I'm just curious to know if it is possible to have a line that says "if something is not entered" and a prompt statement would be followed.
For example,
if(id_ == NULL){printf("John Doe is absent.")}.
Just a curious question because I want to explore C programming a bit more.
You can do this with scanf (or similar functions: fscanf, sscanf...).
Assuming id_ is an int:
if(scanf("%d",&id_)!=1){
printf("John Doe is absent.");
}
These functions return the number of input items successfully matched and assigned.
see top voted answer here for more info.
#include <stdio.h>
void input_id(int **id){
int num;
printf("input id:");
if(scanf("%d", &num)==1)
**id = num;
else
*id = NULL;
}
int main(void){
int id;
int *id_ = &id;
input_id(&id_);
if(id_ == NULL){
printf("John Doe is absent.\n");
} else {
printf("id : %d\n", id);
}
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<windows.h>
int main(){
int iCount=1;
int min=999999;
int max=0;
int iSum;
int iAVG=0;
int iValue;
system("color 97");
for(iCount=1;iCount<36;iCount++){
system("cls");
SetConsoleTitle("chairs");
printf("\t\tCHAIR VALUES\n\n");
printf("\nPlease enter the value of chair#: %d.\n>>", iCount);
scanf("%d",iValue);
iSum+=iValue;
if(iValue<min){
min==iValue;
}
if(iValue>max){
max==iValue;
}
printf("\n\nThe minimum and maximum values entered are:\nminimum value>>%d\nmaximum
value>>%d", min, max);
getche();
}
(iAVG=iSum/iCount);
printf("\n\nThe average value of the entered chairs is: %d", iAVG);
getche();
system("cls");
printf("\t\t\nGOODBYE USER!");
}
I wrote this code, a c question. I compiled it within codeblocks, it was successfully compiled and executed. However, when i entered the first chair value, it says that"chairs.exe has stopped working. Im here trying to see what might have lead to this problem. Any can give me a helping hand?
You missed to add '&' in scanf
scanf("%d",iValue);
Should be
scanf("%d",&iValue); //<--- Notice '&'
And as pointed out by "Zev Eisenberg" in comments.
min = iValue //<-- Make sure you are using assignment operator here.