C Programming_Project 1_Calculating Total and Average Scores_Task 1: Input/Output of Student Scores

“C Programming — /project-driven mode/” Author: Renfei (Neil Ren) Published: www.neilren.com 8 projects in total, from data input/output through pointers to file operations. All rights reserved; when reposting please credit the source and author. This site uses the Creative Commons Attribution-NonCommercial-NoDerivatives 2.5 China license

------------------------------------------------

Skill Objectives

Be able to correctly input/output data and perform simple aggregation.

Knowledge Objectives

  1. Understand C data types.
  2. Grasp the concepts of integer constants, integer variables, real constants, real variables, character constants, and character variables.
  3. Master input/output statements.
  4. Perform simple operations on data.

Project Requirements

Input the scores of 3 students, and compute the total and average. Program run requirements: Input 3 students’ scores: 79/89/90; total of the 3 students: 258.0; average of the 3 students: 86.0.

Task 1: Input/Output of Student Scores

Implementation code:

[code lang=“c”]#include<stdio.h> main() { int x,y,z; printf(“请输入3个学生的成绩”); scanf(“%d%d%d”,&x,&y,&z); printf(“输出3个学生的成绩”); printf(“x=%d,y=%d,z=%dn”,x,y,z); }[/code]

The structure of C

A C program consists of one or more files, and a file consists of one or more functions. A C program must contain exactly one function named main, and only one. Execution starts from main and ends back at main. As you can see from the code above, a C statement ends with a ”;”. But main() and #include are not statements and need no ”;”. A statement is made up of keywords, identifiers, operators, and expressions. ”{” and ”}” mark the start and end of a function’s execution, or the start and end of a block. Text after ”//” is a comment (single-line); you can also use ”/* multi-line comment */” for comments that span lines. The compiler ignores comments — they’re for humans and won’t be compiled. C is free-form: you can put several statements on one line, but it’s case-sensitive; uppercase and lowercase are different things.

Running our program with VC++ 6

Choose File → New, select the C++ Source File option under the Files tab, type the file name, and remember to add the “.c” suffix to indicate a C source file. Choose the save directory, then click OK to finish creating the file.

Creating a C source file

Type our code, click Build in the menu bar (some call it Compile — a translation issue), then choose Compile and click OK to compile it into an EXE executable. Note: after modifying the code you need to recompile. Then click Debug under Build to run our program and see the result.

Compiling a C source file C program run result

Data Types

In the code, int x,y,z defines three integer variables. What other types are there in C?

  1. Basic types: integer, real (floating-point), character, and enumeration.
  2. Constructed types: array, structure, and union.
  3. Pointer types.
  4. Void type.

C data is divided into constants and variables, both of which also belong to the types above. A constant is fixed in the program — for example, numbers like 1, 2, 3, or symbols like A, B, C. A variable can change during the program; it is described by a type and a name — for example, string i makes i a character variable, which you can assign to and read from. Naming rules: start with a letter or underscore, followed by a combination of letters, digits, and underscores; case matters — a and A are two different things.

The formatted output function — printf()

printf() outputs to the screen. The general form is printf("format string"[, output list]). %d stands for a decimal integer, %f for a decimal fraction (6 digits by default), %c for a single character. Escape characters — the “n” in our code is an escape character meaning “newline”; search Google for more.

The formatted input function — scanf()

scanf() is used to read several values of arbitrary types from the keyboard. The form is scanf("format string", address list), where the address list is the variables — written as “&variable name”, e.g. &a means the address of a.