Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicit type conversion in c language

Tags:

c

if my c code is

#include<stdio.h>
int main()
{
    int c;
    while((c=getchar())!=EOF)
    {
       putchar(c);
    }
getch();
return 0;
}

I want to know whether int c woud be converted into char , or the input character would be converted into int and then be stored in c.

like image 470
Bhavdeep Singh Matharu BTech Avatar asked Sep 14 '26 15:09

Bhavdeep Singh Matharu BTech


2 Answers

getchar returns an int and putchar expects an int so there is no need of conversion. char in name may be confusing for you. But type int also allows to accommodate some special values for ex: EOF

like image 159
Mohit Jain Avatar answered Sep 17 '26 06:09

Mohit Jain


From the man page of getchar()

int getchar(void);

So, why'd think of conversion, anyway? The return type of getchar() is int, being stored into an int variable.

Same goes for putchar() also.

int putchar(int c);
like image 26
Sourav Ghosh Avatar answered Sep 17 '26 07:09

Sourav Ghosh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!