Scanf in c seems to require double input? - c

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

Related

Hangup In A Simple Test C Program From Scanf to Printf

Putting the toes into the C programming and tried this test after another small program wasn't working:
#include <stdio.h>
int main()
{
int i;
printf("Test1\n");
printf("Test2\n");
printf("InputTest1: ");
scanf("%d\n", i);
printf("OutputTest1: %d\n", i);
printf("InputTest2: ");
scanf("%d\n", i);
return 0;
}
Output:
Test1
Test2
InputTest1: 10
For some reason that I cannot surmise when going from scanf to printf it hangs and then doesn't print anymore.
Thank you for your assistance,
First of all you need to pass a pointer to i to scanf(), not i itself (C doesn't have arguments by reference). But scanf() is fraught with problems for user input and it would be better to use fgets() and convert the number using strtol().

Output is `blank` space for the normal input on my C code

#include <stdio.h>
int main(void) {
char str[100];
scanf("%s", str[0]);
printf("%c", str[1]);
return 0;
}
I am running this code. I have entered "Jagrit" as an input. I expect the output of above program is 'j'. But instead i get nothing as a output just a blank space. Can anyone tell me why is it so and what was the error in the code ?
scanf expects the address of memrory to write to read data to:
#include <stdio.h>
int main(void) {
char str[100];
scanf("%s", str);
printf("%c",str[0]);
return 0;
}
Have a look at the example section of this doc on scanf.
What was your intention with scanf("%s", "jargrit dolir") ?
This
scanf("%s","jagrit dolir");/*it doesn't put data into str, and doing that causes UB*/
So accessing str[0] may cause undefined behavior because str doesn't initialized & it's not having any data.
Instead use like below.
scanf("%s",str);/* now give input like jagrit dolir */
And then print str[0].
Edit :- since you modify the code. Have you read the manual page of scanf() ?
scanf("%s",str[0]);/* why you are not reading compiler warning here ?*/
Here %s expects argument of char* but you provided char type.
if you just want to take a input as a string can use fgets()
and just use str[0] to print the first character of the string
note that
printf() is used to print values only;
here is the simple version of the code:
#include <stdio.h>
# include<stdlib.h>
int main() {
char str[100];
fgets(str,100,stdin);
printf("%c",str[0]);
return 0;
}
when you use str[0] , you access it's value so the scanf should be like thit scanf("%s",&str[0]); or like this scanf("%s",str);
you also should expect the output to be 'a'
try this code
#include <stdio.h>
int main()
{
char str[100];
printf("%s","jagrit dolir");
printf("%c",str[0]);
return 0;
}

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.

How do I get the user to input a word for string comparison?

I'm running a while loop so the user can constantly enter expressions, until they indicate they want to quit the program. I'm using strcmp() to compare two strings so as soon as they enter quit the program will stop. But the program keeps going, any Ideas?
#include <stdio.h>
#include <string.h>
int main()
{
int min12=0;
char opper;
int x=0;
int min13;
char *Repeatprog="cont";
char *Repeatprog1="quit";
while (strcmp(Repeatprog,Repeatprog1))
{
printf("enter the integer number \n");
scanf( "%d %c %d", &min12, &opper, &min13);
printf("%d %c %d\n", min12, opper, min13);
printf("Type the word quit to end program\n");
scanf("%s", Repeatprog);
}
printf("Good Bye");
return 0;
}
Remember always that an Array is a Pointer to the first object of the array.
And secondly, in your call to scanf() you only read a character. Not a whole string (represented by %s in C)
So in conclusion, your call to scanf() shouldn't have a pointer and should have a string instead of a character.
scanf("%s", Repeatprog);
or simply
gets (Repeatprog);
EDIT :
As the commenter #EOF said, gets() is not a good idea since it can lead to Undefined Behaviour. That's because the program can read more characters than it should have and lead to overflow, thus it isn't secure.
So I recommend using char *fgets(char *str, int n, FILE *stream)
Note:
Also, your code is using string literals. So if you make any attempt to change the content of the char pointer then it will lead to Undefined Behaviour.
For this note, please thank the guys below me [comments]. I made a huge mistake and I'm sorry.

Reading multiple lines of input with scanf

Writing a program for class, restricted to only scanf method. Program receives can receive any number of lines as input. Trouble with receiving input of multiple lines with scanf.
#include <stdio.h>
int main(){
char s[100];
while(scanf("%[^\n]",s)==1){
printf("%s",s);
}
return 0;
}
Example input:
Here is a line.
Here is another line.
This is the current output:
Here is a line.
I want my output to be identical to my input. Using scanf.
I think what you want is something like this (if you're really limited only to scanf):
#include <stdio.h>
int main(){
char s[100];
while(scanf("%[^\n]%*c",s)==1){
printf("%s\n",s);
}
return 0;
}
The %*c is basically going to suppress the last character of input.
From man scanf
An optional '*' assignment-suppression character:
scanf() reads input as directed by the conversion specification,
but discards the input. No corresponding pointer argument is
required, and this specification is not included in the count of
successful assignments returned by scanf().
[ Edit: removed misleading answer as per Chris Dodd's bashing :) ]
try this code and use tab key as delimeter
#include <stdio.h>
int main(){
char s[100];
scanf("%[^\t]",s);
printf("%s",s);
return 0;
}
I'll give you a hint.
You need to repeat the scanf operation until an "EOF" condition is reached.
The way that's usually done is with the
while (!feof(stdin)) {
}
construct.
Try this piece of code..
It works as desired on a GCC compiler with C99 standards..
#include<stdio.h>
int main()
{
int s[100];
printf("Enter multiple line strings\n");
scanf("%[^\r]s",s);
printf("Enterd String is\n");
printf("%s\n",s);
return 0;
}

Resources