英文:
Print to stdout without `printf`, `puts`, `putchar`, etc
问题
#include <stdio.h>
int _putchar(char c)
{
return 0;
}
int main()
{
_putchar('_');
_putchar('p');
_putchar('u');
_putchar('t');
_putchar('c');
_putchar('c');
_putchar('h');
_putchar('a');
_putchar('r');
_putchar('\n');
return(0);
}
Expected output is:
_putchar
英文:
Please, how can I make my main()
function print out the inputs, by calling a function named _putchar()
?
We are not allowed to use built-in functions like printf()
, puts()
, putchar()
, etc.
The instructions are as follows:
> - You are not allowed to use the standard library. Any use of functions like printf, puts, etc… is forbidden
>
> - You are allowed to use _putchar
Apparently, we are only allowed to create our own function, which can then be called to our main output.
Here is my code below:
#include <stdio.h>
int _putchar(char c)
{
return 0;
}
int main()
{
_putchar('_');
_putchar('p');
_putchar('u');
_putchar('t');
_putchar('c');
_putchar('c');
_putchar('h');
_putchar('a');
_putchar('r');
_putchar('\n');
return(0);
}
Expected output is:
> _putchar
答案1
得分: 2
你可能需要使用类似于POSIX系统上的write
系统调用或其他平台上类似的方法来实现_putchar
:
#include <unistd.h>
int _putchar(char c)
{
return write(1, &c, 1) == 1 ? c : -1;
}
英文:
You might be expected to implement _putchar
using a system call such as write
on POSIX systems or something similar on other platforms:
#include <unistd.h>
int _putchar(char c)
{
return write(1, &c, 1) == 1 ? c : -1;
}
答案2
得分: 1
以下是您的答案:
#include <stdio.h>
int _putchar(char c)
{
int n = 0;
n = write(1, &c, 1);
return n;
}
int main()
{
int m = 0;
m = _putchar('_');
m = _putchar('p');
m = _putchar('u');
m = _putchar('t');
m = _putchar('c');
m = _putchar('c');
m = _putchar('h');
m = _putchar('a');
m = _putchar('r');
m = _putchar('\n');
return 0;
}
英文:
Here is my answer:
#include <stdio.h>
int _putchar(char c)
{
int n = 0;
n = write(1,&c,1);
return n;
}
int main()
{
int m = 0;
m = _putchar('_');
m = _putchar('p');
m = _putchar('u');
m = _putchar('t');
m = _putchar('c');
m = _putchar('c');
m = _putchar('h');
m = _putchar('a');
m = _putchar('r');
m = _putchar('\n');
return(0);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论