Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

Algorithm:

C Program
with Sir Beets
#include <stdio.h>
void main(){
Last Lecture // code inside
}
#include <stdio.h>
Preprocessor Directives
tells the preprocessor to
#define ROW 3
include the contents of
#define COL 4 the standard
input/output header
int signal; (<stdio.h>) in the program

int computeProduct(int x, int y);


C Program
Structure void main(){
int x=5, y;
scanf("%d", &y);
printf("%d", computeProduct(x, y));
}

int computeProduct(int x, int y){


return x*y;
}
PREPROCESSOR SYNTAX DESCRIPTION
Syntax: #define
Macro This macro defines a symbolic constant value and can be any
of the basic data types.
Syntax: #include <file_name>
Header file
The source code of the file “file_name” is included in the
Preprocessor inclusion
main program at the specified place.
Directives Conditional
Syntax: #ifdef, #endif, #if, #else, #ifndef
Set of commands are included or excluded in source program
compilation
before compilation with respect to the condition.
Syntax: #undef, #pragma
Other directives #undef is used to undefine a defined macro variable.
#pragma is used to call a function before and after main
function in a C program.
#include <stdio.h>
Global Variable
used to define the
#define ROW 3
variables that can be used
#define COL 4 anywhere in the program
and is used in more than
int signal; one function

int computeProduct(int x, int y);


C Program
Structure void main(){
int x=5, y;
scanf("%d", &y);
printf("%d", computeProduct(x, y));
}

int computeProduct(int x, int y){


return x*y;
}
#include <stdio.h>
Function Prototype
#define ROW 3 and Definition
Function Prototype:
#define COL 4 Declares that there exists
a user-defined function.
int signal; User-defined Function:
Tool created to perform a
specific task
int computeProduct(int x, int y);
C Program
Structure void main(){
int x=5, y;
scanf("%d", &y);
printf("%d", computeProduct(x, y));
}

int computeProduct(int x, int y){


return x*y;
}
#include <stdio.h>
Note
Global function definition
#define ROW 3 works but we follow a
programming structure.
#define COL 4

int signal;

C Program int computeProduct(int x, int y) {

Structure return x*y;


}

void main(){
int x=5, y;
scanf("%d", &y);
printf("%d", computeProduct(x, y));
}
#include <stdio.h>
The main() Function
#define ROW 3 Every C program has a
main() function with a
#define COL 4 declaration and
execution part.
int signal; Functions can return a
value and receive
information.
int computeProduct(int x, int y);
C Program
Structure void main(){
int x=5, y;
scanf("%d", &y);
printf("%d", computeProduct(x, y));
}

int computeProduct(int x, int y){


return x*y;
}
#include <stdio.h>
Local Variable
#define ROW 3 A variable that is declared
inside the function or
#define COL 4 block, must be declared
at the start of the block,
int signal; and must be initialized
before it is used.

int computeProduct(int x, int y);


C Program Define First Before Using

Structure void main(){


int x=5, y;
scanf("%d", &y);
printf("%d", computeProduct(x, y));
}

int computeProduct(int x, int y){


return x*y;
}
#include <stdio.h>
Function Call
Caller – Function that calls
#define ROW 3 another function
#define COL 4

int signal;

int computeProduct(int x, int y);


C Program
Structure void main(){
int x=5, y;
scanf("%d", &y);
printf("%d", computeProduct(x, y));
}

int computeProduct(int x, int y){


return x*y;
}
#include <stdio.h>
Passing Information
Pass-by-copy of values
#define ROW 3
contained by x and y to
#define COL 4 the function
computeProduct().
int signal;

int computeProduct(int x, int y);


C Program
Structure void main(){
int x=5, y;
x y

scanf("%d", &y); 7
5
printf("%d", computeProduct(x, y));
}
x y
int computeProduct(int x, int y){
return x*y; 5 7
}
return-type function-name parameter-list

int computeProduct(int x, int y){


return x*y;
{ } and the code enclosed
}
defines a block of code
Parts of a return-statement

Function
Function Header int computeProduct(int x, int y){
return x*y; Function Body
}

In what ways do Function


Headers and Function
Prototypes differ?
#include <stdio.h>
Preprocessor Directives
#define ROW 3
#define COL 4

int signal; Global Declaration

int computeProduct(int x, int y); Function Prototype


C Program
Structure void main(){
int x=5, y;
The main() Function

scanf("%d", &y);
printf("%d", computeProduct(x, y));
}

