Can anybody tell me how the "scanf" function works? [closed] - c

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.

Related

Store string into array letter by letter in C [closed]

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;
}

Is this program actually valid [closed]

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.

What I'm doing wrong? if statement with scanf not working properly with input [closed]

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 years ago.
Improve this question
I am trying to develop a small program which is available below:
When I run the program, even after the right input, it gives me the output described in else.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char course;
printf("Enter Your Course Name: \n");
scanf(" %s", &course);
if (course == 'TOEFL') {
printf("Yes, you are eligible \n");
} else {
printf("You Can Not Join Us \n");
}
return 0;
}
You mean
scanf(" %c", &course);
But also,
Strings in c MUST be wrapped with double qoutes, the expression 'TOEFL' must be generating a warning about multi character constant, do not ignore it.
Strings in c, are compared one character at a time, so you need to use a function called strcmp() for that.
To read a string, you need an array to store it in, and yes, the "%s" specifier
char cours[100];
scanf("%99s", course);
if (strcmp(course, "TOEFL") == 0) ...
course is a char variable, so it only can contain a single character.
Try changing its declaration to:
char course[10];
See my code comments for explaination //xxxxxx below
#include <stdio.h>
#include <stdlib.h>
int main()
{
char course; //this decl stores only single character not string
printf("Enter Your Course Name: \n");
scanf(" %s", &course); //passing string argument
if (course == 'TOEFL') {//can't compare strings with == use strcmp
printf("Yes, you are eligible \n");
} else {
printf("You Can Not Join Us \n");
}
return 0;
}

How to use the gets() command to take text input and display output using printf statement in c language? [closed]

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 want a program for c language that takes text input using gets() function,...and then, prints the same text using print f statement.please answer! :)
Try this:
#include <stdio.h>
int main()
{
char str[50];
printf("Enter a string : ");
gets(str);
printf("You entered: %s", str);
return(0);
}

input ouput functions in c [closed]

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<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char s[20];
for(int i=1;i<=3;i++)
{
printf("enter a name \n "); // printf & scanf
scanf("%s",&s);
printf("the names are %s \n",s);
}
for(int j=1;j<=3;j++)
{
puts("enter a name \n ");
gets(s); // puts and gets
puts(s);
}
char ch='a';
putchar(ch); // putchar and getchar
ch = getchar();
getch();
}
There is a repetition (three times) of the printf statement...can anyone explain this?
The output is like this:
enter name
my name is alex
the names are my
enter a name
the names are name
enter a name
the names are is
enter a name
alex
enter a name
alex again
enter a name
alex twice
alex twice
scanf with %s will read until the first whitespace character. So your input my name is alex is read over the space of 4 calls to scanf, which will make your output look strange. You might want to consider using the function getline if you want to read until the end of a line (http://en.cppreference.com/w/cpp/string/basic_string/getline)
Note that this was originally also tagged as c++, so I assume the OP can use c++ libs as well.

Resources