Skip to main content

Basic Variables

As a programmer, you will frequently want your program to "remember" a value. For example, if your program requests a value from the user, or if it calculates a value, you will want to remember it somewhere so you can use it later.
This post will show you the different types of variables (Reminder:)that are available in C. The four basic variables are int(integers,float (decimals),double (long numbers)and char (characters).
The following sample program will show you how they could be used;

#include stdio.h

int main(void)
{
int integer;
float decimal;
double longnumber;
char character;

integer = 9;
decimal = 5.6;
longnumber = 56489;
character = 'G';

return 0;
}

Now lets learn to display the contents of the different variable types.
Heres a sample program;

#include stdio.h

int main(void)
{
int integer = 9;
float decimal = 5.6;
double longnumber = 564897;
char character = 'G';

printf("The contents of the variable integer are %d\n", integer);
printf("The contents of the variable decimal are %f\n", decimal);
printf("The contents of the variable longnumber are %f\n", longnumber);
printf("The contents of the variable character are %c\n", character);

return 0;
}

The first four lines of the program declare four different variable types.The next four lines show how to display each variables content to the screen. In a standard printf statement, you use the % operator and then the proper format specifier (%d is used for integers, %f for floats and double, and %c for a character).\n is the newline character (include it anywhere in the printf statement to have cause the program to return at that point. After you have put the proper format specifier in the place where you want the variables contents displayed, then after the quotations that end what is displayed to the screen, you put a comma, then the name of the variable that is to be displayed to the screen at that point.If you want to use multiple variables in one printf statement, then you type it like so:
printf("Variable one is %d, Variable two is %d\n", variable_one, variable_two);


Comments

Popular posts from this blog

Turbo C++ for Windows 7 64 bit

 Its a very simple guide to install Turbo C++ on Windows 7 64 bit  operating system. Follow the steps below to install Turbo C++ compiler on your windows 7 ,64 bit operating system. Note - Please let me know whether the process is working for you or if you are getting some kind of error. Step 1) Install the software DOSBOX version 0.73(Search for it in the search bar). Step 2) Create a folder on your C drive, e.g.Turbo; (c:\Turbo\) Step 3) Extract TC into the created folder(e.g. c:\Turbo\);(you can search and download it) Step 4) Run the application DOSBOX 0.73,previously downloaded and installed. Step 5) Type the following commands at the command prompt that will appear, [z]: mount d c:\Turbo\ Following this you will get a message "Drive D is mounted as local directory c:\Turbo\" Step 6) Type d: to shift to d: Step 7) Then type commands mentioned below; cd tc cd bin tc or tc.exe Step 8) In the turbo C++ go to Options\Directories\ Chang

Calculating the average of two numbers using C++

Though this site aims at understanding C language, I am writing a program for you in C++, to have a general idea about C++ too. A program in C++,to calculate the average of two numbers, which are entered by the keyboard while the execution of the program. Try to understand the program; #include &lt iostream &gt using namespace std; int main() {     float num1,num2,average,sum;     cout << "Enter the first number:";     cin >> num1;     cout << "Enter the second number:";     cin >> num2;     sum=num1+num2;     average=sum/2;     cout << "The sum of the two numbers is:"<< sum<<"\n"<<"The average of the two numbers is:" << average; return(0); } Explaination: The new file header used here in this C++ program is the #include . By this , we are asking the preprocessor to add the contents of the iostream file to our program. The program is written in the

Finding the arithmetic operators

Below is a very simple program to find the arithmetic operator. Please feel free to put a comment if need an explanation. #include #include void main()   {     float a1,a2;     char operator;     printf("\n Enter the values of a1 operator a2\n");     scanf("%f%c%f",&a1,&operator,&a2);     switch(operator)      { case '+': printf("%.2f",a1+a2); break; case '-':  printf("%.2f",a1-a2);  break; case '*':  printf("%.2f",a1*a2); break; case '/':  if(a2==0)  printf("error\n");  else  printf("%.2f",a1/a2);  break; default: printf("unknown operatotr\n"); break;      }    }