This Is A Custom Widget

This Sliding Bar can be switched on or off in theme options, and can take any widget you throw at it or even fill it with your custom HTML Code. Its perfect for grabbing the attention of your viewers. Choose between 1, 2, 3 or 4 columns, set the background color, widget divider color, activate transparency, a top border or fully disable it on desktop and mobile.

This Is A Custom Widget

This Sliding Bar can be switched on or off in theme options, and can take any widget you throw at it or even fill it with your custom HTML Code. Its perfect for grabbing the attention of your viewers. Choose between 1, 2, 3 or 4 columns, set the background color, widget divider color, activate transparency, a top border or fully disable it on desktop and mobile.
Toggle SlidingBar Area

Learning Scanf for College Students

In the last article on printf statements, I gave an example which contained a scanf statement, and today I am going to go through what a scanf is, why your teacher is making you learn such a thing, and why learning scanf for college students is very lucrative. So let’s analyze the example, shown below, that I gave in the article on learning printf for college students.

Everything on here we know from the last few articles, printf, return, a floating point or decimal number, but what happens if the user wants to enter something? Well as programs typically do, the user is asked to input a number, or their name, or something like that. In the case above, the program is asking the user to enter their GPA. So you may have noticed that the scanf statement is fairly similar to the printf statement, and the reason that is, is well, because they are very similar.


#include 
#include 

int main(void)
{
   float GPA = 0;
   
   printf("Enter your current GPA: ");
   scanf("%f", &GPA);

   printf("Your GPA is %.3f", GPA);
   
   return 0;
}

Variations of Scanf

The only thing in the┬áprocess oLearning Scanf for College Studentsf learning scanf for college students that differentiates it from the printf is the fact that the scanf only has %f, or %d (mentioned in the printf article) or whichever indicates the data type you are scanning in. The scanf statement also takes &GPA instead of just GPA, make no mistake, this is not a typo. scanf reads in the “address” of the GPA variable. Addresses will be explained in a later article that will be linked here, but they are basically like addresses on a street, each variable gets its own little house and it stores all its information at that address, but instead of on a street it is in the computer’s memory.

Now let’s take another example of scanf where multiple things are scanned in at once, it will be similar to the way we did it before. I’ll write the code for it below and explain it afterwards.


#include 
#include 

int main(void)
{
   float GPA = 0;
   int numYear = 0;
   
   printf("Enter your current GPA and number of years studying: ");
   scanf("%f %d", &GPA, &numYear);

   printf("Your GPA is %.3f after %d years in college", GPA, numYear);
   
   return 0;
}

Things remain exactly the same as our other example above, the only difference is that now we are having the user enter two variables which are seperated by a space when input. If the user only enters a single value it will go to GPA and numYear will remain the value that we “initialized,” or set it equal to in its beginning.

More Applications of Scanf

Scanf statements are extremely useful for when you need the user to enter a number to do a calculation on. Let’s have one more example on scanf that has a practical application.


#include 
#include 

int main(void)
{
   int a = 0, b = 0, c = 0;
   printf("Enter two numbers to multiply seperated by a space: ");
   scanf("%d %d", &a, &b);

   c = a*b;//* is the operator for multiplication in C

   printf("The product of %d and %d is %d", a, b, c);
   
   return 0;
}

In this program the two integer numbers a and b will be multiplied by each other and the result will be output.

Conclusion

That’s all for this article on learning scanf for college students. Scanf is also a very important tool when using if statements which will be covered in the next article, and so I will leave you with a final program to try and determine what it will do before going on to the next article on learning if statements for college students.


int main(void)
{
   int a = 0, b = 0;

   printf("Enter two numbers to determine if they are equal, greater, or less than the other: ");
   scanf("%d %d", &a, &b);

   if(a==b)
   {
      printf("%d is equal to %d", a, b);
   }
   else if(a<b)
   {
      printf("%d is less than %d", a, b);
   }
   else
   {
      printf("%d is greater than %d", a, b);
   }

   return 0;
}

 

By |April 27th, 2017|Categories: C Lesson|0 Comments

About the Author:

Leave A Comment