Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

ER1731 – Lab 1

Introduction to C.

In future weeks we will be using Microsoft Visual Studio and later Keil but this week we are going to use an online compiler to get started.

Go to :  https://www.onlinegdb.com/online_c_compiler

The webpage starts off with a program very like the Hello World code from the lecture slides.  

#include <stdio.h>

int main()

{

printf(“Hello World”);

return 0;

}

You’ll notice a small changes to the program that’s in the notes but we’ll get to that in a bit, first let’s look at what is going on here.

The first line is #include <stdio.h> this tells the C compiler that we are going to be using some functions from a library, in this case the stdio library (this is the standard I/O library) here we are using it because it contains the definition of how printf works.  All C programs use libraries, quite often many libraries and sometimes we bundle libraries together to make life easier but for now we are just going to be using a few so we will list them separately.

The next line is int main().  All C programs (and many other languages) have a main function (we’ll explore functions in depth later in the module) this is where the program goes to start, depending on the complexity of our program we might start new functions, open different files etc but the program always starts at main.  Again we’ll look at functions in a few weeks but the brackets after main allow us to pass information into the main function, often though main doesn’t have any information at the start so the brackets are empty (don’t worry about this, we’ll cover it later).

Why does the main() function here start with int and the one in the notes doesn’t ?

Again we’ll cover this in more detail in a few weeks but the short answer is that int doesn’t need to be there as main functions are always of type int (hence we can leave it off and the program still compiles without errors.)

The next line contains a single open curly bracket {.  This shows that a block of code is beginning, it is matched by a closing curly bracket ( } ) a few lines later.  What these brackets do is encase the code and say everything between these two brackets are part of the main function, anything outside isn’t.  C uses curly brackets to keep code together, any code between two curly brackets is part of the same block, and each open bracket needs a closing bracket.  This can get very confusing in large programs where we have code blocks within code blocks so to try and help it is good programming style to offset code and brackets using the tab key.  So if we had a number of embedded code blocks rather than having something that looked like the below (I’ve left out any code and just shown the brackets) :

{

{

{

}

}

{

}

}

We would have something that looked like :

{

{

{

}

}

{

}

}

The first one is much harder to see if we have closed all the brackets properly and, obviously, we would normally have lots of code between the brackets so we could see which code belonged in which block.

In our example the next line is  :

printf(“Hello World”);

There is a bit going on here, it’s our first line of actual C (Hurrah!).  printf is a command that prints information to the screen and how it works is stored in stdio.h, how does it work ?  We don’t care, that’s what we mean when we say high-level languages are easier to use than lower level ones, there’s a whole bunch of assembly instructions needed to light up pixels on an attached piece of hardware that’s connected via a particular port using some driver software etc.  We don’t care and we don’t want to care we want to print to the screen with the minimum amount of effort and C does it for us (we’ve other things to worry about).  It’s the same principle why nowadays we wouldn’t write a GUI with C, we could, but there are much easier languages that will do the hard work for us (like C# next year).  The actual template for the printf command looks like this :

printf(“”)

printf will print to the screen anything between (“ and “).  

Finally the line ends with a semi-colon (;).

All C statements (and quite a few other languages) end their statements with a ;  This lets the compiler know that the statement had ended.

Finally there is a return 0; We haven’t seen this before and we’ll talk about it later in the course when we look at function and the main function in general but for now it tells us that the program ran OK and it is optional (you can remove it if you want).

Run the program.

Exercise 1.

Change your program to this.

#include <stdio.h>

Int main()

{

printf(“Hello World”);

printf(“Goodbye World”);

return 0;

}

What do you expect to happen ?  What actually happened ?

Change the line printf(“Hello World”);  to printf(“Hello World\n”);

What happened this time ?

Ok now the Hello World is obvious but what is the \n all about ?

\n is an escape character, there are lots of them, what they do is define sequences or characters that don’t appear on the keyboard, this one \n means Line Feed or in other words move to a new line.

On Linux and Unix systems \n will move to a new line, in the C compiler here will too but in some cases Windows applications need \r\n (Carriage Return, Line Feed).  There are a number of other escape codes and we’ll meet a few throughout the module.

Exercise 2.

a) Write a C program to produce the following output : 

*

* *

* * *

* * * *

b) Write a C Program to produce the following output :

*

* *

* * *

* * * *

(Hint, the escape character to produce a tab is \t)

In a few weeks we’ll revisit these exercises and try them again with loops using only 1 printf line.