Anasayfa » C / C++

C / C++ Courses ? 4 (Execution & Input)

26 Temmuz 2010 2.059 views 2 Yorum

Let’s start!

To write and execute a program:

  1. Edit : writes codes of the program on a suitable platform
  2. It creates a file which has “cpp” extension. For example: “First.cpp”

  3. Preprocess : adds header files and then checks for errors before compiling
  4. Compile :
    • checks for syntax errors
    • converts program code into object code(machine language code)

    At this point, “First.cpp” becomes “First.obj”

  5. Link : makes object code and files in preprocessing work together
  6. Load : loads program into memory (CPU will take instructions)
  7. Execute : runs every line in memory, but this part is under the control of CPU

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 🙂

2 Yorum »

Yoruma Açığız! :)

Be nice. İçeriği çirkin olmadığı sürece her yoruma, öneriye ve soruya "evet" :)

Etiketler: , , , , , , , ,