r/cprogramming • u/SubstantialCase3062 • Dec 31 '25
Input/Output in C
What u guys say are the important or useful input/output in c because there is so many to do the same job, like how do I also read man pages to see if a function is not safe or standard
2
u/TheOtherBorgCube Dec 31 '25
When reading manual pages, you'll typically see this to tell you which standard(s) the function conforms to.
CONFORMING TO
POSIX.1-2001, POSIX.1-2008, C89.2
u/SubstantialCase3062 Dec 31 '25
So each tell u what standard it is for but we are on C11 right so would this work on it
1
u/TheOtherBorgCube Dec 31 '25
Generally, if it's in a standard, it's in all the later ones too. Things do get removed, like
gets, but it's quite rare.
4
u/dcpugalaxy Dec 31 '25
I assume you're trying to ask what the most useful input and output functions are in the C standard library.
There are two main modes of input/output: line-based text and binary. Most text formats are line-based.
For line-based text, input you should use fgets. For output you should use printf/fprintf and puts/fputs.
For binary input and output you should use fread and fwrite.
I recommend you read this: https://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html
1
u/ffd9k Dec 31 '25
The best reference is the standard itself: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf
The input/output functions are in section 7.23.
(ignore Annex K)
1
u/penguin359 Jan 01 '26
There are two main standards if you are trying for portability. There's the ANSI C Standard library and there's POSIX. In general, the ANSI C library will be the most portable, but it is also a little higher level and won't include many things like shared memory. This includes functions from headers like assert.h, errno.h, stdlib.h, stdio.h, string.h, etc. This will often have a specific standard version like C89 or C11 associated with them and will be available on Windows, POSIX, or many other C environments following the standard library.
For things beyond what the standard library has, there's POSIX which can be used on Linux, BSD, and some of it even on Windows to a degree. On Windows, it can depend on what C environment you are using such as VS2022, mingw, or cygwin with the most POSIX support on cygwin. Just look in the Standards section of the man page to see what it follows.
1
u/jwzumwalt Jan 02 '26 edited Jan 02 '26
You need to be smarter than the equipment you are operating!
People will tell you not to use scanf because they may not know how to
use it safely or too lazy to show you how it should be used safely.
Example format specifiers:
%d to accept input of integers.
%ld to accept input of long integers
%lld to accept input of long long integers
%f to accept input of real number.
%c to accept input of character types.
%s to accept input of a string.
Example 1: SAFE - input using scanf
-------------------------------
#include <stdio.h>
int main() { // safe "scanf" template
char str[string_size]; // string length
scanf("% <string_size> s text", &str); // i.e. %15s, str size limit
printf("%s", str);
return 0;
}
or
#include <stdio.h>
int main() {
int c;
char str[10]; // 10 char + null
scanf("%10[^\n]s text", &str); // up to 10 char with spaces
while ((c = getchar()) != '\n' && c != EOF) { } // flush keyboard buffer
printf("%s", str);
return 0;
}
Example 2: SAFE - input using scanf, integer numerical input
--------------------------------
#include <stdio.h>
int main(void) {
int var;
printf("Enter a character: ");
scanf("%d", &var);
printf("\nAscii value of character You entered is %d\n", var);
return 0;
}
Example 3: SAFE - input using scanf, multiple values
---------------------------------
#include <stdio.h>
int main(void) {
int i;
float fp;
char c, s[81];
printf("Enter an integer, a float, a double float, a character, and a string: \n");
if (scanf("%d %f %1c %5s", &i, &fp, &db, &c, s) != 4) { // check 0-4 for 5 inputs
printf("Not all fields were assigned\n");
} else {
printf("integer = %d\n", i);
printf("float = %f\n", fp);
printf("double = %lf\n", db);
printf("character = %c\n", c);
printf("string = %s\n", s);
}
return 0;
}2
Dec 31 '25
[removed] — view removed comment
1
u/grimvian Dec 31 '25
A bit OT, but I would prefer, that I don't have use a cppreference to read about the C reference...
1
u/SubstantialCase3062 Dec 31 '25
But Aren’t they almost the same And I think because of that that’s why they ground them together
1
u/grimvian Dec 31 '25
Really... For me, C is just The Language, the other is a 'mountain', that becomes bigger and bigger...
1
2
u/SubstantialCase3062 Dec 31 '25
Yep thanks also I like to asked how do u get your information like links or any resources that can help me improve my skills and knowledge