Standart inputs and outputs
Hi everybody,
After a long interval i decided to continue the articles about C(due to the lack of jobs, etc). Anyway, I want to talk about the standart input output functions that we can use in C.
First of all, let's remember the first example at the very beginnig of this serie.
#include <stdio.h>int main(){ printf("Hello World!"); return 0;
}
As you see, we have something that related to our topic today. Yes the "include" part. Basicly, this helps you to include some libraries to your program in order to use some functions or methods with your program. We included stdio.h library to our program. This library has all the standart inputs and outputs that we will use(you can understand that from its name actually, std means standart and io means input&output).
In the main function, we used another function ("printf") that prints "Hello World!"(without quotation). This function is the one of the standart output functions that stdio.h includes. It helps us to print data in formatted way. To see how we can use this function, let's look at the example below.
#include <stdio.h>
int main(){ printf("This is a string for test purpose\n"); int numb= 10; char ch='A'; float pi=3.14; double dec = 10.987654321; printf("I have %d fingers\n",numb); printf("My grade was %c\n", ch); printf("The PI number for circular shapes is %f\n",pi); printf("The decimal number that comes to my mind right now is %lf\n",dec); printf("You can use multiple variables in this function like %d and %c\n"); return 0;
}
and the output will be like:
I have 10 fingers My grade was A The PI number for circular shapes is 3.14 The decimal number that comes to my mind right now is 10.987654 You can use multiple variables in this function like 10 and A
Different operators are used for each type of variable. I think i made my point about them and you can observe and understand which operator to use for which variable from example. If you wonder about other operators or other ways to present operators you can check the table 7.1 on http://www.cs.duke.edu/~raw/cps149s/C%20Input%20Output.htm .
Yes, printf is a function for printing. What else... there's a putchar. putchar() prints the value of a character.
Example:
#include <stdio.h>
int main()
{char ch = 'A';
putchar(ch); //it will print just an A return 0;}
stdio.h includes fputc, putc and some other output functions. We will get to them when we are dealing with files.
Standart Inputs
First function we will cover is scanf. Yes it is scan function. It scans from keyboard, nothing fancy.
After you type the value you need to press enter and let the program roll.
Example:
#include <stdio.h>
int main()
{int age;
char first_letter_of_your_name;
float height;
double weight;
scanf("%d",&age); scanf("%c",&first_letter_of_your_name); scanf("%f",&height); scanf("%lf",&weight);return 0;
}
It's very similar to printf(by writing) but we need to put the address operator "&" before the variable. We will talk about it when we are dealing with functions & pointers.
getchar() is the last input function of this day. This function reads one character from keyboard and returns an integer. You should press enter after typing char and let the program roll.
I hope you enjoy the article(how could it be possible). There are 2 functions that we didnot cover, puts and gets. We will be dealing with them in strings section.
