Tutorials Navigation

Tutorials :: New :: Popular :: Top Rated

Tutorials: 18,357 Categories: 12

Total Tutorial Views: 42,396,266

C Programming Tutorial 1, Variables part 1

Tutorial Name: C Programming Tutorial 1, Variables part 1  

Category: PC Tutorials

Submitted By: Nissan

Date Added:

Comments: 4

Views: 2,226

Related Forum: PC Building Forum

Share:

C Programming Tutorial 1, Variables part 1.


The program we are going to be using to learn C programming language is "CodeBlocks" and here is a link to the download its a free program.

Link : [ Register or Signin to view external links. ]

All of these tutorials are going to be done in an console application.

So the first tutorial is going to be on Variables and there is going to be 3 parts 3 different ways on how to use Variables.

So lets get started :

The first thing i'm going to show you today is create a variable to store a number in.


The first thing you need to do then creating a variable is write out the type of data that you want to store inside of your variable and we want to store numbers inside of ours so i am going to write out an int and give that int a name and a value.

So after you have done that this is what your code should look like :

#include <studio.h>
#include <stdlib.h>

int main()
{
   int myFistNumber = 5;
   return 0;
}


The 5 is the value and the myNumber is the name. So now every time you want access to the value of in my case the number 5 all i would have to type out is "myNumber" instead of typing out the number 5.

Now lets create another int that stores another number in it.

So after you have done that this is what your code should look like :

#include <studio.h>
#include <stdlib.h>

int main()
{
   int myFistNumber = 5;
        int mySecondNumber = 9;
   return 0;
}


Now lets create a 3rd variable that is equal to the sum of my first number and my second number.

So after you have done that this is what your code should look like :

#include <studio.h>
#include <stdlib.h>

int main()
{
   int myFistNumber = 5;
        int mySecondNumber = 9;
        int myThirdNumber = myFistNumber + mySecondNumber;
   return 0;
}


So now the value of my third number is the sum of myFistNumber and mySecondNumber so the value of my third number is going to be 14 since the sum of my fist number and second number is equal to 14.

So now i am going to show you how to print out the vaule of myThirdNumber onto the screen and to do that we are going to put :


#include <studio.h>
#include <stdlib.h>

int main()
{
   int myFistNumber = 5;
        int mySecondNumber = 9;
        int myThirdNumber = myFistNumber + mySecondNumber;
        printf("%i", myThirdNumber);
   return 0;
}


We put the %i because we are printing out an integer.

We can put any amount of text before the %i so we could put : the value of my myThirdNumber is %i and where the %i is will be the sum of your first and second number so when we run it it will look like :

[ Register or Signin to view external links. ]

That is it for this tutorial hope you learned something from this.

Ratings

Current rating: 7.33 by 3 users
Please take one second and rate this tutorial...

Not a Chance
1
2
3
4
5
6
7
8
9
10
Absolutely

Comments

"C Programming Tutorial 1, Variables part 1" :: Login/Create an Account :: 4 comments

If you would like to post a comment please signin to your account or register for an account.

NissanPosted:

Valor Very useful tut man, thanks!


Thanks for the feedback I hope it helped.

NissanPosted:

TTG_DIGITAL I love looking at your programming tuts. I always refer to it when I am doing small projects


Thanks for the feedback.

TTG_DIGITALPosted:

I love looking at your programming tuts. I always refer to it when I am doing small projects

ValorPosted:

Very useful tut man, thanks!