You are on page 1of 17

STRUCTURE OF A

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).

 Getch – “a function used to input a single character from a


getch(); keyboard without echoing the character on the monitor”
(SARREAL97).
Example: getch();
}
F. Output statement – a statement used to display the
Output
argument list or string on the monitor (SARREAL97).

Example: printf(“Name”, %s, you are %d yrs old, name, age);


 /* Programmer: Juan dela
Cruz*/

 // Open curly brace signifies begin


 Comment statement – text
embedded in a program for
documentation purposes. It
usually described what the
program does, who wrote it, and
so on. The comments will be
ignored by the compiler.

 \n – used to move the cursor to


the next line

 \n\n - used to move the cursor to


the next line

 “ ” – used for two or more


character
Reserved word – this is a fixed function and cannot be used for
any other purposes. It cannot be used for naming files, variables, or
other user-named objects
Ex.
break case char do double
else float for if int long return
switch void while

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

Data Type Control String Description Example

char “%c” Used for outputting letter or ‘y’ , ‘w’


symbol

char “%s” Used for outputting two or more “melo” “P103”


letters or symbols and alphanumeric
“prof_melo”

int “%d” Used for outputting whole 8, 12, -5, 12854


numbers

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;

printf("\n Enter Name: ");


scanf("%s", &name);

printf("\n\n\n Enter age: ");


scanf("%d", &age);

printf("\n\n\n Hello %s, you are %d years old.", name, age);

getch ();
}
/*This program adds and multiplies two values*/

#include<stdio.h>
void main()
{
int a , b , c , e;

printf("\n\n Enter your first value:");


scanf("%d", &a) ;

printf("\n\n Enter your second value:");


scanf("%d", &b) ;

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) ;

printf("\n\n Enter your


second value:");
scanf("%f", &b) ;

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.

• In C language, the “else is linked to the closest preceding if that does


not already have an else statements associated with it.”
The NESTED-IF statement
1.if……
2.{
3.If……
:
4.}
5.else

Notice that there is a pair of braces found in


number 2 and number 4. The pair of braces
defines the scope of the if statement in number
1 starting from the {in number 2 and ends with}
in number 4. Therefore, the else statement in
number 5 cannot be paired with the if statement
in number 3 because the else statement is
outside the scope of the first if statement. This
makes the if statement in number 1 the nearest
if statement to the else statement in number 5.
The IF-ELSE-IF Ladder
In an if-else-if ladder statement, the
expressions are evaluated from the top
downward. As soon as a true condition
is found, the statement associated with
it is executed, and the rest of the ladder
will not be executed. If none of the
condition is true, the final else is
executed.

The final else acts as a default


condition. If all other conditions are
false, the last else statement is
performed. The final else is not present,
then no action takes place.
The SWITCH statement – a multiple-branch
decision statement
In a switch statement, a variable is successively tested against a list of integer or
character constants. If a match is found, a statement or block of statement is
executed. The DEFAULT part of the switch is executed if no matches are found.
According to Herbert Schildt (1992),
• The switch differs from if statements is such a way that switch can only test for
equality whereas if can evaluate a relational or logical expression
• No two case constants in the same switch can have identical values. Of course, a
switch statement enclosed by an outer switch may have case constants that are
the same.
• If character constants are used in the switch, they are automatically converted to
their integer values.
The SWITCH statement – a multiple-branch
decision statement
Switch (variable)
{
case constant1: {
switch(variable)
{
case constant 1:
statement sequence;
break;
case constant2:
statement sequence;
break;
}
break;
}
case constant2:
statement sequence;
break;
:
:
default:
statement sequence;

You might also like