Sie sind auf Seite 1von 15

Basic C Structure

• Part 1: Discussion (Basic C Structure)


• Part 2 : Learning Task
• Part 3: Hands-on (Machine Problem)
Objectives

At the end of the discussion, the students are expected to:


• Identify the basic components of C program;
• Get familiar with the structure of C program;
• Use simple input and output statements in C;
• Create C arithmetic expressions for solving problem;
• Write a C program following the basic structure of C.
C Programming Language

• is a general purpose programming


• Considered as a middle level programming language because it
combined the low level language and the elegance of high level
language
• Was developed at Bell Laboratories in 1972 by Dennis Ritchie
• C is what is called a compiled language.
– This means that once you write your C program, you must run it through a
C compiler to turn your program into an executable that the computer can
run (execute).
– The C program is the human-readable form, while the executable that
comes out of the compiler is the machine-readable and executable form.
• C is called a structured programming language
– to solve a large problem, C programming language divides the
problem into smaller modules called functions or procedures
each of which handles a particular responsibility..
C Program Structure

• A C program basically has the following form:


– Preprocessor Commands: Declare standard libraries used inside the
program
– Type definitions: Define new data types used inside the program
– Function Prototype: Declare Function Types and Variables passed
function
– Variables: State the names and data types of global and local variables.
– Functions: Include the main function (required) and the functions that their
prototypes were announced above.
– Comments : are plain simple text in a C program that are not compiled by
the compiler. We write comments for better understanding of the program.
#include <stdio.h>
int main()
{
clrscr();
printf (“Welcome to C Programming”);
//this is an output statement
getch();
return 0;
}
• #include <stdio.h>
– Preprocessor commands telling the compiler to include the
header file stdio. h . Header file stdio.h contains the standard
input and output commands in C such as the printf and scanf.
• int main()
– Indicates a main program. Every C program contains this
function.
• {
– Where the main () program starts
• clrscr()
– Is the clear the screen standard function that tells the C compiler to clear the entire
screen.
• //this is an output statement
– Comment that says the preceding line is an output statement
• printf()
– displays on the screen whatever is inside the parenthesis inside the quotation
• getch()
– this command or function allows the user to view the output of a program. Means get
character from the keyboard.
• return (0);
– The return value is the exit code of your program. The 0 exit code is a widely accepted
convention for 'OK the program execution was successful'.
• }
– ending point of the main() program
Basic Input/ Output
Statement in C
• Input
– scanf() is the standard input statement in C. Reads all data types
– getch() reads a single character from keyboard, it does not have any
buffer, so the entered character is immediately returned without
waiting for enter key
– gets() reads series of characters but reads space as character too
– getche() reads single character from the keyboard and displays
immediately on output screen without waiting for enter key
– getchar() reads a character from the terminal and returns it as
integer. This function reads only single character at a time.
• Output
– printf() standard output statement in C
– puts() used to write a series of characters to the output screen
– putchar() displays the character passed to it on the screen and
returns the same character. Displays only a single character at
a time.
Example

• Write a C program that allows you to input your name and


display a comment for the inputted name.
Example

• Write a C program to determine a student’s final grade.


The final grade is calculated as the average of grades in
four subjects. Display the final grade.
Learning Task

• Set A: Write a program that converts the input dollar to its


peso exchange rate equivalent. Assume that the present
exchange rate is 51.50 pesos against dollar. Then display
the peso equivalent exchange rate.
• Set B: Write a program that converts the inputted length in
centimeter into inches. Let us say that 1 inch is equal to
2.54 cm.

Das könnte Ihnen auch gefallen