C language i get an error in my code - c

hello I am searching where I did the wrong step?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int account_on_the_bank=25;
printf("how much money do you have in the banque? \n");
scanf("%f",account_on_the_bank);
printf("Vous avez %d euros sur votre compte",account_on_the_bank);
return 0;
}
where's my problem???? it shows windows has stop working
I have tried everything the same problem shows up always?

This is the problematic line:
scanf("%f",account_on_the_bank);
It has wrong specifier and also not passing the address of the variable to scanf.
It should be:
scanf("%d", &account_on_the_bank);

Please read the basics of C
You have error in your scanf()
scanf("%f",account_on_the_bank);
^ ^
It should be
scanf("%d",&account_on_the_bank);
^ ^
because int needs %d as specifier, and scanf() needs address of the variable in which you want to store the read value(address is obtained by using &)

Related

I'm unreasonably getting zero as an output and I can't figure out why

I'm a begginer programmer and I've been experimenting with this code but I can't figure out why it's giving me zero. I've checked for integer divison , and wrong place holders but I can't find any . I also believe I'm using the correct format for every function such as printf , scanf etc. can anyone hepl ?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
double theta , fr , a, pi = 3.1459;
printf("Enter the value of a \n");
scanf("%f",&a);
theta = atan2(a,27.5);
printf("%f",theta);
fr = 1.67*pow(10,-6)*sin(theta);
printf(" frequency = %f",fr);
return 0;
}
You need to use the correct format specifiers for every data type. Your variables are doubles so you need to use %lf for scanning.
You need to learn how to debug programs yourself. If you add one printf mode the problem becomes obvious:
printf("Enter the value of a \n");
scanf("%f",&a);
printf("%f\n", a);
https://godbolt.org/z/cj3zo6Yqn
It is prints zero 0. So scanf did not scan properly.
Changing to
scanf("%lf",&a);
Resolves the problem.
https://godbolt.org/z/jcjrYaG5j

printf Function not giving output in C

I am an absolute newbie, and I am learning to code.
#include <stdio.h>
#include <stdlib.h>
int main(){
char characterName[] = "Khan";
int characterYear = 2022;
float characterScore = 8.8;
char characterGrade = 'A';
printf("%s passed out of college in %d and had a grade of %s i.e. a score of %f\n",characterName,characterYear,characterGrade,characterScore);
return 0;
}
Can someone help me with this — I was just trying to incorporate all simple data types in one line (for reference) and after I run the program there is no output whatsoever, it just exits! It didn't happen when I had fewer data types in the line.
P.S. This might be the dumbest ever question ever asked on Stack Overflow, but please forgive if I am dumb/silly :)
Your format is wrong:
characterGrade has type char and you want to print it using %s format which requires char * parameter. It has to be %c instead:
"%s passed out of college in %d and had a grade of %c i.e. a score of %f\n"

Problem with C language, I don't understand the error

I'm learning C but I don't understand why the error appears on line number 9
---> scanf("%[^\n]s",&cadena); // i tried with "%s" but still doesnt work.
#include <stdio.h>
int main(void)
{
char cadena[40];
printf("Ingrese cadena: ");
fflush(stdin);
scanf("%[^\n]s",&cadena);
printf("La cadena es %s \n", cadena);
return (0);
}
Remove the ampersand on cadena in scanf
scanf("%[^\n]", cadena);
An array decays to a pointer so you were actually passing a pointer to a pointer in that case.
Also you can just write it like this
scanf("%s",cadena);
Depends on your end goal though.

scanf doesn't accept input

Only my first line of input request user to key in the value. Input B not request user to key in and shows wrong total.
#include <stdio.h>
#include <stdlib.h>
//BASIC CALCULATION INPUT 2 INTEGER ONE BY ONE
int main(int argc, char *argv[])
{
int a,b,c;
//REQUEST ONE INPUT
printf("Integer A: \n");
scanf("%a",&a);
//REQUEST ONE INPUT
printf("Integer B: \n");
scanf("%b",&b);
c=a+b;
//DISPLAY AMOUNT INTEGER
printf("Total: &c",c);
system("PAUSE");
return 0;
}
Your both scanf statements are wrong!
It should be
scanf ("%d",&a);
scanf ("%d",&b);
For taking user input a and b of type integer use %d.
&a is the reference (address) of identifer a which holds the value of a.
And also user output printf statement for integer c should be
printf("%d",c);
Add a space in the scanf to discard all whitespace before matching an integer. Such as a
scanf(" %b",&b);
^^^
space in the scanf
There is no format specifier like %b that's why Input B doesn't request user to key in.I think this this will help you.
#include <stdio.h>
#include <stdlib.h>
//BASIC CALCULATION INPUT 2 INTEGER ONE BY ONE
int main(int argc, char *argv[])
{
int a,b,c;
//REQUEST ONE INPUT
printf("Integer A: \n");
scanf("%d",&a);
//REQUEST ONE INPUT
printf("Integer B: \n");
scanf("%d",&b);
c=a+b;
//DISPLAY AMOUNT INTEGER
printf("Total: &c",c);
system("PAUSE");
return 0;
}
Both of your scanf statements use wrong format specifiers. Thus undefined behaviour.
scanf("%a",&a);
and
scanf("%b",&b);
a expects a float*as its argument, but you are passingint*. There's no format specifierb` in standard C either.
Use %d to scan int's.
Better yet, avoid scanf altogether and use fgets and parse the line instead.
Another problem is your printf statement:
printf("Total: &c",c);
is wrong too. It should be:
printf("Total: %c",c);
to print the value of c.

Scanf in c seems to require double input?

This is probably a really basic question but has me stumped so here goes.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float argument = 8.86;
printf("%f%\n", argument);
argument = 7.75;
printf("%f%\n", argument);
scanf("%f%\n", &argument);
printf("%f%\n", argument);
return 0;
}
I just wrote this code to help me figure out whats going on. Basically, the scanf seems to require me to put any number then press enter as expected, but then enter another number and press enter or just press enter (after which it will then print the value put the FIRST time time). Shouldn't the code just immediately print the entered number and finish?
Could anyone explain how or why this is happening, thanks!
You have an extra % in scanf() and printf() calls. scanf() attempts to intrepret it as additional conversion character. This results in undefined behaviour. Remove them.
If you want to print % sign, use %%.
E.g.:
printf("%f%%\n", argument);
First, you have an extra % in printf() and scanf() function;
Second, you should not have \n in scanf() function;
if have the \n, the console will ignore new line, space and all blank characters when you input.
Maybe like this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float argument = 8.86;
printf("%f\n", argument);
argument = 7.75;
printf("%f\n", argument);
scanf("%f", &argument);
printf("%f\n", argument);
return 0;
}
I am also a beginner in C language. We come together!
Try this:
#include <stdio.h>
int main()
{
char str1[20], str2[30];
printf("Enter String 1: ");
scanf("%s", &str1);
printf("Enter your String 2: ");
scanf("%s", &str2);
printf("String 1: %s\n", str1);
printf("String 2:%s", str2);
return(0);
}
**Only change in scan function **
from scan(%d\n)
to

Resources