Why this program does not print the desired output? - c

I just know about the %i format specifier from this link
Difference between format specifiers %i and %d in printf
and I tried to implement it with this program.
#include <stdio.h>
int main(){
long long a,b;
printf("Input: ");
scanf("%i %lld",&b,&a);
printf("Output: %i %lld",b,a);
}
%i worked properly but %lld stores a garbage value in variable a.
This is the output of this program.
Input : 033 033
Output : 27 141733920846
Process returned 0 (0x0) execution time : 4.443 s
Press any key to continue.
Can anyone explain, why I am getting the garbage value in variable a?

scanf %i takes an int *, but you're passing &b, which is a long long int *. This has undefined behavior.
You should be using %lli.
The same problem occurs in printf: Use %lli to print b, not %i.
You should also check scanf's return value to make sure two values were successfully read.

First of all, using %i for a long long int is undefined behavior, so use %lli instead.
The same issue persists in the printf statement, too.
Fixed code:
#include <stdio.h>
int main(){
long long a,b;
int retval;
printf("Input: \n");
retval = scanf("%lli %lld",&b,&a);
printf("Output: %lli %lld",b,a);
printf("\nRetval: %d",retval);
return 1;
}
Input:
033 033
Output:
Input: Output: 27 33 Retval: 2
Live Demo
Note: Always check the return value of scanf. It returns the number of scanned items, which you should test against your expectations.

Related

strange output number ( C )

#include<stdio.h>
int main(void)
{
int h;
int m;
scanf_s("%d : %d", &h,&m);
printf("%d : %d", h,m);
return 0;
}
That is it,
I typed 12 13
It gave me 12 : -858993460
ironic.. would you give me an idea?
If you typed exactly "12 13" as you wrote in the question, then the issue is that your input does not match the format specified in your call to scanf. In particular, scanf is only looking for input in the form of <some integer> : <some integer>. Note that your input does NOT include the : character. As such, scanf only successfully reads a number into your first variable (h), but fails to read into the second variable (m).
You can confirm this is the case by checking the return value of scanf, which returns "the number of input items successfully matched" (see the man page).
The reason you're seeing a "random" number in the output is that your variable m is never initialized. If you instead initialize it to 0, then you will no longer see a "random" value. This is good practice anyway.
For example (with error check):
#include<stdio.h>
int main(void)
{
int h = 0;
int m = 0;
int rc = scanf_s("%d : %d", &h,&m);
if (rc != 2) { // expect to read 2 values
printf("scanf failed, read %d value(s)\n", rc);
} else {
printf("%d : %d", h,m);
}
return 0;
}
Keep the format in scanf_s("%d : %d", &h,&m);
scanf():
The C library function int scanf(const char *format, ...) reads formatted input from stdin.
In your case, type 12 : 13
Your call to the scanf_s function expects a colon in the input, but you didn't type one, so nothing got stored in m in the call. As a result, you got a garbage value for m (as you didn't initialize it either). If you type in 12 : 13, then you should get the expected result.
Also, make sure to check the return value of scanf_s to make sure that you've got all of the values you want.

Unexpected behavior of printf()

