I am learning to code C (I am a beginner) and recently came across a problem which asked me to write a C code which lets the following people:
- above 18 and below 90 (both inclusive)
- who have a vip pass
drive a car. Here is my attempt at it:
#include <stdio.h>
// &&, || , ! is for 'and', 'or' and 'not' respectively. in if statements.
int main()
{
int age, Vippasscheck;
printf("Welcome to the land of illegal driving\n");
printf("Do you have a vip pass?\n1)Yes\n2)No\n");
scanf("%d", &Vippasscheck);
if (Vippasscheck == 1)
{
printf("Congratulations, You can drive!\n");
}
else
{
printf("Please enter your age\n");
scanf("%d", &age);
if (age > 90 || age < 18)
{
printf("Sorry, You cannot drive!\n");
}
else
{
printf("Congratulations, You can drive!\n");
}
}
return 0;
}
This code works fine and all this works for me but the solution given is like this (here is where the ambiguity arises):
#include<stdio.h>
int main(){
int age;
int vipPass = 0;
vipPass = 1;
printf("Enter your age\n");
scanf("%d", &age);
if ((age <= 90 && age>=18) || (vipPass==1))
{
printf("You are above 18 and below 90, you can drive\n");
}
else
{
printf("You cannot drive\n");
}
return 0;
}
The solution mentioned here first defines an integer vippass
and then assigns it the value 0 and then in the next line redefines it the value 1?
int vipPass = 0;
vipPass = 1;
I am unable to understand how can one variable be assigned something in first line and then reassign it something else? I am a beginner.
Actually the person whose lectures I was watching to understand C Said initially that "I can keep the vip pass value to zero for everyone else, but here I am giving myself a vip pass" and then he added the second line.
Edit: I now understand a very important thing about C. You can define the value of a variable(initializing it)(for some initial set of instructions) and then at some later point reassign its value for another set of instructions:
#include <stdio.h>
#include <math.h>
int main(){
int a=5;
printf("the value of a is %d\n", a);
a=7;
printf("the value of a is %d\n", a);
return 0;
}
the output of this is:
the value of a is 5
the value of a is 7
This happened because Compiler reads the code line by line. This property is what people use, to do different things at different intervals (i.e. at different places in their code), by assigning the same variable differently at different points.
So yes, your code won't be messed up if you assign it different values at different points, and the initial part of code will use initial assigned value of the code and final part of the code will use final part of the assigned value of the same variable.
CodePudding user response:
You can always reassign new values to a variable; that's why it's called a variable, after all, and it happens all the time in programming. The important thing is that the data type must be correct, but in this case vipPass
has been declared as an integer, and gets integer values assigned.
Although I must say this is, in fact, unnecessarily confusing. What often happens is that programmers declare a variable without initialising it, i.e., they define it (with its type) but don't assign it a value:
int vipPass;
Here we have declared vipPass
as an integer variable but not given it a value. If we go beyond that and not only declare it but give it its first value, that's called an initialisation:
int vipPass = 0;
Here we have initialised vipPass to zero. It does not prevent us from giving it a different value later on (as vipPass = 1;
does), but it is indeed quite pointless to initialise it to zero and then, immediately afterwards, give it a new value. The more customary way would be to either initialise it to one immediately with int vipPass = 1;
or to separate declaration and first value assignment, but without the zero value in between:
int vipPass;
vipPass = 1;