(Beginner's Guide) Learning to Program with C: Messing around with Variables#1

in #variables7 years ago

Hi Programmers, wassup?
It's good to meet you again. Today we will be just messing around with variables and know how to use them properly. And yes, We will be doing some programming today :D
So Lets get started,

variablesEverywhere.jpg

You might have heard the term 'variables' from Mathematics, yeah you got it right, your own x. Variables are those terms whose values can be changed. By values i don't mean only numbers they can be a piece of sentence also.

VARIABLES

Syntax of assigning/defining a variable:
(Type of variable) Name_of_the_variable = Value;

For example If I wanna assign the value of 11 to number_of_football_players then i do like
int number_of_football_players = 11;
Here int refers to integer since 11 is a integer :D (( angry face )What is integer? Oh, it is just like a whole number whose value can be negative , zero or positive but not in decimals like 11.11 or 0.23. And by the way, we call decimal numbers in programming as float ie. 11.11 is a float number )

So what if, I wanted to place the price of my newly bought watch onto a variable?
float price = $1200.25; ? (Is it? No we cannot enter symbols and other letters because we have defined our variable as a float(or decimal NUMBER ) )
We can simply place it as
float price = 1200.25;

decimals.jpg

Now what if I want to store my initial letter?
char myInitial = ''G'';
Here in C char refers to character and characters can be anything, it can be ''$'' or ''A'' even a number like ''5''. But it only contains one symbol or alphabets or a number for example "$#" "AB" "56" are not a character.

So what do I do when I want to store my whole name instead of my initials?
Well, get ready(Being Dramatic) I would like you to introduce you with Strings.
Strings are just the collection of characters(yeah i know it was not worth that hype, I overreacted! )
like "$#" is the collection of 2 character "$" and "#"
"AB " is the collection of 3 character (what?) yes, look closely it contains "A" , "B" , and a space " " , everything is a character bro !
And you might have also noted that we keep the characters inside the " " just because they don't walk away :D

Hey bro you said that we're gonna store our name ?
So, as you now know you can store your name in an string,ie a collection of characters so we can tweak it like
char my_name [ 6 ] = "Gaurab" ;
[6] ? because there are 6 letters in my word so i say to computer, hey i'm gonna pass you six characters, you must store it in the variable named my_name. I hope you got that well.
char because everything is a character (and also my name is not a number or a float )
my_name is simply a variable where i wanna store my name
[6] six because i want to store six characters

NOW LET US MAKE A PROGRAM THAT ADD 2 NUMBERS GIVEN BY THE USER.
But make sure to do hello world program, since it is the first program made my most of the programmers including me.

#include <stdio.h> int main(){ /*Since we need to store the numbers given by user, let us create 3 variables, 2 to store the numbers and 1 variable to store the sum */ int n1 , n2 ,sum; //Now Let us ask the user the 2 numbers he wants to add printf("Enter two numbers: \n "); //Now let us store the number given by user in n1 and n2 scanf("%d%d" &n1, & n2); sum = n1 + n2; printf("\n The sum of %d and %d is %d", n1, n2, sum); return 0 ; } Now let us go through the code line by line: A) #include <stdio.h> full form of stdio = Standard input and output This statement includes a file named stdio.h which is also known as header files(.h is for header) which contains a bunch of functions to make our things easier like printf() It prints everything that is written within () , remember functions from #0, it takes something and processes it and gives an output, Here printf() takes a string, and prints it in a console.(remember console application from codeblocks? ) Similarly scanf() is also a function that is used to take user input( ie input from the keyboard ) So what if you didn't "include <stdio.h> " file in your program ? you are right! you could not take user input and print something in your program here. There are also other header file that contains different functions like time.h which you should include if you wanna work with time or math.h etc ................................................................... B) int main() { } Remember f(x) ? here, main( ){ } is also a function. Don't worry about the int and return 0 for now. Trust me, I'll explain it to you later, More Specifically when we read about Function(There is a whole topic on that .) So here the name of our function is main() Infact Every C Program must have the function called main(). It is the first function the computer is going to look up when you run your program. For ex: f1(){ printf("I am f1"); } main(){ printf("I am the main"); f1(); f2(); } f2(){ printf("I am f2, you can rename any files by hitting me up! "); } Here the computer first looks up for main and finds a command to print "I am the main" then it finds f1() so it goes to the function f1() and see if there is something to do, and again goes to f2() and so on... What I wanted to tell you guys is that "it doesn't matter how many functions there are in your computer the program execution always starts from the main() function, so you must have a function called main()" .......................................................................... C) /* */ these symbols are used to write a comment in your program. Anything written in between these will not be executed or refereed as a instruction. These are solely for your purpose, you might wanna write what a piece of code does above it so that it will be easier for you as well as other programmers when they look up your project. Make a habit of writing comments when you code. D) scanf () As said earlier scanf() takes user input. Wear your swimming costume, Let us dive deep in. Ready? Inoder to take a user input first you must have a container to hold that stuff ie a variable to store its value, and also note the type of the variable, should be the same as the type of data user is going to input. For example: if user is going to give us a number we must use integer variable to store that number. And there are something called format specifiers to specify the format that we are using %d as you saw is used to denote integer %f is for a float and %c for a character or string So as done here, "%d%d" tells the computer that we are taking 2 integer input from the user. So when this piece of code runs whatever number you type in will be stored in the variable n1(since it is at the first) unless you print enter, similar happens on n2 (But there is a limit to the range of number the int variable can store/hold )and by writing & infront of them ie &n1 , &n2 we are telling it to store the values in n1 and n2 variables( integer variable ) respectively. &---- is known as ampersand ( I will write a small blog on ampersand after this so don't worry now. ) So this is how scanf() works Tell it what type of data you want to store (By using format specifiers) GIve it a variable to store that data ( Which matches its type) E) sum = n1+n2 hope you get this we are just saying the computer to add n1 and n2 and store it in the sum variable. F)printf("") In this statement by saying %d I am saying to computer that I need to display the value of a integer variable in this place so make a space. and after we end our writings we give it the value like n1 n2 and sum in a serial manner. What if we gave the name of the variable n1 n2 and sum instead of %d? Remember any thing in between " " is considered as string so n1 n2 and sum would be also a sets of characters to the computer instead of a variable. That is Right !! %(type) and name_of_variable comes in pair while we want to print or get the user input. That is all for today I hope you had fun with variables today. It was a pretty simple concept so I hope you get the content. If not, feel free to ask me via comments. See you on next blog. Bye!! Image Source:Google

Sort:  

Congratulations @igaurab! You have received a personal award!

1 Year on Steemit
Click on the badge to view your Board of Honor.

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @igaurab! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!