Why my program combines two printf commands? - c

So here is my program. It is suposed to write out square of some intiger.
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.");
scanf("%i", &a);
printf("Square of that intiger is %i", a*a);
return 0;
}
When i run a program in Eclipse it first requires me to input a number.I put in 5. And then as output it gives me
Type an intiger.Square of that intiger is 25.
It should first print "Type an intiger" and then the rest. But it just combines two printf commands. What is the problem?

You need a newline character - printf("Type an intiger.\n");
In computing, a newline, also known as a line break or end-of-line
(EOL) marker, or simply break, is a special character or sequence of
characters signifying the end of a line of text.
Also format specifier for integer is %d
scanf("%d", &a);
printf("Square of that intiger is %d", a*a);

If you want it on separate lines you can always add '\n' to the string to get a new line.
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.\n");
scanf("%i", &a);
printf("Square of that intiger is %i", a*a);
return 0;
}

There is 2 problem in it. First, if you input the integer, it should be %d. Example :
scanf("%d", &a);
The second, after the input, you should print \n. So, it will be like this printf("\n");. Take a look at my code :
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.");
scanf("%d", &a);
printf("\nSquare of that intiger is %d", a*a);
return 0;
}

In code::blocks it compiles fine anyway put a \n at the end of the first printf and change %i with %d

Related

Why is scanf running taking my second input?

Why is my scanf for my array running before the for loop? Output [enter image description here][1]
#include <stdio.h>
void main()
{
int num,i,arr[10];
printf("The number you want to add with each number is:");
scanf("%d \n",&num);
// printf("%d \n",num);
printf("the 10 numbers are:\n");
for(i=0;i<=9;i++){
scanf("%d \n",&arr[i]);
}
printf("The new array is:");
for(i=0;i<=9;i++){
arr[i] = arr[i]+ num;
printf("%d \n",arr[i]);
}
}
It's because you are using trailing white spaces in your scanf() syntax. In addition, you are using \n in scanf and your scanf here is not only scanning the number %d, but scanning the whole thing in between quotations (" "). and that causes unexpected errors. So you should write your code in another way.

Stuck on multiplication table in C

I'm new to C programming. I was trying to write a program that accepts an integer from user and displays its multiplication table up to 10 multiples.
This is my program:
#include <stdio.h>
int main ()
{
int number;
int count = 1;
int sum;
printf("Enter a number to display its table: ");
scanf(" %i ", &number);
while (count <=10)
{
sum = number * count;
printf("%i x %i = %i\n", number, count, sum);
count += 1;
}
return 0;
}
Compilation successfully completes, but when I execute the output file, nothing happens, the terminal is stuck at nothing, i've to press ctrl+c to get out..
This is due to the spaces used in your scanf command.
If you replace that with
scanf("%i", &number);
you get an instant response.
With your scanf format " %i ", the scanf function will read (and skip) possible leading spaces because of your leading space in the format.
Then it will read the integer.
Then, due to the trailing space, it will read and discard space until it find a non-space input.
Since there's no non-space input afterward, then scanf will block until you give some non-space input.
Solve simply by not having any spaces in the format. Or by entering some extra dummy input (followed by Enter).
The problem resides with the scanf.
Just replace
scanf(" %i ", &number);
with:
scanf("%i", &number);
and it will work.
#include <stdio.h>
int main (){
int number;
int count = 1;
int sum;
printf("Enter a number to display its table: ");
scanf("%d", &number);
while (count <=10){
sum = number * count;
printf("%d * %d = %d\n", number, count, sum);
count += 1;
}
return 0;
}
Note: You can use both %d or %i where %d specifies signed decimal integer while %i specifies integer.
Problem: The problem of your code was using a whitespace before %i.
Wrong:
scanf(" %i ", &number); //Wrong
Right:
scanf("%i", &number); //Right.

C: Extra output in printf

I've just started to learn C programming last week and I've learnt about some basics about it. So now I'm trying to make a program which can add up two numbers and show the result.
Here's my code:
#include <stdio.h>
int main (void)
{
int a;
int b;
int result;
printf("Insert a number:%d\n");
scanf ("%d",&a);
printf ("Insert the next number:%d\n");
scanf ("%d",&b);
result = a + b;
printf ("Result is:%d\n",result);
return 0;
}
It can be compiled and run but the following result is shown.
[1]http://i.stack.imgur.com/4Xjdv.png
Can someone please help me to get rid of that 4200612, which is output at the first printf statement? Thanks for your help and sorry for my bad english.
There is no need of %d in first two printf statement.
printf("Insert a number: ");
scanf ("%d",&a);
printf ("Insert the next number: ");
scanf ("%d",&b);
Since there is no corresponding argument. It will print some random value.
Try getting rid of the extra %d's in your printfs.
#include <stdio.h>
int main (void)
{
int a;
int b;
int result;
printf("Insert a number:\n");
scanf ("%d",&a);
printf ("Insert the next number:\n");
scanf ("%d",&b);
result = a + b;
printf ("Result is:%d\n",result);
return 0;
}

Problems with functions

I have two problems with the code, the first one being that the program wants me to enter my number twice and the second one being that the program closes down immediately after it has finished its process.
I have tried to use the getchar() statement to stop it doing so but it doesn't seem to work.
#include <stdio.h>
int square(int); /*function prototype*/
main()
{
int x; /*defining the function*/
printf("Enter your number\n");
scanf_s("%d \n", &x); /*reading the users input*/
printf("Your new answer is %d \n", square(x)); /*calling the function*/
getchar();
getchar();
}
int square(y) /*actual function*/
{
return y * y;
}
Fix the issue by changing
scanf_s("%d \n", &x);
to
scanf_s("%d", &x);
The problem was that a whitespace character (space, newline etc) in the format string of scanf insructs scanf to scan and discard any number of whitespace characters, if any, until the first non-whitespace character.
As for the problem with getchar(), replace the first getchar() with:
int c;
while((c = getchar()) != '\n' && c != EOF);
This will scan and discard everything until a \n or EOF.
Also, change
main()
to
int main(void)
and
int square(y)
to
int square(int y)
I would recommend using scanf("%d", &x); to read your number. Your problem is that your argument looks like this:"%d \n" so the program expects you to enter your number AND \n. This way, you say how you want your x to look, and in your case, it expects it to be a numeric value, a space and end of line.
As for the closing one, use getch();. For this function, you need to include the conio.h like you did with stdio, meaningly: #include <conio.h>.

Simple for loop running infinitely

I'm new to C and I need help with this simple exercise using for. I need to get a char and an int value from the user. Then I have to print that char as many times as the int entered before.
This is what I have:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a;
printf("Enter a character:");
scanf(" %c", &a);
int n;
printf("Enter a number:");
scanf(" %c", &n);
printf("\n");
int x;
for(x=0; x < n; x++){
printf(" %c", a);
}
return 0;
}
My problem is that it makes an infinite loop in the for.
Please I need your help.
Thanks
Here, n is an int, not a char. Thus, you need to use %d to read it. Using %c here will cause undefined behavior. According to "C99 – ISO 9899-1999":
§7.19.6.2 The fscanf function1
[...] If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.
Change
scanf(" %c", &n);
to
scanf(" %d", &n);
Check out here for more info.
1: The scanf function is equivalent to fscanf with the argument stdin interposed
before the arguments to scanf.

Resources