Skip to document

Activity 5 Programming Microcontroller Input Output Functions 2

N/A
Course

General Education (Eng 101)

487 Documents
Students shared 487 documents in this course
Academic year: 2017/2018
Uploaded by:
Anonymous Student
This document has been uploaded by a student, just like you, who decided to remain anonymous.
National University (Philippines)

Comments

Please sign in or register to post comments.

Preview text

University of Science and Technology of Southern Philippines ECE Department

Name: __________________________ Date Performed: ________________ Subject: _________________________ Date Submitted: ________________

Schedule: ________________________ Remarks : _____________________

Programming Microcontroller; C Input/Output Functions

Activity #5 / Simulation # By: Daryl Anne B. Varela., MECE

OBJECTIVES:

 To be familiar with the input function ().  To use variables and different data types.  To process input data and display a desired output.

THEORY:

Normally, you are at liberty to give functions whatever names you like, but the main is special because your program begins executing at the beginning of the main(). This means that every program must have a main() function.

The first line of the program, #include <stdio> tells the compiler to include standard input/output library information. This includes information about the printf command, which is a library function that prints output, in this case, the string of characters between the quotes. For example, in printf (“Hello World!”), Hello World! is a character string or string constant, and is the argument passed to the printf function. Details of functions will be covered later.

VARIABLES

A variable is a name for a place in the computer's memory where you store some data. We can assign values to variables through the assignment statement. For example, a=2; b = 1; a=100;

Variable Names

Only the following characters are allowed for variable names: Small letters (a-z) Capital letters (A-Z) Digits (0-9) Underscore(_)

Valid variable names: a, b, MyVariable, num1, no2, choice, Present_Age

Invalid variable names: 2num (must start with a letter) Last Score (blanks not allowed) exer#1 (# not a valid character)

Variable Declaration

But before you can use variables, they must be declared at the beginning of the function before any executable statement.

A declaration announces the properties of variables; it consists of a type name and a list of variables, such as:

int a; int answer, sum; float b; where int and float are called data types.

Variables can also be initialized while declaring them. int a = 2; int num1 = 25, num2 = 50; float fare=7, change;

DATA TYPES

Variables in C programming that are assigned to different data types. Table 2 below shows samples of variable declarations and assigning of values to variables:

Table 2 Basic Data Types and Samples Data Type Meaning Variable Declaration Assigning Values To Variables

int integer int a; a=100; float floating point float pi; pi=3. double double-precision floating point double quotient; quotient= 3. char character; a single byte char choice; choice = ‘a’

Table 2 below shows these data types, their size during storage, and the limitation of the values that can be represented by these data types.

There are two ways to declare a variable and they are distinguished as the global variable and local variable. Their difference is in the location in which they are declared. The global variable is declared outside the main () function and is visible and accessible to all. The local variable, on the other hand, is declared inside a function, thus it is not accessible to all.

scanf() One of the essential operations performed in C programming language is providing input values to a program, and outputting the data processed by the program. A common method used to input data from the user is the scanf() function which reads data from a keyboard. For outputting results, we have used extensively the function printf(), which sends results out to a terminal. Several functions in ‘C’ language can carry out input-output operations. These functions are collectively known as standard Input/Output Library:

 scanf() - reads input from stdin (standard input), according to the given format, and stores the data in the address of the specified location represented by the given variable name. It uses the <stdio> header file. The format string for this function consists of control characters or specifiers, whitespace characters, and non-whitespace characters. e. scanf(“%i”, &variable); The characters’ specifiers are preceded by a % sign, and are as follows:

SPECIFICIER DESCRIPTION %c A single character %d A decimal integer %i An integer %e, %f, %g A floating point number %lf A double %o An octal number %s A string %x A hexadecimal number %p A pointer %n An integer equal to the number of characters read so far %u An unsigned integer %[] A set of characters %% A percent sign

 printf() – stands for print formatted and is used to output a formatted string. The print formatted data writes to stdout (standard out). It uses the same specifiers as the scanf() shown in the table above.  getch() – is a function that is used to get a character from the keyboard but does not display on the user screen. It belongs to the <conio> library.  getche() –is a function that reads a single variable from the keyboard and displays it on the user screen. Just like getch(), it also belongs to the <conio> library.

EXERCISES:

  1. Make a new project and type the code below. Compile the program and check for errors. Learn to debug your own errors. Then run the program and display it on the user screen.

#include <stdio> int main() { int age; age = 18 ; printf(“You are %d years old.”, age); getch(); return 0; }

Output: ___________________________________________________________

  1. This type, the age value is not fixed. It depends on the number entered from the keyboard. Type the C program below:

#include <stdio> int main() { int age; printf("Enter your age: ") ; scanf("%d", &age); printf("You are %d years old.", age); getch() return 0; }

Note: Enter your age using the keyboard then press the Enter key.

Output: ______________________________________________________________________

  1. Type the C program below:

#include <stdio> int main() { int num1 ,num2,sum; num1 = 10; num2 = 5; sum = num1 + num2; printf("The sum of %d and %d is %d.", num1, num2, sum) ; getch(); return 0; }

Output: ____________________________________________________________

scanf("%s", name); printf("Hello, %s.", name) ; getch(); return 0; }

  1. Make a C program that will ask for a name and year of birth then displays the age. e. Enter name: Maria When were you born? 1996 Maria, you are 18 years old.

  2. Ask for the temperature in Celsius. Display the Farenheit and Kelvin equivalent: e. Enter temperature in Celsius: 100 Farenheit: 212 Kelvin: 373

Celsius(C) to Fahrenheit (F) : F = C x (9/5) + 32 Fahrenheit (F) to Celsius (C) : C = (F – 32) * (5/9)

  1. Ask for the radius of a circle. Solve and display its area.

e .

Enter radius (R in cm): 3 The area of the circle is 28 sq cm.

  1. See the output of the program below: main() { char c; printf("Enter a character: "); scanf("%c", &c); printf("\nCharacter = %c; ASCII Code=%i", a,a); }

Output: _____________________________________________________________ What are the ASCII codes (decimal and hex) of: Decimal Hex a. B _____________ _____________ b. A _____________ _____________ c.? _____________ _____________ d. <space> _____________ _____________ e. 1 ____________ _ _____________ f.! _____________ _____________ g.. (period) ___

Was this document helpful?

Activity 5 Programming Microcontroller Input Output Functions 2

Course: General Education (Eng 101)

487 Documents
Students shared 487 documents in this course
Was this document helpful?
University of Science and Technology of Southern Philippines
ECE Department
Name: __________________________ Date Performed: ________________
Subject: _________________________ Date Submitted: ________________
Schedule: ________________________ Remarks : _____________________
Programming Microcontroller; C Input/Output Functions
Activity #5 / Simulation #2
By:
Daryl Anne B. Varela., MECE
OBJECTIVES:
To be familiar with the input function ().
To use variables and different data types.
To process input data and display a desired output.
THEORY:
Normally, you are at liberty to give functions whatever names you like, but the main is special
because your program begins executing at the beginning of the main(). This means that every
program must have a main() function.
The first line of the program, #include <stdio.h> tells the compiler to include standard
input/output library information. This includes information about the printf command, which is
a library function that prints output, in this case, the string of characters between the quotes. For
example, in printf (“Hello World!”), Hello World! is a character string or string constant, and is
the argument passed to the printf function. Details of functions will be covered later.
VARIABLES
A variable is a name for a place in the computer's memory where you store some data. We can
assign values to variables through the assignment statement. For example,
a=2;
b = 1.5;
a=100;
Variable Names
1