Hangup In A Simple Test C Program From Scanf to Printf - c

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().

Related

Using scanf funciton in printf to get values

#include<stdio.h>
#include <stdlib.h>
int main ()
{
int i;
printf ("%d", scanf ("%d", &i));
}
while using the
scanf
function in
printf
statement,when i give any integer as input it prints only 1.
Input:5 output:1
i am beginner in c ,so kindly help.
can any one explain this program ?
scanf() returns number of receiving arguments successfully assigned or EOF in case of failure.
Read more about scanf() here.
In context of your program, when you give input 5 the scanf() successfully assign it to receiving argument i and since the receiving argument is 1, scanf() is returning 1 which is getting printed. Hence you are getting output 1.
Try to give a character as input and check the output.
you need something like this
EDIT - more info here
scanf_s is for reading from console
printf to print on screen
int main()
{
int i;
scanf_s("%d", &i);
printf("%d", i);
return 0;
}

Eclipse IDE always ask for input first regardless of actual code order

I'm currently studying beginner C programming using Eclipse IDE.
Below is a very basic program that suppose to print out a line first, take in some input, and then print out a second line. But my Eclipse always ask me for the input first before printing out the two lines together afterwards.
This is extremely frustrating because it is something so simple and doesn't work. What am I doing wrong?
#include <stdio.h>
int main(void){
int aNumber;
printf("first line\n");
scanf("%d", &aNumber);
printf("second line with %d", aNumber);
return 0;
}
Had the same problem once.
Solved it by flushing stdout.
#include <stdio.h>
int main(void){
int aNumber;
printf("first line\n");
fflush(stdout); // Prints stdout content
scanf("%d", &aNumber);
printf("second line with %d", aNumber);
return 0;
}

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

Simple test program using sscanf does not work

I was wondering why the following does not work:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int nbytes = 100;
char* string;
string = malloc((sizeof(char) * nbytes) + 1);
int x, y, z;
int args_assigned = 0;
while (args_assigned != 3)
{
printf("Please enter three integers separated by whitespace\n"
scanf("%s", string);
args_assigned = sscanf(string, "%d %d %d", &x, &y, &z);
}
free(string);
printf("Thanks for %d %d %d\n", x, y, z);
return 0;
}
In the original version of this code, the programmer had used getline instead of scanf, but apparently this is no longer a valid function in C99? Please correct me if I am wrong.
Anyway, I suspect the problem is to do with my use of scanf. Is there another function I should be using to provide the string for sscanf to parse? Of course, there could be something far more fundamental that I am missing here...
You ask the user to enter three integers separated by spaces.
You try to read that information with "%s" which stops reading at the first space. This is why you run into problems. You could have found this out by printing what you get from your scanf() call.
Don't forget to check the return value from sscanf().
There is a POSIX function getline() which reads a whole line and tells you how long it is. Alternatively, there is the standard C function fgets() which can be used to read whole lines.
At a pinch, you could use scanf(" %[^\n]", string), but I really wouldn't recommend it; use getline() or fgets().
Be sure to finish this line:
printf("Please enter three integers separated by whitespace\n"
To read a line of input, use fgets().
fgets(string, nbytes + 1, stdin);

Why doesn't my output show up until the program exits?

I have a simple program from a C programming book, and it's supposed to ask for two integers and then add them together and show the sum. I'm able to enter the two numbers, but the output doesn't show up until the very end of the program.
#include <stdlib.h>
#include <stdio.h>
/* Addition Program*/
main()
{
int integer1, integer2, sum;
printf("Enter first integer\n");
scanf("%d", &integer1);
printf("Enter second integer\n");
scanf("%d", &integer2);
sum = integer1 + integer2;
printf("Sum is %d\n", sum);
return 0;
}
The output looks like this:
2
6
Enter first integer
Enter second integer
Sum is 8
Any help would be greatly appreciated, thanks!
It is possible that the output is not being flushed automatically. You can add fflush(stdout) after each printf() and see if that helps.
Which environment are you using to build and run this program?
Further to the above, printf will only automatically flush it's buffer if it reaches a newline.
If you are running on windows, a newline is \r\n instead of \n.
Alternatively you can do:
fflush(stdout);
Another alternative is to turn off buffering by calling:
setbuf(stdout, NULL);
EDIT:
Just found this similar(but not the same) question:
Why does printf not flush after the call unless a newline is in the format string?

Resources