Technology Inside Out!

Index ¦ Archives ¦ Atom ¦ RSS

Learn C - More about variables

Welcome to your next tutorial on the Learn C language, here we'll have a deeper look at the use of variables in C language. In previous tutorial about variables in C, we defined a variable sam of integer type and then initialized it with a value (10), this can be done in simpler manner in just one line. If you haven't read the previous tutorial, then please read it first, and you can read the Learn C tutorial Series .

Declaration and Initialization together :

So, we'll have a look at how to do this :

int sam = 10;

In the above statement, I have declared my integer type variable named sam, and then initialised it with a value.

So, I had two options, one to do something like mentioned above, or have declaration in one line and initialization in other as :

int sam;

sam = 10;

Now, we'll see how to take values from our user

Accepting values from user :

We can store values in our variables depending on choice of end user. This is done through a function know as scanf() and is defined under header file stdio.h (Standard Input/Output). We first define a variable named value.

int value;

Now, we'll accept input form user as :

scanf(“%d”, &value);

Note : scanf() function is somewhat like printf function defined before, but with the function name it's necessary to have ampersand (&) before variable name. This operator is used for reference, in simple terms, this helps in passing the address of memory location that is reserved for our new variable. We'll discuss about these things in detail in later tutorial, till now, just see it's done in this way.

Note: scanf() function should always contain ampersand before your variable name.

Now, we'll write a program here which will accept an input from user and then print the same on the screen.

#include<stdio.h>
int main()
{
  int value;
  printf(Enter value : );
  scanf(%d , &value);
  printf(Value entered is %d”,value);
  return 0;
}

The above code snippet would produce the following output :

Learn C variables Program Output

Note: Output compiled in Turbo C with DosBox for Windows, rather than gcc.

Note here, that whatever value end user enters, the same is printed on the screen.

Accepting user Input and displaying output for variables of different data types :

Now, we'll see how you can accept and return different types of variables :

For character type variable , you can do as :

char ch;
scanf(“%c” , &ch);
printf(“%c”, ch);

For float type variable, you can do as :

float fvar;
scanf(%f, &fvar);
printf(%f, fvar);

And so on, you can accept and return the values in variables of different kinds, what you have to remember is , you have to specify the placeholders in C in order to enter and retrieve values in variables, and if you want to accept input, use scanf function with ampersand, and for output use just variable name.

In the next tutorial we'll learn about Operators in C, what they are used for, and then with help of operators manipulate the values stored in variable, and implement all this through a small program to understand about what all we've discussed in these tutorials.

If you like the posts, don't forget to share this with your friends. For any doubts, let us know through comments, we'll answer you and clear your doubts ASAP.

© The Geeky Way. Built using Pelican. Theme by Giulio Fidente on github.

Disclaimer Privacy policy