int computeProduct(int x, int y){ Function Definition


return x*y;
}
/***********************************************************************/
/* Description: This is a test program that illustrates the Basic Structure of a C Program*/
/* Author : Blasminda Catubig Mayol */
/* Date : September 1, 2019 */ Documentation
/* Place : University of San Carlos, Cebu City, Philippines */
/***********************************************************************/

#include<stdio.h>
Preprocessor Directives
#define c 10
#define d 20

int m = 22, n = 44; Global Declaration


int a = 50, b = 80;
void test();
Function Prototype
Formal int main()
{
int x, y, prod;

C Program x = 5;
y = 4;
prod = x * y;

Structure printf("\nAll variables are accessed from the main function");


printf("\nValues: m=%d:n=%d:a=%d:b=%d\n", m,n,a,b);
The main() Function

test();

printf("\n\nThe values of c = %d; d = %d.", c, d);


printf("\n\nThe product of x and y = %d.", prod);
return 0;
}

void test()
{
printf("\n\nAll variables are accessed from the test function");
printf("\nValues: m=%d:n=%d:a=%d:b=%d", m, n, a, b); Function Definition
printf("\nc = %d; d = %d.\n", c, d);
}
You insert comments to document programs and improve program
readability. Comments do not cause the computer to perform any
action when the program is run—they’re ignored by the C compiler
and do not cause any machine-language object code to be
generated.
You can use /*...*/ multi-line comments in which everything from
Comments in /* on the first line to */ at the end of the last line is a comment.

C Current C Standard allows Single-line comments


// this function will compute the product
// of two numbers
We prefer // comments because they’re shorter and they eliminate
common programming errors that occur with /*...*/ comments,
especially when the closing */ is omitted.
Header Comments
Comments written at the beginning of the program that must include
the following information:
Comments in
C a description of the program
programmer’s name
the date & time (created/updated)
other needed information
Inline and block comments
Comments in Inline comments are short comments that are interspersed through
your program code or on the same line as a program instruction.

C Block comments explain what an entire section of your code is


supposed to do. Write them above the block or function.
Blank Lines and White Space
– You use blank lines, space characters and tab characters (i.e.,
“tabs”) to make programs easier to read. Together, these
White space characters are known as white space. White-space characters are
normally ignored by the compiler.
Identifiers and Case Sensitivity
An identifier is a name given to entities such as constants, variables,
structures, and functions.
An identifier is a series of characters consisting of letters, digits and
underscores (_) that does not begin with a digit. Must be unique
and not contain white spaces.
Identifier C is case sensitive
uppercase and lowercase letters are different in C
different identifiers
A1 is not the same as a1
main is not the same as Main
_uText is not the same as _UText

Camel Notation Identifiers that start with an


computeProduct underscore are often used by
netIncomeTax2021 compiler and library.
Keywords are reserved words which have been assigned with
specific meanings in the C language. Cannot be used as
variable name, constant name, and others.

C LANGUAGE 32 KEYWORDS
auto break case char

Keywords const continue default do


double else enum extern
float for goto if
int long register return
short signed sizeof static
struct switch typedef union
unsigned void volatile while
A variable is named memory location that is used to store data, its
value can be changed, and its value can be reused many times.
data_type variable_name;
data_type variable_name1, variable_name2,
variable_name3;

Variables
int width, height = 5; float area, side = 2;
width = 8; area = side * side;

char letter = 'A'; double d;


d = 10;
Initialization:
Assigning a literal to the variable.
For variables: Specifies the type of data it can store.
For functions: Specifies the type of data it is expected to return.

Data Type Types Data Types


Primary (Built-In) char, int, float, double, void
Derived array, references, pointers
User-Defined structure, union, enum
Types Function
Used to declare variables that store numeric or
int integer values
It does not have a fractional part

Primary float
Used to declare variables that can take on
numeric values with a fractional part
Data Types Used for variables that can take on numeric
double values with a fractional part
Its size is more than float
Used to declare variables that can store one
char character as a value
Holds no value
void
Void type function: will not return any value
int i;
char c;
float f;
double d;

printf("size of int is %d\n", (int) sizeof(i));


sizeof
printf("size of char is %d\n", (int) sizeof(c));
printf("size of float is %d\n", (int) sizeof(f));
printf("size of double is %d\n", (int) sizeof(d));

Look in to the different


