SITE SCIENTIFIQUE D'AZROU

Computer science

C/C++

lesson two.


In 1st lesson, we learnt how to display message in screen. Displaying message in screen, is sending informations to screen memory.
Now we we'll try to get informations from the keyboard, so we must use variables. Look at thiere types:


To enter data from the keyboard, we'll use the " scanf " function, it is in "stdio.h" and its prototype is :
Scanf("%k",&data);
"%k" : is the data type, k is :


&data is the memory address where we will store the data. while data alone is what is in data.
Example :
Input an integer from the keyboard:


If you enter an integer it's OK, but if you enter a real, the computer will save what is before the comma in "n" data, and it will store it in memory. For example n=3.12 , it will strore 3 in "n".
Run this code:


The computer will ask you to enterdata, the first is for integer, The second is for a real data and the third is for a string of char(character), then it store them in memory . The integer is stored in &a address, the real in &b address and the char in &d address.
Now how we'll display what is in memory?
With " printf " instruction, this instruction display messages and data.
Example:


First we declared "t" as a flaot, then we cleared the screen and we displayed a message to tell us that he wait for a real number. After that the computer store the data entered at the &t address, and it cleared screen again. Then it displayed: "The real stored in memory is:" followed by the (t) value (it's what is in memory). This value was displayed in the some place we put the char "%f".
Please run this program:


You see that the value of t was displayed between real and stored.
Now we'll see what will be Happened when we run this code:


After running we'll enter for example the value 4.02, the computer store it in memory and display it again, then we'll enter the value 2.1, the computer will store it at the some place in memory where it stored the 4.02. So the first value is lost from the memory, just the last one be saved .
We can declare many variables in the some line and display them in the some message.
The code:


The variables order is important, if we write :
printf(" the three integers are : a=%,b=%,c=%",b,a,c) ;
we'll see that the a value is the b one, and the b's value is the value of a.
The last program can be written like this:


NB: after you enter data, you must press enter.

http://Azrousoft.8m.com