Building Your First C Application

Chapter 3: Building Your First C Application - Sum of Two Numbers with GitHub Copilot Chat

In the previous chapters, we explored data, methods, and their collaboration in C programs. Now, let's create your first application! We'll build a program that calculates the sum of two user-entered numbers, leveraging the power of GitHub Copilot Chat.


3.1 Setting the Stage

Before diving into code, we need a basic understanding of the program's functionality.

Input: The program will prompt the user to enter two numbers.

Process: It will add the entered numbers using a method.

Output: Finally, the program will display the sum of the two numbers.


3.2 Utilizing GitHub Copilot Chat

Copilot Chat can be a valuable tool while writing your first C program. Here's how to leverage it effectively:

Step 1: Project Setup

Open your preferred text editor or IDE (e.g., Visual Studio Code).

Create a new file and save it with a .c extension (e.g., sum_application.c).

Step 2: Including Necessary Headers 

In Copilot Chat, type:

**Ask Copilot Chat:** Include the header file for standard input/output functions in C.


Copilot Chat will suggest:

C

#include <stdio.h>


Use code with caution.

content_copy

This line includes the stdio.h header, providing functions for user input and output.

Step 3: Creating the main Function

**Ask Copilot Chat:** Create the main function in C.


Copilot Chat will provide a basic template for the main function:

C

int main() {

  // Your program logic goes here

  return 0;

}


Use code with caution.


Step 4: User Input Type:

**Ask Copilot Chat:** Write C code to prompt the user to enter two numbers and store them in variables.


Copilot Chat might suggest:

C

int main() {

  int number1, number2;


  printf("Enter the first number: ");

  scanf("%d", &number1);


  printf("Enter the second number: ");

  scanf("%d", &number2);


  // Your program logic goes here

  return 0;

}

Use code with caution.


This code declares two integer variables (number1 and number2) to store the user-entered numbers. It then uses printf to display prompts and scanf to read the user's input, storing it in the respective variables

Type:

**Ask Copilot Chat:** Write a C function that takes two numbers as input, calculates their sum, and returns the result.


Copilot Chat might provide:

C

int calculateSum(int num1, int num2) {

  int sum = num1 + num2;

  return sum;

}

Use code with caution.


This defines a function named calculateSum that takes two integer parameters (num1 and num2). It calculates their sum and stores the result in an int variable named sum. Finally, it returns the calculated sum using the return statement.


Step 6: Calling the Method and Displaying the Result

Type:

**Ask Copilot Chat:** In the main function, call the calculateSum function and display the sum of the two numbers.


Copilot Chat might suggest:

C

int main() {

  int number1, number2;


  printf("Enter the first number: ");

  scanf("%d", &number1);


  printf("Enter the second number: ");

  scanf("%d", &number2);


  int totalSum = calculateSum(number1, number2);


  printf("The sum of %d and %d is %d\n", number1, number2, totalSum);


  return 0;

}


Use code with caution.

This code calls the calculateSum function, passing the user-entered number1 and number2 as arguments. The returned sum is stored in totalSum. Finally, printf displays the original numbers and the calculated totalSum.