1 Introduction
This lab is your first introduction to C programming and will introduce you to many of the basic concepts of C and low-level coding. By the end of this lab, you will be expecting to understand the follow key points:
1. Understanding C libraries.
2. Understanding functions in C.
3. Understanding arithmetic in C.
4. Printing text to stdout in C.
5. Reading text from stdin in C.
6. Creating C Source files (.c) and Header files (.h).
7. Compiling C code.
The proceeding labs will build and expand this knowledge into the realm of low level computing, including memory management, data structures, methods for approaching low-level computation, and a variety of other subjects. As such, knowing these key principles will be important for future programming and development.
In this lab you are expected to perform the basics of cloning your Lab 2 repository from the GitLab course group. A link to the course group can be found here and your repository can be found in the Lab2 subgroup. See the Lab Technical Document for more information on using git. You will notice that your repository has a file in the Lab2 directory named delete this file. Due to the limitations of GitLab, we are not able to push completely empty directories. Before you push your work to your repository (which should be in the Lab2 directory anyway), make sure to first use the git rm command to remove the extra file. If you do not, your pipeline will fail.
Be sure to read this entire document before starting!
2 Simple C Code Example
Since you should already have some experience with a previous programming language, the easiest way to get started with the C programming language is to try a working example. Read through the following code and then use the instructions at the bottom of this page to get it running.
2.1 example.c
1
2
3
4
5
6 // The stdio . h library gives us access to printf and scanf.
# include <stdio .h>
// The stdlib . h library gives us access to atoi. # include <stdlib .h>
7 int change Number ( int number)
8 {
9 // Add 15 to the number.
10 int new Number = number + 15;
11
12 // Multiply the number by 12.
13 new Number = new Number * 12;
14
15 // Subtract 15 from the number.
16 new Number = new Number - 15;
17
18 // Divide the number by 5. This acts as integer division !
19 new Number = new Number / 5;
20
21 // Find the remainder of the number divided by 150.
22 new Number = new Number % 150;
23
24 // Return the new number after calculations .
25 return new Number;
26 }
27
28 int main ( int argc , char ** argv)
29 {
30 // Create four integer variables.
31 int one , two , three , four;
32
33 // Alert the user that they have to enter some numbers.
34 printf(" Enter four integers: ");
35
36 // Retrieve four integers separated by spaces from stdin .
37 scanf("% d % d % d % d", & one , & two , & three , & four);
38
39 // Print the four integers to the screen .
40 printf(" Numbers Received : % d % d % d % d\ n", one , two , three , four);
41
42 // Update each number using the change Number function .
43 one = change Number ( one );
44 two = change Number ( two );
45 three = change Number ( three );
46 four = change Number ( four);
47
48 // Print the updated numbers to the screen .
49 printf(" Updated Numbers : % d % d % d % d\ n", one , two , three , four);
50
51 // Create four string variables , each of length 10.
52 char s1 [10] , s2 [10] , s3 [10] , s4 [10];
53
54 // Convert the four integers to strings , stored in our string variables.
55 printf(" Converting integers to strings ...");
56 sprintf( s1 , "% d", one );
57 sprintf( s2 , "% d", two );
58 sprintf( s3 , "% d", three );
59 sprintf( s4 , "% d", four);
60 printf(" Done .\ n");
61
62 // Print the integers from various sources , including a original integer ,
63 // a string , and two strings converted back to integers.
64 printf(" Testing AtoI Output: % d % s % d % d\ n", atoi( s1 ), s2 , atoi( s3 ), four);
65
66 // Explicit successful exit code 0.
67 return 0;
68 }
2.2 Output
Enter four integers: 1 2 3 4
Numbers Received : 1 2 3 4
Updated Numbers : 35 37 40 42
Converting integers to strings ... Done .
Testing AtoI Output: 35 37 40 42
2.3 Running example.c on Timberlea
Make a test directory somewhere on Timberlea, create a file named example.c and place the above source code inside it using vim. Save and close vim, then enter the commands at the Unix bash prompt:
Take a few minutes to try different inputs to the program to see what happens.
3 Data Types, Keywords, and Language Features
The C programming language has a variety of keywords which are reserved and may only be used for their intended purpose. These reserved keywords offer a collection of functionality and make up the key building blocks of your C programs, some of which you will use very regularly, and some you may never use at all.
3.1 Data Types
The reserved words you are likely to use the most are related to data types. Since C is statically typed, all of the data types of variables and values are checked at compile time to ensure the logic of your code will not fail. This means you will have to be careful and specific when assigning variable types. However, C has some methods of ”breaking” the static typing rules, such as being able to push data of one type into another type using pointers. We will discuss that in a later lab.
DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma
Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t
Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th
1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of
1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of