Programming

profileAdejoke
Wk1_GuidedPracticeCreatingyourfirstCprogram2.docx

Guided Practice – Creating your First C Program

Design

C functionality

Example

Declaration

Datatype + variable name

int number;

Output

printf() function

printf(“Hello World”);

Input

scanf() function

scanf(“%d”, &number);

Assignment statement

variable = data

result = num1 + num2;

If decisional statement

if() statement

if (result > 10)

Now, that we have finished the design of our first program project, we will use either the pseudocode or flowchart as our design tool to create the first program.

1. Review either the pseudocode or flowchart from one of the previous guided practices.

2. Open Dev C++

3. Go to File -> New -> Source File

Graphical user interface, application, Word  Description automatically generated

4. Click on File -> Save As -> Name your File. Then click on the down arrow and select the .c file extension. We use the .c file extension since we are learning standard procedural C in this class. Be sure you know where you are saving your file. It is recommended that you create a folder to store your files for this course. You will save every source program file with a .c file extension throughout the term, not a .cpp file.

Graphical user interface, application  Description automatically generated

5. The first thing we want to do is add some comments at the top of the program showing your name, assignment, and date. Notice that comments are followed by the // syntax (see image below). You can add comments anywhere in your program – the compiler knows to ignore these statements when compiling. Type the statements below.

Graphical user interface, application, Word  Description automatically generated

6. Next, we will add the header file stdio.h (standard input/output). Including this header file will allow us to use the printf() and scanf() functions throughout our program for output and input purposes. The syntax #include <stdio.h> has been added to line 5. Be sure that you type all syntax accurately. One typing mistake will cause compiler errors when you go to run your program. Type the statements below.

Graphical user interface, text, application, Word  Description automatically generated

7. Next, we will include the main( ) function. The main ( ) function is the first function of every C program and is responsible for starting the execution and termination of the program. Note the specific syntax. After we declare int main( ) we add the open and closed curly brackets { } which will contain the executable commands. We also add the return 0; statement which tells the compiler to end the program. Type the statements below.

Graphical user interface, text, application  Description automatically generated

The above code will be used for every program moving forward – it will be referenced as a template to get started.

8. Now that we have our template created. Let’s go back to the program description from the previous guided practices. Be sure to have either the pseudocode or flowchart design handy too.

Here is the program description: Represent the logic of a program that allows the user to enter two values. The program outputs the product of the two values.

Referencing either your pseudocode or flowchart from the design process, let’s begin creating our C program.

9. First, we will declare our variables which will store integer values. The keyword in C for the integer data types is ‘int’. We will also include the variable names from our design and end the statement with a semicolon ; Type the statements below.

Graphical user interface, text, application  Description automatically generated

10. Next, we will add our output statement. Output statements in C are performed by the printf() function. All text that you wish to be displayed needs to be enclosed in double quotations. Also, the printf() statement ends with a semicolon ; Type the statements below.

Graphical user interface, text, application  Description automatically generated

11. Now, we can add the two input statements. Input statements store user data and we use the scanf() function in C to accomplish this task. We include a conversion specifier %d in double-quotes which references the datatype that the variable was declared with. The two conversion specifiers related to an integer datatype are %d and %i. The syntax &num1 and &num2 tells the compiler to store the integer values the user types into the address of the variables named num1 or num2 in the computer’s memory. All scanf() statements end with a semicolon ; Type the statements below.

Graphical user interface, text, application  Description automatically generated

12. Next we will add the assignment statement to multiply the values stored in the two variables. Remember, you always want to include the variable named in computations since each time the program runs the values stored in the variables most likely will change. With assignment statements, anything on the right side of the equals sign is assigned to the variable on the left of the equals sign. Assignment statements also end with a semicolon ; Type the code below.

Graphical user interface, text, application  Description automatically generated

13. The last statement that we want to add is the output statement to display a message and the value stored in the variable result. We will use the printf() function to complete this task. Notice that we need to include the conversion specifier %d inside of the double quotes and the variable name after the comma to display the value stored in the variable. If you are missing either one, the value will not display. Type the code as seen below.

Graphical user interface, text, application  Description automatically generated

The above code is the completed program. You will submit this .c file into the Canvas dropbox for grading.

14. Okay, now that we have added all of the lines of code, it is time to run it. Go to Execute -> Compile and Run. Click. Note: if you didn’t save your file in the beginning, you will be asked to do so prior to compiling – be sure that you save your file with a .c file extension. The following output screen appears.

Text  Description automatically generated with medium confidence

As you can see from above the first printf() statement has been executed and is prompting you to enter two numbers. Enter two numbers.

From the screenshot below, I entered values 3 and 6. After the scanf() function stored those values into the variables num1 and num2, the assignment statement was executed, and then the last printf() function, which displays the message and result of the numbers multiplied together. See screenshot below.

Text  Description automatically generated

Submission instructions: Upload your .c source file and a screenshot of the C output screen. You can save your screenshot in a Word document or upload an image file (.png or .jpeg)

image3.png

image4.png

image5.png

image6.png

image7.png

image8.png

image9.png

image10.png

image11.png

image12.png

image1.png

image2.png