format specifiers for printf()
char is a 1 byte int
range = {0 to 127}
You can assign an int value to char.
char letter = 65;
printf("%c", letter);
char // A

You print and int value of a char.


char letter = 'A';
printf("%d", letter);
// 65
ASCII TABLE

https://www.asciitable.com/
The fundamental raw material of any language used to represent
information, and useful to build computer programs.
Character Set Categories of the characters in C
1. Source Character Set
2. Execution Character Set
Alphabet: a-z and A-Z
Digits: 0,1,2,3,4,5,6,7,8,9

Special Character Examples


~ > * ]
Source % ^ \ !
Character Set | # ) ,
@ = ʹ {
+ & : ?
< $ [ .
_ / " }
- ( ;
Execution characters set are always represented by a backslash (\)
followed by a character. Note that each one of character constants
represents one character, although they consist of two characters.
These character combinations are called as escape sequence.

Execution printf("size of int is %d\n", (int) sizeof(i));


Character Set
Notice that the characters \n were not printed on the screen. The
backslash (\) as used here is called an escape character. It indicates
that printf is supposed to do something out of the ordinary. When
encountering a backslash in a string, the compiler looks ahead at the
next character and combines it with the backslash to form an
escape sequence.
Character Escape Sequence Result
Null \0 Null
Alarm (bell) \a Beep Sound
Back space \b Moves previous position
Character
Horizontal tab Escape \t
Sequence Moves nextResult
horizontal tab
New line \n Moves next Line
Execution Vertical tab \v Moves next vertical tab
Character Set Form feed \f
Moves initial position of
next page
Carriage return \r Moves beginning of the line
Double quote \" Present Double quotes
Single quote \' Present Apostrophe
Question mark \? Present Question Mark
Back slash \\ Present back slash
short, long, signed and unsigned modifies the meaning or size of a
variable. May be declared as is, assumed to be int.

short int a;
long int b;

Qualifiers signed int c;


unsigned int d;
printf("size of short is %d\n", (int) sizeof(a));
printf("size of long is %d\n", (int) sizeof(b));
printf("size of signed is %d\n", (int) sizeof(c));
printf("size of unsigned is %d\n", (int) sizeof(d));
Types Description
Arrays are sequences of data items having
homogeneous values.
Arrays Final
They have adjacent memory locations to
Derived store values.
Data Types Function pointers allow referencing
References
functions with a particular signature.
These are powerful C features which are
Pointers used to access the memory and deal with Pre-Final
their addresses.
Types Description Prog II

It is a package of variables of different types


under a single name. This is done to handle
Structure data efficiently.
"struct" keyword is used to define a structure.

These allow storing various data types in the


User-Defined same memory location. Programmers can
Union define a union with different members, but
Data Types only a single member can contain a value at a
given time.

Enumeration is a special data type that consists


of integral constants. Each of the constants is
Enum assigned with a specific name.
"enum" keyword is used to define the
enumerated data type.
Constant Type Description
Integer Number or Integer Constant A number (+ or -) without a decimal point.
Real Numbers (Floating Point Numbers)
A number (+ or -) with a decimal point.
Real Constant (Floating Point Constant)

Constants Character Constant


A single alphabet or a number or a special
symbol enclosed in a single quote.
A group of alphabets, numbers, special
String Constant characters that are enclosed in double
quotes.

const int x = 12; Recall: #define


//x = 14; x becomes read only #define ROW 3
#define COL 4
printf("%d", x);
End of Lecture
Next meeting: Quiz
Next week: Operators, Functions, and Control Structures
üCreate functions for:
1. Uppercase to lowercase character conversion (‘A’ to ‘a’)
2. Lowercase to uppercase character conversion (‘z’ to ‘Z’)
3. Compute and print the difference of two characters
4. Print each digit of a number by its ASCII value from ones place to
the highest place (143 would print 515249)

Practice üFamiliarize yourself with ASCII value equivalents of characters


üRemember you can perform mathematical operations for
characters but remember its range limit
üDo a function call in main()
Do not scanf() when not asked for a user input, instead, initialize
values.
#include <stdio.h>

int computeAverage(int a, int b);

int main()
{
int x, y, ave;
x = 5;
y = 7;
Example ave = computeAverage(x,y);
// printf("%d",ave); do not print unless asked
return 0;
}

int computeAverage(int a, int b){


int retval;
retval = (a + b)/2;
return retval;
}
みんな頑張って

You might also like