cstdio 2018-03-06 Using printf() & scanf() in C/C++ code is not only faster than using cin & cout, but it's also way simpler and cleaner. To use scanf/printf include stdio.h: #include <stdio.h> or cstdio in C++: #include <cstdio> For cin & cout it's iostream: #include <iostream> cin inputs whatever the fuck you want into a variable. There's no difference in syntax, whether your input is a string or a single integer. That means at the first glance you won't know what to expect from the input. Syntax: cin >> [variable] >> [another variable] >> [etc.]; That way you can get the input into 3 (or more) variables. For instance, this might input 3 different numbers into 3 different vars, but might as well get you a number, a string, and a char. cout works similarly, it spits out your variables to the output. To input more variables (of different types?), you need to add even more "<<". AFAIK that is necesarry. So, if you want to output a few numbers, each separated with a few characters, you need to add "<<" in between every single part of your call. Syntax: cout << [vars] << "or text\n " << [more vars] << [etc.]; Notice, that the first variable won't be spaced out from the string that comes after it. On the output you might get for example "1321or text"... To clear out: '\n' means newline. Escaped characters work both with couts and printfs. Some others are '\t' for tab and '\a' for a beep sound. Now to the printf(), as it's simpler to output, than to input. printf() takes a string(char table in C) and parses it, replacing special character sequences with variables, which you pass to the function as later arguments. This might seem confusing for you C++ monkeys, but it's really not. Syntax looks like this: printf("yo %[type code], simple, ain't it? %[same] %[etc.]\n", [var1], [var2], [etc.]); The string has to be there, or else you'll get a compilation error. By the [type code] I mean one of these sweeties below: %d - int (same as %i) %ld - long int (%li) %lld - long long %f - float %lf - double %c - char %s - string %x - hexadecimal As you've probably already figured out, you need to supply which type you want to output on which place. This %something is a placeholder for the value of our variable. Such approach has a number of benefits. Firstable, you know what type you're outputting/inputing on the first glance. Secondably - it's much simpler to space out variables with whatever you like. Mixing and formatting output is just way easier with printf. scanf() works similarly, but there's a small difference. You shouldn't supply your variables straight to the function, that won't tell it anything. You pass the space where you want your variable to be saved, so an address of a variable, a POINTER. Simple as that. If you don't know A N Y T H I N G about pointers and memory addresses, just remember that with every type, except for tables (particularly char tables/strings), you need to preceed your variable name with a single '&' character. This will pass the address instead of the value further to the function. The syntax is as folows: scanf(" %s %[?] %[?]", [stringname], &[var1], &[var2]); passed string typically consists just of spaceholders and spaces. By a space, scanf means every white character. In this case, it'll ignore every '\n' and ' ' preceeding our string. To summarize: scanf("%s", stringname); scanf("%?", &varname); printf("%?", varname); That's basically it. An example:
#include <stdio.h>
int main(void)
{
	int n;
	scanf("%d", &n);
	int tab[n];
	for(int i = 0; i < n; i++)
		scanf("%d", &tab[i]);
	
	for(int i = 0; i < n-1; i++)
	{
		printf("%d, ", tab[i]);
		if(i%5 == 4) printf("\n");
	}
	printf("%d.\n\tAnd that's it.\n", tab[n-1]);
	return 0;
}
BONUS: to get higher i/o speed with cin/cout use following lines in your c++ main function: std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0);