Structure of A Simple C Program
Structure of A Simple C Program
SIMPLE C
PROGRAM
Structure of a simple C program
#include <stdio.h> A. #include <directive> - “contains information needed by
the program to ensure the correct operation of C’s
standard library functions” (LIM02)
void main()
Example: #include <stdio.h>
{ B. Body of the program – “start by typing void main() and the
printf("Hello World!!!"); { and } (open and close braces ). All statements should be
written inside the {and} braces” (LIM02)
getch(); C. Variable declaration section – “it is the place where you can
declare your variables” (LIM02)
}
D. Example of commonly used include file in C
Output language(SARREAL97).
stdio.h - defines types and macros needed for standard
I/O
Structure of a simple C program
D. Example of commonly used include file in C
#include <stdio.h> language(SARREAL97).
void main() stdio.h - defines types and macros needed for standard
I/O
E. Input Statement – a statement used to input a single
{ character or a sequence of characters from the
printf("Hello World"); keyboard(SARREAL97).
Printf – a reserved word used to output function that commands the computer
to display the message on the screen which is enclosed in double quotations(“ ”)
Syntax – refers to the rules that govern the structure of commands, statements,
or instructions and the rules governing the structure and the content of the
statements
Ex. printf(“text/message”)
Semicolon(;) – used to separate one statement to another statement
Scanf – a function used to input single character or sequence of characters
from a keyboard, it needs the control string codes in able to recognize.
Spaces are not accepted upon inputting. Terminated by pressing spacebar
Format string specifiers – start with a percent sign (%) and are followed by a
single letter indicating the type of data and how are able to be formatted
Ex. %f – number with floating or decimal
Scanf(“%f”, &pesos);
printf(“%f”, pesos);
C Data Types
float “%f” Used for outputting numbers with 8.12, 16.06 -23.813
decimal places
void Valueless
Basic Arithmetic Operation
SYMBOL DESCRIPTION
+ Used in addition
- Used in subtraction
/ Used in division
* Used in multiplication
Increment
++
Decrement
--
#include<stdio.h>
void main()
{
char name [10];
int age;
getch ();
}
/*This program adds and multiplies two values*/
#include<stdio.h>
void main()
{
int a , b , c , e;
c = a+b;
e = a*b;
printf("\nThe sum of the two values is %d",c);
printf("\nThe product of the two values is %d",e);
getch();
}
#include<stdio.h> printf("\nThe sum of the two values is %.3f",c);
void main() printf("\nThe product of the two values is %.3f",e);
{
printf("\nThe difference of the two values is %.3f",f);
float a , b , c , e, f, g; printf("\nThe product of the two values is %.3f",g);
getch();
printf("\n\n Enter your }
first value:");
scanf("%f", &a) ;
c = a+b;
e = a*b;
f = a-b;
.3f – up to three decimal places only
g = a/b;
Conditional Statement are statement that check an expression
then may or may not execute a statement depending on the result of the
condition.( #include<stdio.h>
The IF-ELSE statement void main()
General Form {
if (expression) int grade;
statement_sequence; printf("Enter your grade: ");
else scanf("%d", &grade);
statement_sequence;
if(grade>=75)
In an if-else statement, if the expression is
TRUE(1), the statement or block of statements printf("Congratulations you PASSED");
after the if statement will be executed; otherwise,
else
the statement or block of statements in the else
statement will be executed. printf("Sorry you FAILED");
Note: Only the code associated with the if or the code is getch();
}
associated with the else executes, never both.
The NESTED-IF statement
• One of the most confusing aspects of the if statement in an
programming language is nested ifs. A nested if is an if statement that
is object of either an if or else. This is sometimes referred to as “an if
within an if”.
• The reason that nested ifs are so confusing is that it can be difficult to
know what else associates with what if.