[source code] C Programming Tutorial - 7 - Variables

+6 Bucky Roberts · August 4, 2014



  • Variables are just placeholders

  • Variables must begin with letters, but then can include numbers and underscores as well

  • No spaces or symbols other than an underscore

  • Can't name a variable the same name as a function or C will think you're trying to use a function (main, printf, etc...)

  • To use a variable, you need to first let C know that you are going to be using it (called defining it)

  • This is so C knows that it's a variable when it comes across it in your program

  • First need to tell C what type of data your variable is going to hold, and then the variable name




#include <stdio.h>
#include <stdlib.h>

int main()
{
    int age;
    int currentYear;
    int birthYear;

    currentYear = 2014;
    birthYear = 1987;
    age = currentYear - birthYear;

    printf("Bucky is %d years old.", age);
}


Post a Reply

Replies

Oldest  Newest  Rating
+2 Alec Grogan · September 2, 2014
I am sorry if this seems dumb, but i am new to C. What is wrong with my code the program won't run.

#include <stdio.h>
#include <stdlib.h>

int main()
{

    int age;
    int currentYear;
    int birthyear;

    age = currentYear - birthYear;
    currentYear = 2014;
    birthYear = 1999;

    printf("Alec is %d years old", age);

    return 0;
}
+5 Aubree Keegan · September 2, 2014
You forgot to capitalize the "Y" in birthYear  :)


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int age;
    int currentYear;
    int birthYear; //you forgot to capitalize the "Y" in birthYear

    age = currentYear - birthYear;
    currentYear = 2014;
    birthYear = 1999;

    printf("Alec is %d years old", age);

    return 0;
}
+2 Musie Meressa · September 2, 2014
/* age shoule be assigned after assigning a value to variables currentYear and birthYear*/
#include <stdio.h>
#include <stdlib.h>

int main()
{
int age;
int currentYear;
int birthYear;


currentYear = 2014;
birthYear = 1999;
age = currentYear - birthYear;

printf("Alec is %d years old", age);

return 0;
}
0 KUAN-WEI HUANG · April 23, 2017
:)
  • 1

C

127,961 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator