C / C++ Courses ? 4 (Execution & Input)
Let’s start!
To write and execute a program:
- Edit : writes codes of the program on a suitable platform
- Preprocess : adds header files and then checks for errors before compiling
- Compile :
- checks for syntax errors
- converts program code into object code(machine language code)
At this point, “First.cpp” becomes “First.obj”
- Link : makes object code and files in preprocessing work together
- Load : loads program into memory (CPU will take instructions)
- Execute : runs every line in memory, but this part is under the control of CPU
It creates a file which has “cpp” extension. For example: “First.cpp”
These brief descriptions are enough for us.
Input:
We use scanf function to allow the user to enter any information. Scanf fuction exists inside “stdio.h”
Example: Write a program that allows the user to enter an integer
#include<stdio.h>
int main(){
int num;
printf("Enter an integer \n");
scanf( "%d", &num ); // we use conversion specifiers also for input in scanf functions
printf("The number you entered is %d", num);
return 0;
}
Output will be like that:
Enter an integer
(It will wait until we enter an integer. Let’s enter 19. )
19
The number you entered is 19
As you can see in the example, we use scanf functions with conversion specifiers. But there is difference in usage of them between printf and scanf. For input, we put “&” sign to write a value in a variable.Don’t forget to put it.
And also, if we write scanf( “%d”, &num); num should be an integer, and we should enter an integer value.
And the other difference between printf and scanf is this. We can write anything that we want in printf statements, but for scanf statements, we should write only conversion specifiers in it. Not anything else.
Entering two values at the same time has no difference.
~
Example: Write a program that allows the user to enter his/her birthyear and current year.Then write informations and his/her age.
#include<stdio.h>
int main(){
int byear;
int curyear;
printf( "Enter your birthyear and current year\n" );
scanf( "%d%d" ,&year, &curyear);
printf ( "You are %d years old." ,curyear-year);
return 0;
}
Output is like that:
Enter your birthyear and current year
1990
2010
You are 20 years old.
As you can see, we can use mathematical operations if they have the same type.
»Extra:What happens if we write like them:
float x=3,5;
int y;
y=x;
printf( “%d”, y );
int x=6;
float y;
y=x;
printf( “%f” ,y);
Answer is in the following lesson😉
?Answer of previous lesson’s extra:
It will give any integer randomly if we forget to write what it is.
Take care 🙂
[…] Answer is in the following lesson […]
[…] ?Answer of previous lesson’s extra: […]
Yoruma Açığız! :)
Kategoriler
Son Yorumlar
Arşivler
Bulutumdan Seç Beğen Al :)
bilgisayar mühendisliği oryantasyon bilm 100 hakkında c/c++ ders notları c/c++ training courses can yücel sözleri şiirleri can yücel şiirleri c courses c dersleri Ceng 100 / Bilm 100 Courses c hakkında örnekler program yazma c programlama dersleri c programming courses examples güzel sözler Güzel Yazılar Hz.Mevlana Mevlana sözleri yusuf ile züleyha sözleri Ziynet'in kaleminden yazılar Ziynet Nesibe Sözleri züleyhadan yusufa İskender Pala sözleri
WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.
En Çok Görüntülenenler