#include<stdio.h>
int main()
{
int a=3;
printf("%d"+1,a);
return 0;
}
Why is the output of this program is "d"?
And if this int specifier (%d) is replaced by float (%f) then it gives output as "f".
Please explain this code...
"%d" + 1 is the same as &"%d"[1], i.e. a pointer to the second character in the string. printf starts interpreting the string from there on as the format specifier, which is thus the same as just "d" (and the second argument is simply ignored).
Related
If you run the following code you will get the ASCII characters, even though we use int i within %c inside the for loop.
#include <stdio.h>
int main()
{
int i;
for (i = 0; i <= 255; i++) /*ASCII values ranges from 0-255*/
{
printf("ASCII value of character %c = %d\n", i, i);
}
return 0;
}
Could you please advise how is this possible since there are characters inside ASCII?
Every variable in the computer is stored as binary and its value will depend on the type you define. For example, in memory store the value 1010. If you cast it as int, it will print -6, if you cast it as unsigned int, it will print 10. Same for int and char. It depends on you
printf() - Print formatted
we use format specifiers to specify the format of given variable,
%d format specifier to display the value of an integer variable.
%c is used to display character
as ASCII values ranges from 0-256 , %c will print the respective character symbol of number in Integer i
When you declare two variables char a,b; and then you use first 'a' and then 'b',it prints only b, but if you declare it 'b' then 'a', it has no problem printing both in ASCII,the point of the program is to read 121 and 120 and to print yx. the problem - https://prnt.sc/pr5nww
and if you swap them -https://prnt.sc/pr5mt5
#include <stdio.h>
#include <stdlib.h>
int main(){
char a,b;
scanf("%d",&a);
scanf("%d",&b);
printf("%c",a);
printf("%c",b);
}
This is kind of a confusing situation. When it comes to mixing char and int values (as you might do when investigating the numeric values of characters in a character set), it turns out the rules for scanf and printf are almost completely different.
First let's look at the scanf lines:
char a,b;
scanf("%d",&a);
scanf("%d",&b);
This is, in a word, wrong. The %d format in scanf is for scanning int values only. You cannot use %d to input a value of type char. If you want to input a character, the format for that is %c (although it'll input it as a character, not a number).
So you'd need to change this to
char a,b;
scanf("%c",&a);
scanf("%c",&b);
Now you can type characters like A and $ and 3 and have them read into your char variables a and b. (Actually, you're going to have additional problems if you hit the Return key between typing the characters for a and b, but that's a different story.)
When it comes to printing the characters out, you have a little more freedom. Your lines
printf("%c",a);
printf("%c",b);
are fine. And if you wanted to see the integer character-set values associated with the characters, you could have typed
printf("%d",a);
printf("%d",b);
and that would have worked, too. This is because when you call printf (and other functions ike it), there are some automatic conversions that take place: types char and short int are automatically promoted to (passed as) int, and type float is promoted to double. But these automatic conversions happen only for values of those types (as when calling printf). There a=is no such conversion when you're passing pointers to these types, as when calling scanf.
What if you wanted to read numbers, not characters? That is, what if you wanted to input the number 65 and see it get printed as capital A? There are several possible ways to do that.
The first way would be to continue to use %d in your scanf call, but change the type of your variables to int:
int a,b;
scanf("%d",&a);
scanf("%d",&b);
Now you can print a and b out using either %c or %d, and it'll work fine.
You could also use a temporary int variable, before reassigning to char, like this:
char a,b;
int tmp
scanf("%d",&tmp);
a = tmp;
scanf("%d",&tmp);
b = tmp;
The final, lesser-known and somewhat more obscure way, is to use the h modifier. If you say
char a,b;
scanf("%hhd",&a);
scanf("%hhd",&b);
now you're telling scanf, "I want to read decimal digits, but the target variable is a char, not an int."
And, again, you can print a and b out using either %c or %d, and it'll work fine.
the point of the program is to read 121 and 120 and to print yx
Do
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char a, b;
/* Scan into the half of the half of an int (the leading blank
makes scanf() eat whitespaces): */
scanf(" %hhd", &a);
scanf(" %hhd", &b);
/* Print the half of the half of an int: */
printf("%hhd", a);
printf("%hhd", b);
}
To print the characters literally do the printing part like this:
...
printf("%c", a);
printf("%c", b);
}
I'm trying to print out the digits of an integer using the char format specifier.
This is what I currently have
//counter = arbitrary int
while (counter > 0) {
char c = (char)(counter % 10);
printf("%c", c);
counter/=10;
}
This just prints out blank but if I change the format specifier to int but leave the type cast, it'll print the correct value. Something like this
char c = (char)(counter % 10);
printf("%d", c);
Any ideas why this isn't printing when I use the char format specifier?
%c is for printing a character, not a number. If you want to print a number, you should use %d. The reason it works for char is because char is implicitly promoted to int when you pass it as an argument to a variadic function.
If c contained 'A', and you used %c to format it, you would get the letter ‘A’, but if you used %d, you would get its integer value, e.g. 65.
If you absolutely must use %c, you can add the value of '0' to the character before printing, e.g.L
printf("%c", c + '0');
%c can be used in this context as printf("%c",c+'0') where +'0' will turn it into the integer equivalent of 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.
#include <stdio.h>
int main ( ) {
int n;
n = 0;
printf ("%c\n", n);
return 0;
}
so that's my code, but when i print it, it just prints a blank space. Shouldn't it print 0?
Change %c by %d:
printf ("%d\n", n);
n is integer type so try to print it as..
printf("%d\n",n);
and if u use %c then its printing first byte's related char in ASCII table .
If you need to display 0 without changing the printf statement, the way to do it is to do
n = '0';
or
n = 48;
Take a look at Ascii Table, to see the ascii value of 0.
Printf Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers.
Here you are Using wrong format specifier to print your value. %c used for printing variables of type char(character) . Use %d for int(integer).
Try this Link