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 months ago.
Improve this question
#include <stdio.h>
int main(){
char arr[100];
printf("%d", scanf("%s", arr));
return 0;
}
Input value give for scanf is love2code
According to the C Standard (7.21.6.4 The scanf function)
3 The scanf function returns the value of the macro EOF if an input
failure occurs before the first conversion (if any) has completed.
Otherwise, the scanf function returns the number of input items
assigned, which can be fewer than provided for, or even zero, in the
event of an early matching failure.
So if the call of scanf was successful
scanf("%s", arr)
then its returned value is equal to 1 that is outputted by the call of printf.
printf("%d", scanf("%s", arr));
This statement can be rewritten like
int result = scanf("%s", arr);
printf("%d", result);
Pay attention to that the output does not depend on what string was entered (provided that ii can be accommodate in the array arr, otherwise there will be undefined behavior). It is important that only one item (arr) was assigned.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
#include<stdio.h>
int main()
{
char str(100);
int i;
printf("Please enter your name")
scanf("%s",str);
printf("your name");
return 0 ;
}
This is the code I have written,
but the problem is the code runs but never gives the output
until I manually stop the code.
How does the scanf("") function work?
Scanf reads formatted input from the standard input. There is nothing wrong with scanf itself. Your way of using it is also correct, but there are two problems with your code:
The first is that arrays must be defined with []. Therefore,
char str(100);
should be
char str[100];
The second problem is that you are receiving the input, but you are never printing it. To do so, you can write the following code:
printf("%s\n", str);
The final code being:
#include <stdio.h>
int main()
{
char str[100];
printf("Please enter your name");
scanf("%s", str);
printf("your name is: %s\n", str);
return 0;
}
Also, don't forget the semi-colons.
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 1 year ago.
Improve this question
Why does this program output "41"?
#include <stdio.h>
int main(){
int a = 0;
a = printf("4");
printf("%d", a);
return 0;
}
The function printf returns the number of outputted characters.
From the C Standard (7.21.6.3 The printf function)
3 The printf function returns the number of characters transmitted, or
a negative value if an output or encoding error occurred.
So at first in this statement
a = printf("4");
there is outputted the character '4' stored in the string literal "4" and a is assigned with the value 1 because only one character was transmitted and then in the next call
printf("%d", a);
there is outputted the integer 1 stored in the variable a assigned in the preceding statement.
You could get the same result using one statement the following way
printf( "%d", printf( "4" ) );
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 1 year ago.
Improve this question
I am trying to receive a word as input from a user and store it into an array which stores each letter as its own element.
basically the same as would be done if I were to initialise
char word[] = "hello";
so I could then use loops to check for certain letters.
I have tried using scanf in various ways with %c and %s specifiers and using a loop to read letter by letter, which did not work.
Everything I have tried so far just stores the full word as the first element in the array or just throws errors.
Thanks for the help!
What you describe cannot cause errors. Rather, you are implementing your plan incorrectly. Try this and you will succeed.
#include<stdio.h>
int main(void)
{
char w[100];
int i;
printf("Enter the word:");
scanf(" %s", w);
for(i=0;w[i]!='\0';i++)
printf("w[%d]=%c\n",i,w[i]);
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 2 years ago.
Improve this question
#include<stdio.h>
int main()
{
char word[1000];
scanf("word%s", word);
printf("%s", word);
}
It seems that when I input any string, as long as I type out "word" first, I get proper output.
But is this program actually valid
It compiles and therefore is valid from a syntax perspective. It's also fine in order to check that a prefix is used.
However, there are at least two ways to get undefined behaviour:
scanf might store more than 1000 characters (read 999 and one for the final \0)
scanf might read none if the input does not start with "word"
You should therefore check the result of scanf, initialize word, and also limit the maximum number of characters that scanf reads:
#include<stdio.h>
int main()
{
char word[1000] = {0};
int ret = scanf("word%999s", word);
if ( ret == 1 ) {
printf("%s", word);
}
}
this program is valid ,but you have to be careful about buffer overflow , which means if user input more than 999 chars this will lead to undefined behavior , so I suggest this:
scanf("word%999s", word);
also as you said as long as I type out "word" first ,otherwise char word[1000] will be uninitialized.
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 4 years ago.
Improve this question
How will this loop be executed infinite times?
#include <stdio.h>
int main() {
int i;
for (; scanf("%d", &i); printf("%d\n", i));
return 0;
}
Condition part of for loop is scanf("%d", &i); which return true until user provided invalid input . Read man 3 scanf and check return value.
Better run loop until you press key like Ctrl+d
for (; scanf("%d", &i) != EOF; printf("%d\n", i));
Or compare the return value of scanf(),as scanf("%d", &i) == 1 returns 1 as long as it is able to convert user input into integer.
for (; scanf("%d", &i) == 1; printf("%d\n", i));
That will run until scanf returns a zero value, so it could run forever so long as there's sufficient input. End-of-file will be zero.
This is a really ugly way of expressing that logic. Just use a while.
Actual files aren't infinite length, but there are things like the yes utility that produce endless streams of output that could be piped into a program like this.