char nation[100];
printf("Enter Your Name : ");
gets(nation);
int size = strlen(nation);
printf("Output = %f\n", size);
I need to solve the question, please.
char nation[100];
printf("Enter Your Name : ");
fgets(nation, sizeof(nation), stdin); //gets is depreciated use fgets instead
size_t size = strlen(nation); //strlen returns size_t not int
printf("Output = %zu\n", size); //%f is to print floats
You need to tell printf what type you want to print:
%c character
%d decimal (integer) number (base 10)
%e exponential floating-point number
%f floating-point number
%i integer (base 10)
%o octal number (base 8)
%s a string of characters
%u unsigned decimal (integer) number
%x number in hexadecimal (base 16)
%% print a percent sign
Related
I've tried to build a simple calculator for physics force experiments.
//
// main.c
#include <stdio.h>
int main() {
char name[20];
int month,date;
int difference_percentage1, difference_percentage2, difference_percentage3;
double force1, force2, force3;
scanf("%s", name);
scanf("%d %d", &month, &date);
scanf("%lf %lf %lf", &force1, &force2, &force3);
double Favg=(force1 + force2 + force3)/3.000;
puts(name);
difference_percentage1=100*(force1-Favg)/Favg;
difference_percentage2=100*(force2-Favg)/Favg;
difference_percentage3=100*(force3-Favg)/Favg;
printf("%d %d %d\n", difference_percentage1, difference_percentage2, difference_percentage3);
getchar();
return 0;
}
The calculate doesn't match what I've typed for scanf().
The o's of the % mark for the %f look a bit larger to me than for the %d, so my guess is that you used the wrong % for the %f format specifier which isn't recognized by the compiler; it interprets the double value as int.
Edit: one more reason to post text instead of a screenshot of the code! (o;
Edit2: Yup, your % mark in %f is actually a "EF BC 85" in hex but should be "25"
I'm still a beginner at C, so I'm finding difficulty in understanding "%d! = %ld".
I know that %d and %ld are respectively used for an integer and long, so "! =" is confusing me.
#include<stdio.h>
long factorial(int);
int main() {
int n;
long f;
printf("Enter an non-negative integer: ");
scanf("%d", &n);
if (n < 0)
printf("Negative integers are not allowed.\n");
else {
f = factorial(n);
printf("%d! = %ld\n", n, f); //what does this mean?
}
return 0; }
long factorial(int n) {
if (n == 0)
return 1;
else
return(n * factorial(n-1)); }
This will print:
%d, i.e. the decimal value of int n
! =, i.e. the literal character sequence
%ld, i.e. the decimal value of long f
%d and %ld are the formatting placeholders for int and long int in printf. The exclamation point is just the factorial symbol, as mentioned in the comment.
printf() allows you to print a string with variables inside of it. Let's say you have a variable i, containing an integer, 7.
printf("My variable is %d", i);
Will print
My variable is 7
to the console! That's because %d is how you tell printf(), "Hey, put an integer variable here!". The integer is then supplied as the next argument to the function. In your case, %d represents the integer n, and %ld represents the long integer f. Since f might be really big, we make it a long, which means more bytes are allocated to it internally on your computer. So for example, if we wanted to get the factorial of 5 and print it, we might do the following:
printf("Factorial of %d equals %ld\n", 5, factorial(5))
// this will print "Factorial of 5 is 120" then a newline
Oh, and \n just means print a newline afterwords!
printf("%d! = %ld\n", n, f); //what does this mean?
%d - print an integer as a signed decimal number.
l - specifies that the argument is a long int or unsigned long int as appropriate. %ld then prints a long int or unsigned long int
The printed text will become something like
n! = f
(factorial notation n!)
I want my code to prompt the user to input a phone number of form 1(xxx)-xxx-xxxx and then sum the digits of the number. However I do not know what is wrong with my code. See below
printf("Enter a phone number in 1(xxx)-xxx-xxxx format: \n");
scanf(" %*c%*c%d %d %d %*c%*c%d %d %d %*c%d %d %d %d", &i, &j, &k, &l, &m, &n, &o, &p, &q, &r);
sum = (i + j + k + l + m + n + o + p + q + r);
realsum = sum + 1;
printf("The sum of the digits = %d \n\n", realsum)
;
Can anyone help? It seems to be assigning the first part of the number (xxx) entirely to i, and j is zero. How do I get it to assign each digit to each variable one by one?
The problem is that %d keeps reading until it finds a character that can't be part of a decimal number. So if the user enters 1(123)-456-7890 then the first %d will set i to 123.
The solution is to use %1d. That tells scanf to read a one-digit decimal number.
btw: you should verify that the return value from scanf is correct. In this example, the correct return value is 10. Any other number indicates that the user did not enter a valid phone number.
You did account for the non-integer characters that the user enters, but integers are read as a whole, so 123 is not read as1 then 2 then 3 but rather as 123.
scanf(" %*c%*c%d %*c%*c%d %*c%d ", &i, &j, &k);
Let me start by saying that I am brand new to programming. I am a math major and I don't know very much about the computer programming world. That said, my assignment wants me to enter an integer and print it out as the corresponding ASCII character, decimal, float. I am ok going from a character to the corresponding integer, but the reverse has me puzzled. Please help!
#include <stdio.h>
int main (void)
{
char E = 'E';
char e = 'e';
char D = 'D';
char d = 'd';
int m;
printf("\nPlease enter an integer : ");
scanf("%d", &m);
printf("\nThe number as a character is : %c", E, e, D, d);
return 0;
} // main
This is what I have so far, but I know it's wrong.
I am not exactly sure what you want to achieve.
printf will treat a parameter as a type you want using % format specifier.
So if you have entered some value which is interpreted as signed decimal integer you can print it treating as a different type with the printf function.
If you want your m variable being printed as character do:
printf("The number as a character is %c", m);
If you want to display it as a float, use %f.
Here is some reference:
http://www.cplusplus.com/reference/cstdio/printf/
You're probably looking at the printf formats
#include <stdio.h>
int main (void)
{
int m;
printf("Please enter an integer : ");
scanf("%d", &m);
printf("The number as a character is : '%c' %02X %o %d %f", m, m, m, m, (float) m);
return 0;
}
I would like to avoid 'floating point error in this code.
A purpose of this code is to gain 'The average of whole numbers' but the number of 'whole numbers' is limited by the input of users. Please help me.
#include <stdio.h>
int main(void)
{
int num=0;
int limit;
int result=0;
printf("number of integer: ");
scanf("&d", &limit);
while(num<limit)
{
int output;
printf("Input integer : ");
scanf("%d", &output);
result += output;
num++;
}
printf("average of total integer: %d \n", result/limit);
return 0;
}
Thank you for reading.
When you divide 2 integers, the result is also an integer.
To return a float, you need to cast one of the arguments as a float.
So your last line becomes
printf("average of total integer: %f \n", result/(float)limit);
As the result of two integer dividing is also an integer,so it as
printf("average of total integer: %f \n", result/(float)limit);
when you type cast the variable limit to float what happens is that result will be implicitly converted to float and so the result is a float.