I executed following code in ubuntu with gcc compiler.
As a=0, the second printf() prints some garbage value.
What kind of behavior is it by printf()?
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a = 0;
printf("\nThe Value of %ns : %d\n", &a, a);
printf("\n%d\n", a);
printf("\n\n");
return 0;
}
If you read e.g. this printf reference you will see that the %n format specifier will:
returns the number of characters written so far by this call to the function.
So it will overwrite the contents of a with the number of characters it has written so far, which should be 14 if I count correctly.
The output of the first printf is 0
Because the second argument of printf passes a by the value and it gets printed afterwards.
The output of the second printf is 14
Because at this time the value of a was replaced by the number of characters that was printed before %n by the first printf
As a=0, the second printf() prints some garbage value.
You can't believe that statement until you verify it!
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a = 0;
printf("\nThe Value of %ns : %d\n", &a, a);
assert(a == 0);
printf("\n%d\n", a);
printf("\n\n");
}
Any time you want to say "I'm sure that (some invariant) is true at place x", use assert to state that fact and let it be checked at runtime.
Output:
output.s: ./example.c:8: main: Assertion `a == 0' failed.

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.

Scanf continuous input in C

#include <stdio.h>
int main ()
{
double a=0;
char b=0;
scanf ("%d%c",&a,&b);
printf ("%d,%c", a, b);
return 0;
}
This is my code for a quick test program I wrote to play around with the scanf function in C. I am trying to have the user input something like 78X + 5 = 19 (then hit enter) and then parse that into variables a, b, and c where in this case a=78, b=5, c=19. In the sample code, when I type in 78X, c doesn't store a value to b and only prints "78, " and then terminates. Why won't it store a value to b?
If your input is 75x then below is the code which reads the value and stores it in a(75) and b(x) respectively
#include <stdio.h>
int main ()
{
int a=0;
char b=0;
scanf ("%d%c",&a,&b);
printf ("%d%c", a, b);
return 0;
}
The , in your format string is significant. The string %d,%c would match the input 78,x but it would not match 78x .
Also you need to use %f to scan and print a double. Using %d causes undefined behaviour (which may manifest itself as b seeming to not appear). Either change to %f, or change your double to an int.

how to convert char to int

I'm trying to write a short program were:
#include <stdio.h>
void main()
{
char=a,b,c;
printf("please place 3 numbers:\n");
scanf("%c%c%c", &a,&b,&c);
}
The exercise I'm trying to solve is how to change the char to int so if I write in a the number 3, I will get the number 3 Printed.
at this point I'm only getting the value.
I would appreciate any help.
The answer depends somewhat on what you can assume about the character set. If it's something like ASCII (or really, any character set that includes the digits in sequential order), you just need to offset the character value by the value of the character 0:
int aValue = a - '0';
I'm sure that C# provides better ways to do what you're trying to do, though. For example, see this question for some examples of converting strings to integer values.
First of all your syntax need some checking
You should know that you declare a variable this way (a char in this example):
char a;
If you want to declare multiple variables of the same type in a row you do :
char a, b, c;
If you want to assign a value to a declared variable :
a = '3';
Now to print a char using printf (man printf is a must read, more infos are in coreutils) :
printf("%c", a);
If you want to get the char from the command line, I recommand you to use getchar() (man getchar) instead of scanf because if suits better what you are trying to achieve and doesn't require you to use a syntax in scanf that I am sure you don't fully understand yet.
Your question is incredibly light on details, so here are several options:
#include <stdio.h>
int main()
{
char a,b,c;
printf("please place 3 numbers:\n");
scanf("%c%c%c", &a,&b,&c);
printf("Printing ints (auto-promotion): %d %d %d\n", a, b, c);
printf("Printing ints (explicit-promotion): %d %d %d\n", (int)a, (int)b, (int)c);
printf("Printing digits: %d %d %d\n", a-0x30, b-0x30, c-0x30);
return 0;
}
If the input is 123,
I expect the output to be:
Printing ints (auto-promotion): 49 50 51
Printing ints (explicit-promotion): 49 50 51
Printing digits: 1 2 3
Some things I fixed along the way.
main should return an int, not be void.
char=a,b,c; is a syntax error. You meant char a,b,c;
added a return 0; at the end of main.
You question is not quite understandable. Still I'll try to help. I think that what you want is to store an integer value in the char variable. You can do so by using the following code:
#include<stdio.h>
void main()
{
char a,b,c;
printf("Enter three numbers:\n");
scanf(" %c %c %c",&a,&b,&c); //notice the spaces between %c
}
Or if you want to enter a character and print its ASCII value, you can use the following code:
#include<stdio.h>
#include<conio.h>
void main()
{
char a,b,c;
printf("Enter three characters:\n");
scanf(" %c %c %c",&a,&b,&c);
printf("Entered values: %d %d %d",a,b,c);
getch();
}

Resources