Input/Output & First Program

An important aspect of any programming language is the ability to input and output data. Take, for example, your internet browser. Internet Explorer requires you to type a web address in the address bar, this is a form of input. Furthermore, when running a Google search you provide a text input into the search box.
To do this in the C programming language, you make use of the function:

printf();

This function is already written for us and can be found in the standard input/output library.

 #include <stdio.h>

Now that we have that out of the way, let’s use printf.
Inside the brackets, we specify what to print. You need to place quotations before and after the text inside printf, like so:

  printf("Hello Worldn");

The “n” simply means that we want to jump to a new line after the output

We are now ready to write our first C program.
Here is my page on creating a new C project in Visual Studio.

 #include <stdio.h>

int main(void)
{
   printf("Hello Worldn");

   return 0;
}

Note that, every C program has at least one function, called the “main function”.
The prototype, or header of the function is the following:

   int main(void)

The first word is “int” this refers to what we wish to return when we finish running, note that the last line of Main is “return 0”. 0 is an integer, and assuming that every line above “return 0” was executed we got to “return 0” with no errors. The only thing left to do is return a 0, this means that where were no errors.
The second word, “main” is the name of the function.
Finally, inside the quotations, “void” is the parameter of the function, in this case we can also leave it blank like so:

int main()

Void just means that we aren’t passing any arguments into it.

We will learn more about functions later, for now just know that every program you write must have a main function written as shown here.

Outputting Varaiables

int x = 5;

How about printing out a variable?
The syntax for printf gets a little strange here, just follow closely.

int x = 5;

printf("The value of the integer X is: %d", x);

As you can see, we have %d written inside the brackets and the variable identifier following the comma outside the quotations.
The compiler recognized %d as a placeholder for an integer variable, then looks after the comma for a variable to use to fill that spot. In this case it will print out
The value of the integer X is 5.
Similarly, we can print out a character.

char myC = 'c';

printf("The value of the character variable myC is: %c", myC);

Note that to output a variable of type Char, we use %c as the placeholder.

Input
Outputting variables and text is useful, but we also need to be able to assign values to variables during run-time. This means that as the program is running, a user should be able to decide the value of a variable. To do this we use anther function from the input/output library called scanf.

int x;

scanf("%d", &x);

printf("The value of the integer X is: %d", x);

Following the syntax for the scanf function from left to right, we see that the first item is %d in quotations. This means that we are reading in an integer value. The latter portion of scanf is &x following the comma. Recall that in the printf() function, we simply have x following the comma without the ampersand symbol. When we write “x” (without &) we specify the VALUE of x. As in, whatever this variable holds. Due to the fact that an integer variable will hold an integer value, we wish to just print it out. However, in the case of scanf, we are writing a value into x. We need to specify that whatever the user enters must be assigned to x, we specify to the compiler to find where in memory x resides, to go there and place the value that the user enters. The ampersand symbol, hence, means “address” we are saying write this value into the address of x.
If you run the command:

int x = 3;
printf("The value of the integer X is: %d", &x);

Note that here, there is an ampersand symbol in front of x. What this will do is print out the address in memory where x resides. Be careful when programming as you always need to just print the value of a variable and aren’t interested in the physical address. &x is used only for scanf, when reading in a value and writing it into a variable.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s