getting wrong output when using char and int [duplicate] - c

This question already has answers here:
Scanning with %c or %s
(2 answers)
Why does C's printf format string have both %c and %s?
(11 answers)
Closed 4 years ago.
Writing a simple code that's suppose to scan an integer and a character and then write them out.
my input is 1a and the output should be 1a but i'm getting 0 on the integer spot. have a pretty basic understanding of c so may have missed something that's pretty obvious thanks in advance.
#include <stdio.h>
int main()
{
int a;
char b;
scanf("%d", &a);
scanf(" %s", &b);
printf("%d", a);
printf("%s", &b);
}

b is a character so replace %s with %c, moreover
scanf() takes & before the variable as it want to store the variable refering to that address.
2.printf() Just outputs the value to the console present in that varaible.
Thereby no need to use & inside it
CORRECTED CODE:
#include <stdio.h>
int main()
{
int a;
char b;
scanf("%d", &a);
scanf(" %c", &b);
printf("%d", a);
printf("%c", b);
}

b is a char, %s is for string input so adds trailing 0 after b and you can get a crash. Use %c to input char.

You basically want this:
#include <stdio.h>
int main()
{
int a;
char b[100]; // array of 100 chars
scanf("%d", &a);
scanf("%s", b);
printf("%d", a);
printf("%s", b);
}
To fully understand this, you need to read the chapters dealing with scanf and the one dealing with strings in your C text book.

you can try it:
#include <stdio.h>
int main()
{
int a;
char b;
scanf("%d", &a);
scanf(" %c", &b);
printf("%d", a);
printf("%c", b);
}

Related

calculation program in C [duplicate]

This question already has answers here:
(Why) is using an uninitialized variable undefined behavior?
(7 answers)
Closed 2 years ago.
#include <stdio.h>
int main()
{
int a, b, c;
c = a * b;
printf("a는?: ");
scanf("%d", &a);
printf("b는?: ");
scanf("%d", &b);
printf("%d * %d = %d ", a, b, c);
return 0;
}
i made a calculation program . but the result is wrong.
if i put 5,10 in a,b
c should be 50(510)
but the result is 0
i used ab instead of c . then it was solved. but i want to use c variation.
what is the problem?
should i use double instead of int? please tell me the reason why the problem evoked
You're assigning c = a * b at a point when a and b doesn't have proper values. You should do this calculation after assigning values to a and b, otherwise result will be some garbage value.
Right way to do it:
#include <stdio.h>
int main()
{
int a, b, c;
printf("a는?: ");
scanf("%d", &a);
printf("b는?: ");
scanf("%d", &b);
c = a * b;
printf("%d * %d = %d ", a, b, c);
return 0;
}

convert a char element from an array to an int

Hi I'm trying to convert elements of an array into integers using input from the user.
#include <stdio.h>
#include <string.h>
int main()
{
char i[9]={'-','-','-','-','-','-','-','-','\0'};
int j;
printf ("enter an integer for an element ");
sscanf(i, "%d", &j);
return 0;
}
I read somewhere that using sscanf is one way to do it but I don't know the correct way to use it.
scanf reads from the standard input.
sscanf reads from a string.
Example of scanf usage:
#include <stdio.h>
int main()
{
char str1[20];
printf("Enter name: ");
scanf("%s", str1);
printf("First char converted to int: %d\n", str1[0]);
int j = str1[1];
printf("Second char converted to int: %d\n", j);
return(0);
}
Input and Output:
Enter name: marcel
First char converted to int: 109
Second char converted to int: 97
As Marcel said, there are two different functions that do different things.
You request convert elements of an array into integers using input from the user, so I can think you want this:
char i[9]={'-','-','-','-','-','-','-','-','\0'};
int j;
printf ("enter an integer for an element ");
scanf("%i", &j);
printf("Element[ %i ] in integer is: %i", j, i[j]);

How to understand the data move from A to B in character array?

#include <stdio.h>
int main ()
{
char a[10], b[9], c[5];
scanf("%s", a);
scanf("%s", b);
scanf("%s", c);
printf("%s\n", b);
printf("%s %s %s", a, b, c);
return 0;
}
when input c[] array's number > 5, the rest osf the characters will be wrriten to b[] array, why?
for example:
input:
program
is
wonderful
output:
rful
program rful wonderful
You're suffering C buffer overflow.
Try to avoid using scanf("%s", char[]) for this purpose.
Better use fgets() or similar.
Offtopic:
Interestingly this is related on how Nintendo's Wii was cracked. Or so it says the urban legend.

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.

Why my program combines two printf commands?

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

Resources