dev c++, c programme, every character is displaced by a square - c

#include<stdio.h>
int main()
{
char c;
while((c=getchar()!=EOF))
{
putchar(c);//
}
}
Every character of the output is displaced by '' these sorts of things.
enter image description here

Your parentheses are wrong so c gets assigned the value of the condition, which is 0 or 1 (false or true).
What you have now is the same as c = (getchar() != EOF) because of operator precedence.
Also, use the correct type for c, which is int:
#include<stdio.h>
int main()
{
int c;
while( (c = getchar()) != EOF )
{
putchar(c);//
}
}

Related

if/else not receiving value at array in C [duplicate]

#include<stdio.h>
#include<ctype.h>
int peekchar() {
int c;
c = getchar();
if (c != EOF) {
ungetc(c, stdin);
}
return c;
}
int readNumber(void) {
int c;
int accumulator = 0;
while ((c = peekchar() != EOF) && isdigit(c)) {
c = getchar();
accumulator *= 10;
accumulator += c - '0';
}
return accumulator;
}
int main() {
int result = readNumber();
printf("%d\n", result);
return 0;
}
I am trying to read an integer written in decimal notation from stdin until the first non-digit. But its not giving the correct result:
M1508444:CProg sb054043$ gcc -g3 readNumber.c -o readNumber
M1508444:CProg sb054043$ ./readNumber
123
0
Can someone please help me identify the problem?
The issue is with operator precedence. c = peekchar() != EOF is grouped as c = (peekchar() != EOF), and so c is either 0 or 1, which accounts for the result.
Fix with (c = peekchar()) != EOF.
Or, given that isdigit is defined to be 0 for EOF, your loop conditional can be simplified to
while (isdigit(c = peekchar())){
Hi you need to modify your while loop like below:-
while ( (c = peekchar()) != EOF && isdigit(c)) {
c = getchar();
accumulator *= 10;
accumulator += c - '0';
}
First of all you need to read the value and store it in variable c and that you can achieve by doing (c = peekchar()). Once the value stored in c now your while loop will first check whether it is EOF if not then only it will check whether it is a digit or not.

Function of (char)getchar() in C programming

My friend asked me what is (char)getchar() which he found in some online code and I googled and found 0 results of it being used, I thought the regular usage is just ch = getchar(). This is the code he found, Can anyone explain what is this function?
else if (input == 2)
{
if (notes_counter != LIST_SIZE)
{
printf("Enter header: ");
getchar();
char c = (char)getchar();
int tmp_count = 0;
while (c != '\n' && tmp_count < HEADER)
{
note[notes_counter].header[tmp_count++] = c;
c = (char)getchar();
}
note[notes_counter].header[tmp_count] = '\0';
printf("Enter content: ");
c = (char)getchar();
tmp_count = 0;
while (c != '\n' && tmp_count < CONTENT)
{
note[notes_counter].content[tmp_count++] = c;
c = (char)getchar();
}
note[notes_counter].content[tmp_count] = '\0';
printf("\n");
notes_counter++;
}
}
(char)getchar() is a mistake. Never use it.
getchar returns an int that is either an unsigned char value of a character that was read or is the value of EOF, which is negative. If you convert it to char, you lose the distinction between EOF and some character that maps to the same char value.
The result of getchar should always be assigned to an int object, not a char object, so that these values are preserved, and the result should be tested to see if it is EOF before the program assumes a character has been read. Since the program uses c to store the result of getchar, c should be declared as int c, not char c.
It is possible a compiler issued a warning for c = getchar(); because that assignment implicitly converts an int to a char, which can lose information as mentioned above. (This warning is not always issued by a compiler; it may depend on warning switches used.) The correct solution for that warning is to change c to an int, not to insert a cast to char.
About the conversion: The C standard allows char to be either signed or unsigned. If it is unsigned, then (char) getchar() will convert an EOF returned by getchar() to some non-negative value, which will be the same value as one of the character values. If it is signed, then (char) getchar() will convert some of the unsigned char character values to char in an implementation-defined way, and some of those conversions may produce the same value as EOF.
The code is a typical example of incorrect usage of the getchar() function.
getchar(), and more generally getc(fp) and fgetc(fp) return a byte from the stream as a positive value between 0 and UCHAR_MAX or the special negative value EOF upon error or end of file.
Storing this value into a variable of type char loses information. It makes testing for EOF
unreliable if type char is signed: if EOF has the value (-1) it cannot be distinguished from a valid byte value 255, which most likely gets converted to -1 when stored to a char variable on CPUs with 8-bit bytes
impossible on architectures where type char is unsigned by default, on which all char values are different from EOF.
In this program, the variables receiving the getchar() return value should have type int.
Note also that EOF is not tested in the code fragment, causing invalid input strings such as long sequences of ÿÿÿÿÿÿÿÿ at end of file.
Here is a modified version:
else if (input == 2)
{
if (notes_counter != LIST_SIZE)
{
int c;
// consume the rest of the input line left pending by `scanf()`
// this should be performed earlier in the function
while ((c = getchar()) != EOF && c != '\n')
continue;
printf("Enter header: ");
int tmp_count = 0;
while ((c = getchar()) != EOF && c != '\n') {
if (tmp_count + 1 < HEADER)
note[notes_counter].header[tmp_count++] = c;
}
note[notes_counter].header[tmp_count] = '\0';
printf("Enter content: ");
tmp_count = 0;
while ((c = getchar()) != EOF && c != '\n')
if (tmp_count + 1 < CONTENT)
note[notes_counter].content[tmp_count++] = c;
}
note[notes_counter].content[tmp_count] = '\0';
printf("\n");
notes_counter++;
}
}

