You are on page 1of 15

Structure of

C Program

Carlo H. Coronel
ITA102 - Fundamentals of Programming,
Database Theory and Applications
Data Types
int (Integer)
can store a value in the range -32768 to
+32767. No fractional part.
Format:
int variable_name;

float (Float)
has about seven digits of precision and a
range of about 1.E-36 to 1.E+36.
Format:
float variable_name;
double (Double)
double, or double precision, number has about
13 digits of precision and a range of about 1.E-
303 to 1.E+303.
Format:
double variable_name;

char (Character)
- to store one character to a variable name, the
character should be enclosed in single quotes.
Single character.
Format:
char variable_name;
Example:
char first_char = ‘A’;
Layout of a Simple C Program

pre-processor directives
global declarations;

main( )
{
local variables to function main ;
statements associated with function main ;
}
Layout of a Simple C Program

#include <stdio.h>
main ()
{
int number, product;
int MULTIPLIER = 10;
printf (“Enter any number to be multiplied by 10\n”);
scanf (“%d”, &number);
product = number * MULTIPLIER;
printf (“%d * %d = %d”, number, MULTIPLIER, product);
}
Basic Input and Output codes

The printf function


Example:
Printf (“Hello!”);

The scanf function


Example:
scanf(“%d”, &num);
Basic Input and Output codes

Format Specifiers

%c – used for single characters


scanf (“%c”, &ch);
printf (“%c”, ch);

%d – used for whole numbers


scanf (“%d”, &num);
printf (“%d”, num);
Basic Input and Output codes

Format Specifiers

%f – used for number with floating or decimal point


scanf (“%f”, &pesos);
printf (“%f”, pesos);

%s – used for string of characters


scanf (“%s”, &str);
printf (“%s”, str);
Running C Program
Developing a program in a compiled language
such as C requires at least four steps:

1. Editing (or writing) the program (source file)


2. Compiling - source file must be translated
into binary numbers understandable to the
computer's Central Processing Unit.
Compiling also produces an object file.
3. Linking - after linking, the file extension is
.exe which are executable files.
4. Executing - running the program for
production.
Basic C Operators
() Parentheses (for grouping)
* Multiplication
/ Division
% Modulus (finding the remainder)
+ Addition
- Subtraction
= Assignment (assigning the value
to a variable)
Basic C Operators
Example:
Finding the average of three input
numbers:

int num1, num2, num3, ave;


ave = (num1 + num2 + num3)/3;

** Take note that the remainder is dropped


when you declare the variable (storing
the final result) as integer type.
Unary Operators
Increment: ++value Increment: value++

#include <stdio.h> #include <stdio.h>


main() main()
{ {
int num1 = 10; int num1 = 10;
int num2 = 5; int num2 = 5;
int total; int total;
total = num1 + ++num2; total = num1 + num2++;
printf (“total = %d”, total); printf (“total = %d”, total);
} }
10 + 6 = 16 10 + 5 + 1 = 16
Unary Operators
Decrement: --value Decrement: value- -

#include <stdio.h> #include <stdio.h>


main() main()
{ {
int num1 = 10; int num1 = 10;
int num2 = 5; int num2 = 5;
int total; int total;
total = num1 + -- num2; total = num1 + num2--;
printf (“total = %d”, total); printf (“total = %d”, total);
} }
10 + 4 = 14 10 + 5 - 1 = 14
?

You might also like