• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

C/C++ Nikkster's C/C++ Programming School Lesson 2

Nikkster

Programmer
Joined
May 9, 2008
Messages
2,848
Reaction score
9
Location
Confidential
Welcome back Folks, hope you enjoyed my previous lesson.


Today I'm going to give you a little closer look about why variables are that good to know and why it can be quite good to know sometimes.

This lesson is mostly C.

Here is the code I'm going to explain for you guys about and what you need to think of in this lesson).

This is an example of a run competition:
Code:
#include <stdio.h>
int main()
}
      float time1, time2;
   printf('Time in the first round? ');
   scanf('%f", &time1);
   printf("Time in the second round");
   scanf("%f", &time2);
   printf("Total time: %f\n", time1+time2);
   printf("average time: %f\n", (time1+time2) /2);
When you compile the code and run the program, it can look like this:
Time in the first round? 54.25
Time in the second round? 55.75
Total time: 110.000 000
Average time: 55.000000

First of all, to make things a little bit easier for you and prevent you from being confused when coding, it might be good to have a clue about how you can make comments in your code (that will have no effect at all on the program, just that you do it in the right way).

This symbol is an example of making comments in your program:
/* My first comment, it rocks */

Nothing hard at all, just that you remember that the slash is before the "star-dot" and when you end the comment, you always end with a "star-dot" and then a slash. However, in c++, it works by doing 2 slashes.

I want to remind you that variables are quite important, and is one of the most basic thing you learn in c/c++ except the printf command we have learnt in my previous lesson.

Now let me explain this for you, in detail..

The
Code:
float time1, time2,
is being declared in two variables and it gives you the name time1 and time2. All variables you use NEEDS to be declared. The declaration is being written before the executable commands, this is one thing you need to remember. The command float is telling you that the variables time1 and time2 has the type "float, which means that they are supposed to store numbers. Why we have changed the float to bold font, is because it's a keyword, a keyword that has a important role.

The commands
Code:
printf("Time in the first round");
scanf("%f", &time1);
printf("Time in the second round");
scanf("%f", &time2");
Means that it reads the variables time1 & time2 and time2.scanf is a standard function that has already been defined in stdio.h, that's why we include it everytime when we start writing our codes. The parameter scanf looks a bit odd, yes I know..but the very first parameter is always a format string that's telling the terminal or MSDOS on how it should be intepreted.

Now let's continue to this:
Code:
printf("Total time: %f\n", time1+time2);
The first parameter is a formatstring, it's telling you that the text "Total time:" should be executed and give you x number, it always marks with the transformationspecification - yeah, even a difficult word for me to remember but you just need to practise and practise, and then you will know all this simple stuff, trust me. However, as I explained for you guys in my previous lesson, we should always end our sentences with a \n if we want to start on a new row.

Okay, that's it for now! Make a code on your own now with variables and post them in this thread.

Lesson 3 will be hopefully released this weekend.

Best Regards,
Nick
 
Last edited:
Why don't you make two individual tutorials for both C and C++?
I don't know much about C++ but
Code:
std::cout << "TEXT" << std::endl;
is usually used in C++, I don't think mixing C and C++ is a good idea.
 
Why don't you make two individual tutorials for both C and C++?
I don't know much about C++ but
Code:
std::cout << "TEXT" << std::endl;
is usually used in C++, I don't think mixing C and C++ is a good idea.

Correct, I might do it.. But C is a very good programming language and very good for C++ programmers who wants to use C's useful libraries. :)
 
Back
Top