Why having getchar() assigned to a variable in a while loop condition leads to output of 0?

So currently I'm learning C from the C programming language 2nd edition book and it says that:
while (c = getchar() != EOF) {
}
is identical to:
while (c != EOF) {
c = getchar();
}
However, when I run the code I just had written:
#include <stdio.h>
int main() {
char c;
int times;
while (c = getchar() != EOF) {
if (c == 'a') {
++times;
}
}
printf("%d\n", times);
}
The value of times it outputs is 0 instead of actual value of times I typed in 'a' character.
Now in this code, it works fine:
#include <stdio.h>
int main() {
char c;
int times;
while (c != EOF) {
c = getchar();
if (c == 'a') {
++times;
}
}
printf("%d\n",times);
}
and if I type a 3 times, the value it outputs is 3.
Precedence!
c = getchar() != EOF
means
c = ( getchar() != EOF ) // Assigns `0` or `1` to `c`.
but you want
( c = getchar() ) != EOF
Also note that c needs to be an int.
This means you could use
for (int c; ( c = getchar() ) != EOF; ) {
...
}
but I prefer
while (1) {
int c = getchar();
if (c == EOF)
break;
...
}
Operator precedence.
!= operator is higher in precedence than =, so your expression is equivalent to:
while (c = (getchar() != EOF))
use a parenthesis around c=getchar() and it will work.

C Program to read number character by character giving incorrect result

#include<stdio.h>
#include<ctype.h>
int peekchar() {
int c;
c = getchar();
if (c != EOF) {
ungetc(c, stdin);
}
return c;
}
int readNumber(void) {
int c;
int accumulator = 0;
while ((c = peekchar() != EOF) && isdigit(c)) {
c = getchar();
accumulator *= 10;
accumulator += c - '0';
}
return accumulator;
}
int main() {
int result = readNumber();
printf("%d\n", result);
return 0;
}
I am trying to read an integer written in decimal notation from stdin until the first non-digit. But its not giving the correct result:
M1508444:CProg sb054043$ gcc -g3 readNumber.c -o readNumber
M1508444:CProg sb054043$ ./readNumber
123
0
Can someone please help me identify the problem?
The issue is with operator precedence. c = peekchar() != EOF is grouped as c = (peekchar() != EOF), and so c is either 0 or 1, which accounts for the result.
Fix with (c = peekchar()) != EOF.
Or, given that isdigit is defined to be 0 for EOF, your loop conditional can be simplified to
while (isdigit(c = peekchar())){
Hi you need to modify your while loop like below:-
while ( (c = peekchar()) != EOF && isdigit(c)) {
c = getchar();
accumulator *= 10;
accumulator += c - '0';
}
First of all you need to read the value and store it in variable c and that you can achieve by doing (c = peekchar()). Once the value stored in c now your while loop will first check whether it is EOF if not then only it will check whether it is a digit or not.

C getchar() misunderstanding

Can someone explain me the code ?? Wouldn't d be always equal to c ? I guess I don't really get this getchar() function.Why isn't d always equal to `c ?
#include<stdio.h>
void test(int c);
int main(void) {
int c;
while ((c = getchar()) != EOF) {
test(c);
}
return 0;
}
void test(int c) {
int d;
if (c == '/') {
d = getchar();
printf("%c", d);
}
}
Input:
/*
Output:
*
No, not really. As mentioned in C11, chapter §7.21.7.6, The getchar function, (emphasis mine)
The getchar function returns the next character from the input stream pointed to by
stdin. [...]
So, each call to getchar() will give you the next character input present in the input stream. So, when c == '/' condition is met, it will read the next entry and store into d, it need not be the same as c, anyway.

Resources