printf() with %c format code not printing what I expect - c

#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

Related

Why do we get the ASCII characters with int i within %c?

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

Why are the outputs of this C code different when formatting with %c and %d

Take a look at the following piece of code:
#include <stdio.h>
int main()
{
int i;
for (i = 48; i < 58; i++)
{
printf("%d", i);
}
return 0;
}
The output is: 48495051525354555657
But if you change printf("%d", i); to printf("%c", i); the output becomes: 0123456789
Shouldn't both output 48495051525354555657 Why are the outputs different when substituting d with c?
Because with %c the i is interpreted as a character and if you look at the ASCII table you can see that 48 represents the character '0', 49 is '1', etc.
%d prints out a number and %c a character. The ASCII codes for 0-9 are 48-57, so it prints those numbers as characters.
In C when you want to print something you need to provide format specifier. Then the compiler will print the things accordingly.
According to your question %d specifier is for printing number and %c is for printing character.
So when you tried printing integer value using %c format specifier then the system is converting 48-57 in char type and that is '0'-'9'. And if you go further it will print
: ; < = > ? # A B C and so on.
And this is because system follows ASCII.
%d is format specifier for signed integers whereas %c is format specifier for charecters .
let i=48 when %d is used it prints the integer (prints 48).
but when %c is used the 48 is taken as ASCII value and 0 is printed for 48
ascii values
0 = 48
1 = 49
.
.
.
soon
hope you understand press the upvote. :) ;)

C int to char not printing out using char format

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.

can we use printf statement inside printf() function in C language

is the following statement correct in C?
printf(" the string is %s", printf("xyz"));
I mean, is it possible to use 1 printf statement inside printf function?
if possible then please send the proper syntax for that expression.
printf returns the number of characters printed, so the above statement would be wrong as your formatstring tries to print a string.
You could do it like this though:
printf("characters printed: %d", printf("xyz"));
If you want to output the result of a formatted string, you would have to use snprintf first, printing it to an array, which in turn you can use to print as a string then.
Example:
char s[100];
snprint(s, sizeof(s), "My string %d", 3);
printf(" the string is %s", s);
What printf does is just push characters to the buffered output stream. print returns:
On success, the total number of characters written is returned.
If a writing error occurs, the error indicator (ferror) is set and a negative number is returned.
If a multibyte character encoding error occurs while writing wide characters, errno is set to EILSEQ and a negative number is returned.
so if you are looking to print any of those out, you can with that second printf. But it does not return a string, those characters are in the buffered output stream until one of these happen: program's termination, a '\n' is encountered, buffer is full, or a command like ffslush() is called.
This statement is not correct but, you can use printf within printf.
Upon successful return, printf functions return the number of characters printed (not including the trailing '\0' used to end output to strings).
you can use like:
printf(" the string is %d", printf("xyz"));
Syntax :-
printf ("Your String Length :: %d",printf("TestString"));
1) First it will execute inner printf
2) inner printf return with the string length.
3) that is used in outer printf with %d specifier.
It's pretty simple.
First printf() represent the main value, then rest of printf() count the number of print value.
Example:
#include <stdio.h>
#include <conio.h>
void main()
{
int i = 43;
printf("%d", printf("%d", printf("%d", printf("%d", i))));
getch();
}

Explain the output of c Program

#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).

Resources