#include<stdio.h>
int main()
{
int i, j;
for(scanf("%d ",&i); i<=10; i++)
printf("%d ",i);
return 0;
}
I am a beginner in the programming world so please help me understand why on compiling the above C code it asks inputs twice.Maybe there's some logic to loop here I might be missing. Please help me understand.Thanks in advance.:)
Change this:
scanf("%d ",&i);
to this:
scanf("%d",&i);
Read more in What does space in scanf mean?
Currencly, you placed scanf() in a for loop, which it asks for input for a 10 times.This will not happen when you remove scanf() from for loop.
I was facing the same problem and the only change I did was to change "%d " to "%d". This solves the problem.
Related
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().
I seriously don't know what is wrong. It may be something very simple, but I can't find the error. I wrote this very simple program in C:
#include<stdio.h>
int main(void) {
int n;
scanf("%d", &n);
printf("\n%d", &n);
return 0;
}
But when I ran it, this is what I got:
1 // My input
-1936471972
What am I doing wrong?
Thanks in advance!
I'll just post what the others have already pointed out:
int main(void) {
int n;
scanf("%d", &n);
printf("%d\n", n);
^^ ^
return 0;
}
Remove the & from &n if you just want to display the value of the variable (you were basically printing the address of the variable) and move the \n after you have printed the variable as explained by Weather Vane.
I seriously don't know what is wrong.
printf("\n%d", &n); use a print specifier of "%d", which expects an int. &n is the address of an int. What is really good about this is the modern compilers and compilers with their warnings well enable, will automatically warn about this error. This saves you time! No need for an SO post.
Proficient coders uses tools like a compiler with is warnings well enabled to be efficient and focus on the subtle problems a compilers cannot detect. Using printf("\n%d", n); or printf("%d\n", n); may solve today's small problem, but using a better compiler environment is really the thing to learn from all this.
Use that
printf("%d\n", n);
instead of
printf("\n%d", &n);
scanf takes a variable address as a parameter, butprintf takes the variable value. Try changing the line to
printf("\n%d", n);
I'm a beginner in C and am currently experiencing some troubles invovling chars.
I'm having an issue trying to enter values in a char matrix and then print it.
Here's my code:
#include <stdio.h>
#define N 3
int main( )
{
char arr[N][N]={{0}};
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N;j++){
scanf("%c",&arr[i][j]);
}
}
for(i=0;i<N;i++){
for(j=0;j<N;j++){
printf("%c",arr[i][j]);
}
}
return 0;
}
There are two chars that are missing at the end of the output.
I don't know what I'm doing wrong and I would like to understand my mistake:
-Is it some kind of problem involving the scanf fonction? I've heard about the buffer before, is that related? Is the problem coming from the moment I press enter?
-Am I initializing my matrix in a wrong way?
-Is it better to use getchar() in this situation? If so, how can I manage to enter exactly N*N values and not more?
Thanks a lot.
Jordan.
You should use " %c" to accept char as input. When you add a space before "%c" it consumes white-spaces (newline, tab, space, etc.) entered with previous inputs.
#include <stdio.h>
#define N 3
int main() {
char arr[N][N]={{0}};
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N;j++){
scanf(" %c",&arr[i][j]);
// ^ --- the space before %c
}
}
for(i=0;i<N;i++){
for(j=0;j<N;j++){
printf("%c ",arr[i][j]);
}
printf("\n");
}
return 0;
}
Input:
a b c d e f g h i
You can also input these characters one by one using Enter or Return button.
Output:
a b c
d e f
g h i
You may also check this post which addresses the same issue.
Another way to do this.. Add getchar(); statement after scanf() statement.
for(i=0;i<N;i++){
for(j=0;j<N;j++){
scanf("%c",&arr[i][j]);
getchar();
}
}
getchar() will consume tailling new lines.
In this case, just enter the 9 characters in sequence without pressing enter button like
123456789\n
because putting a newline character after each character is stored in one of character index becuase \n is a character itself which surely you don't want to be entered like this
1\n2\n3\n4\n5\
In above case only 5 characters would be taken from user intentionally.
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?
I can't figure out the problem in this:
#include<stdio.h>
int main()
{
int a,b,count ;
count =0;
printf("enter the value for a ");
scanf("%d ",&a);
while(a>0)
{
b=a%10;
count=b+count;
a=a/10;
printf ("hence the simplified result is %d",count);
}
return 0;
}
There's a silent killer in your code:
scanf("%d ",&a);
The extra space in your scanf will make entering numbers harder: this will match 12<space>, but not 12. Replace the "%d " with "%d".
You do not terminate your printf() with a "\n". The output stream (stdout) is, usually, line buffered. That means that incomplete lines need not be printed unless you force them with fflush(). But there's no need for that.
Simply add a "\n" to your printf()
printf("hence the simplified result is %d\n", count);
One issue is you print the count with every loop, rather than than after the loop.
Not an issue, but C has arithmetic assignment (aka compound assignment) operators that can be more readable. For example, a /= 10 is equivalent to a = a/10.
I think the printf statement should be outside the loop.
Move the printf out side the loop. That will fix it.
Try the following:
#include<stdio.h>
int main()
{
int a,b,count ;
count =0;
printf("enter the value for a ");
scanf("%d",&a);
while(a>0)
{
b=a%10;
count=b+count;
a=a/10;
}
printf ("hence the simplified result is %d",count);
return 0;
}