PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 30, 2022

[FIXED] What does putc() return in c?

 October 30, 2022     ascii, c, eof, non-ascii-characters, stdio     No comments   

Issue

I was willing to know what does putc() return. I was going through the following link:https://www.tutorialspoint.com/c_standard_library/c_function_putc.htm. Now if putc() returns EOF on error, then a natural question is what does it return on end of file? for that I wrote the following in dev c++:

Code

#include <stdio.h>

int main() {
    char ch = EOF;
    printf("%d\n", putc(ch, stdout));
    printf("hello %d", EOF);

    return 0;
} 

Output

255
hello -1

This is a little bit weird. Can anyone help me out ? EOF is not an ASCII character as stated in What is the ascii value of EOF in c.? then why 255 (yes its not ASCII) in the first line and -1 in second?


Solution

EOF is -1 as you already have probably already found out. So if you putc(EOF,stdout) then you see 255 in the output because -1 is converted to unsigned char before printing.



Answered By - vasek
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing