Learning C
We will be doing this lab inconjuction with the C lecture. Key concepts will be explained and you will put into practice.
So let's make our first program.
To do this you will need to open up a terminal and ensure your system has gcc
installed.
1. First C Program
-
Create a new directory and call it
ELEE1119/Learning_C
we can do this using the following commands in terminal. -
Now you are to navigate to this directory using the following command and create your first file:
-
If we use the command
ls
we can list the content of the directory and should see at a file named 'helloworld.c' -
Now we are going to open up and edit the content of the file and write it out:
-
Enter the following:
#include <stdio.h> // we need this library to get access to the input and out put methods for printing to terminal int main() { printf("Hello World\n"); // lets say hello, where it all began... printf("Goodbye World\n"); // this seems fitting as the program will close after this. return 0; // returns 0 to the int of main() and terminate the program }
Some explanation about the above code:
-
All code gets executed inside of
main()
, -
For the program to terminate the
main()
has to have a returnable value,int
, -
The keyword at the end of the
main()
isreturn
, this is will return the value preceeding it, -
A
0
execute means no errors. -
Similar to
C#
to use librariesc
programs import with the#include
keyword instead ofusing
. -
The included library is the standard input out header,
stdio.h
. -
By including this header file we have access to the
printf()
function that enables us to return information to the terminal in string format.
-
-
Now we are going to compile the code so that we have an executable file that can be run from the terminal:
-
gcc
is another shell command that is built inc
, it's purpose is to compile.c
files into executables using the thegcc
. -
Using the option
-o
we specifiy the output path/to/file -
For more info -> gcc
-
-
Now let's see the fruits of our labour, the file can be executed as follows:
2. Input/Output functions
In C programming, printf()
is one of the main output function. The function sends formatted output to the screen. For example, the code below is a modified version of the helloworld programme we wrote a moment ago.
-
Create a new file
vim inputoutput.c
and reproduce the code below:#include <stdio.h> int main() { // Displays the string inside quotations printf("C Programming"); return 0; }
-
Compile and run:
2.2 Data Type: Printing Integer
-
Now we are going to modifiy the script again
$ vim inputoutput.c
to look like below:#include <stdio.h> int main() { int testInteger = 5; printf("Number = %d", testInteger); return 0; }
We use
%d
format specifier to printint
types. Here, the%d
inside the quotations will be replaced by the value oftestInteger
. -
Run the script again...
-
Well we need to recompile the code.
2.3 Data Types: Printing Float and Double
-
Open and modify the same file again to look like below:
#include <stdio.h> int main() { float number1 = 13.5; double number2 = 12.4; printf("number1 = %f\n", number1); printf("number2 = %lf", number2); return 0; }
-
Compile the code again using format
gcc <filesource> -o <fileoutput>
... -
Run it
./<fileoutput>
:
2.4 Data Types: Printing Characters
-
Open and modify the same file again to look like below:
-
Remember to compile and then run:
2.5 User Input in C
In C programming, scanf()
is one of the commonly used function to take input from the user. The scanf()
function reads formatted input from the standard input such as keyboards.
-
Again we will modify the program to look like the code below:
#include <stdio.h> int main() { int testInteger; printf("Enter an integer: "); scanf("%d", &testInteger); printf("Number = %d",testInteger); return 0; }
Here, we have used
%d
format specifier inside thescanf()
function to takeint
input from the user. When the user enters an integer, it is stored in thetestInteger
variable.Notice, that we have used
&testInteger
insidescanf()
. It is because&testInteger
gets the address oftestInteger
, and the value entered by the user is stored in that address. We will cover addressing and pointers at a later date. -
Compile and run:
3. Format Specifiers
Here is a table of possible format specifiers for input and output:
Data Type | Format Specifier |
---|---|
int | %d |
char | %c |
float | %f |
double | %lf |
short int | %hd |
unsigned int | %u |
long int | %li |
long long int | %lli |
unsigned long int | %lu |
unsigned long long int | %llu |
signed char | %c |
unsigned char | %c |
long double | %Lf |
4. Data Types
-
Create a new file with
vim
like this: -
We are going to write a program that returns the size of each data type availabe in
c
.#include<stdio.h> int main(){ printf("Data_Types\t\tStorage_Size \n"); printf("char\t\t\t%d byte(s) \n", sizeof(char)); printf("int\t\t\t%d byte(s) \n", sizeof(int)); printf("double\t\t\t%d byte(s) \n", sizeof(double)); printf("float\t\t\t%d byte(s) \n", sizeof(float)); printf("unsigned char\t\t%ld byte(s) \n", sizeof(unsigned char)); printf("long\t\t\t%d byte(s) \n", sizeof(long)); printf("unsigned long\t\t%ld byte(s) \n", sizeof(unsigned long)); printf("long double\t\t%ld byte(s) \n", sizeof(long double)); return 0; }
-
Now enter the following to see the data types and there available sizes in bytes: