Professional Documents
Culture Documents
THE ORIGIN OF C
ANSI established a committee in the beginning of summer, 1983 to create an ANSI Standard for C Language
The standard was finally adopted in 1990. Turbo C fully implements the resulting ANSI standard for C DEC-PDP11 was the 1st program created using C that uses the UNIX Operating System.
MIDDLE-LEVEL LANGUAGE
C is often called a middle-level language, meaning it combines elements of high-level language with the functionality of assembly language.
HIGH-LEVEL LANGUAGES:
ADA (US Dept. of Defense) MODULA-2 (Niklaus Wirth, 1980) PASCAL (named after Blaise Pascal) FORTRAN BASIC COBOL
HIGH-LEVEL LANGUAGES:
PASCAL (named after Blaise Pascal)
Procedural programming language that encourages programmers to write well-structured, modular programs that take advantage of modern control structures and lack of spaghetti code
HIGH-LEVEL LANGUAGES:
FORTRAN = Formula Translation
Enables programmers to describe and solve complex mathematical calculations
HIGH-LEVEL LANGUAGES:
BASIC = Beginners All-Purpose Symbolic Instruction Code
Developed in 1964; a procedural language that tells the computer what to do step-by-step
HIGH-LEVEL LANGUAGES:
COBOL = Common Business Oriented Language
Developed/designed business applications for
MIDDLE-LEVEL LANGUAGES:
C FORTH = Fourth Generation
Charles Moore, 1970
QUIZ
I. Identify the person who developed the following: 1. BCPL 2. C 3. Language B 4. MODULA-2 5. FORTH II. Write the meaning of the ff. acronyms: 6. BASIC 7. COBOL 8. ASCII 9. FORTRAN 10. BCPL
ANSWERS
I. Identify the person who developed the following: 1. BCPL - Martin Richards 2. C Dennis Ritchie 3. Language B Ken Thompson II. Write the meaning of the ff. acronyms: 6. BASIC Beginners All-Purpose Symbolic Instruction Code 7. COBOL Common Business Oriented Language 8. ASCII American Standard Code for Information Interchange 9. FORTRAN Formula Translation 10. BCPL- Binary Coded Programming Language
Identifier Names
IDENTIFIERS
names used to reference variables, functions, labels, and various other userdefined objects can vary from 1 to 32 characters the first character must be a letter or an underscore
IDENTIFIERS
Correct Incorrect
Data Types
character
integer
floating point
double floating point
valueless
char
used ASCII characters or any 8-bit quantity from values -128 to 127 (but only 0 to 127 are used) ex. char ch; ch = e; *it is also possible to have the type unsigned char
Control characters i.e. non printable characters are put into programs by using a backslash \ and a special character or number. The characters and their meanings are:
\b backspace BS
\f form feed FF (also clear screen) \n new line NL (like pressing return or enter) \r carriage return CR (cursor to start of line) \t horizontal tab HT \v vertical tab (not all versions)
int
used to hold integer quantities
ex. :
int i, j;
i = j = 0;
int x = 25;
float
used to hold real numbers
ex. : float x, y, z; x = 0.1; y = 2.317; z = 0;
double
used to hold real numbers
ex. : double big; small; big = 2.36E208; small = 3.2E 300;
void
declares explicitly a function as a returning no value
declares explicitly a function as having no parameters creates generic pointers
MODIFIERS
alter the meaning of the base type to fit the needs of the various situations more precisely signed, unsigned, long, short may be applied to character and integer base types
*long may also be applied to double
Access Modifiers
control the ways in which variables may be accessed or modified
const may not be changed during execution by your program ex. : const int a; const int count = 100;
Access Modifiers
control the ways in which variables may be accessed or modified
volatile tells the compiler that a variables value can be changed in ways not explicitly specified by the program ex. : const volatile unsigned char *port=0x30;
Declaration of Variables
all variables must be declared before they are used general form of a declaration: type variable_list; type valid C data type variable_list may consist of one or more identifier names with comma separators
Examples:
int a, b, c;
Local Variables
declared inside a function may be referred to as automatic variables
can be referenced only by the statements that are inside the block in which the variables are declared
Formal Parameters
If a function is to use arguments, then it must declare variables that will accept the values of the arguments. behave like any other variables inside the function
Global Variables
known throughout the entire program and may be used by any piece of code hold their values during the entire execution of the program created by declaring them outside of any function
VARIABLE INITIALIZATION
giving variables in C a value at the time they are declared by placing an equal sign and a constant after a variable name general form: type variable_name = constant; ex.: int x = 1;
QUIZ I. Identification
1. names used to reference variables 2. used to hold integer quantities 3. 3-4. used to hold real numbers
QUIZ I. Identification
1. names used to reference variables identifiers 2. used to hold integer quantities int 3-4. used to hold real numbers float, double 5. declares explicitly a function as having no parameters - void
OPERATORS
symbols that tell the compiler to perform specific mathematical or logical manipulations Assignment Operators
general form of assignment statement: variable_name = expression; ex. : x = a+b;
ARITHMETIC OPERATORS
Operator Action subtraction + addition * multiplication / division % modulus division -decrement ++ increment
RELATIONAL OPERATORS
Operator Action > greater than >= greater than equal to less than < less than equal to <= equal == not equal !=
LOGICAL OPERATORS
Operator && || !
Action AND OR NOT
LOGICAL OPERATORS
p 0 0 1 1 q 0 1 1 0 p&&q 0 0 1 0 p||q 0 1 1 1 !q 1 1 0 0
BITWISE OPERATORS
the testing, setting, or shifting of the actual bits in a byte or word *shift operators >> and << move all bits in a variable to the right or left as specified *ones compliment operator ~ will reverse the state of each bit in the specified variable
BITWISE OPERATORS
Operator Action & AND OR | ^ EXCLUSIVE OR (XOR) Ones Compliment ~ Shift right >> Shift left <<
BITWISE OPERATORS
p 0 0 1 1 q 0 1 1 0 p&&q 0 0 1 0 p||q 0 1 1 1 !q 1 1 0 0
? OPERATOR
ternary operator replace certain statement of if-then-else form general form: Exp1 ? Exp2 : Exp3
QUIZ
ENUMERATION: ANSWERS:
1-5. += , -= , *=, /= , %=
6-12. - , + , * , / , & , -- , ++ 13-18. > , >=, < , <=, == , !=
19-21. && , || , !
19-21. Logical Operators 22-28. & , | , ^ , ~ , >> , << 22-27. Bitwise Operators
Sample Program
To display text:
#include<stdio.h> main() { printf(Hello World!);
Sample Program
To display inputted number: #include<stdio.h> main() {
int a; printf(enter a number); scanf(%d, &a); printf(the inputed number is %d, a); }
Conditional Statement
if statement
if (expression) { statement sequence } else { statement sequence }
Conditional Statement
sample program using if statement:
#include<stdio.h> main() { int x = 123; int y; printf(enter your guess); scanf(%d, &y); if (y==x) {printf(Right);} }
Conditional Statement
sample program using if-else statement:
#include<stdio.h> main() { int x = 123; int y; printf(enter your guess); scanf(%d, &y); if (y==x) {printf(Right);} else { printf(Wrong); } }
Conditional Statement
nested if if statement that is the object of either an if or else
#include <stdio.h> main(){ int x=123, y; printf(Enter your guess: ); scanf(%d, &y); if (y!=x)
{ if(y>x) printf(..too high..); else printf(..too low..);} else printf(Its correct!); }
Conditional Statement
if-else-if ladder - common programming construct
if (expression) statements; else if (expression) statements; else if (expression) statements; . . . else statement;
Conditional Statement
sample program using if-else-if ladder:
#include<stdio.h> main() { int x = 123; int y; printf(enter your guess); scanf(%d, &y); if (y==x) {printf(Right);} else if (y>x) { printf(Wrong, too high); } else printf(Too Low); }
Conditional Statement
? alternative - ? operator can be used to replace if/else statement called ternary operator because it requires 3 operands Exp1 ? Exp2 : Exp3
Conditional Statement
sample program using ? alternative:
#include<stdio.h> main() { int x = 123; int y; printf(enter your guess); scanf(%d, &y); if (y==x) {printf(Right);} else y > x ? printf(too high) : printf(too low); }
Conditional Statement
Switch statement often used to process keyboard commands such as menu selection switch (variable) { case constant1: statement sequencebreak; case constant2:statement sequencebreak; . . . default:statement sequence; }
Conditional Statement
the default statement is executed if no matches are found; if not present, no action takes place if all matches fail break statement is optional switch differs from if in that switch can only test for equality whereas the if can evaluate a relational or logical expression no two case constants in the same switch can have identical values; a switch statement enclosed by an outer switch may have constants that the same if character constants are used in the switch, they are automatically converted to their integer values
Conditional Statement
sample program using switch statement:
#include <stdio.h> const int RED = 1; const int GREEN = 2; const int BLUE = 3; void main(){ int color = 1; printf("Enter an integer to choose a color(red=1,green=2,blue=3):\n"); scanf("%d",&color);
switch(color){ case RED: printf("you chose red color\n"); break; case GREEN:printf("you chose green color\n"); break; case BLUE:printf("you chose blue color\n"); break; default:printf("you did not choose any color\n"); } }
Conditional Statement
Nested Switch Statements switch(x) { case 1: switch(y) { case 0: printf(Typed something.\n"); break; case 1: process(x,y); } break; case 2: }
LOOPS
provide a way to repeat commands and control how many times they are repeated while loop - executes a statement repeatedly as long as the controlling expression is true Syntax: while(condition) { statement(s); }
LOOPS
Sample Program (while loop)
#include <stdio.h> main () { int i = 0; while (i<10) { printf ("\n%d", i); i++; } }
LOOPS
Sample Program (while loop) OUTPUT: 0 1 2 3 4 5 6 7 8 9
LOOPS
for loop similar to while but written differently; often used to process lists such as a range of numbers
Syntax: for(expression1; expression2; expression3) { statement(s); }
LOOPS
expression1 initializes variables expression2 conditional expression, as long as this condition is true, the loop will keep executing expression3 modifier which may be simple increment of a variable
LOOPS
Sample Program (for loop)
#include <stdio.h> main () { int i; for (i=0; i<=10; i++) { printf (hello %d\n, i); } }
LOOPS
Sample Program (for loop) OUTPUT: hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9 hello 10
LOOPS
do/while loop allows you to execute code block in loop body at least once
Syntax: do { statement(s); } while(condition);
LOOPS
Sample Program (do/while loop)
#include <stdio.h> main () { int i = 0; do { printf ("\n%d", i); i++; } while (i<10); }
LOOPS
Sample Program (do/while loop) OUTPUT: 0 1 2 3 4 5 6 7 8 9
LOOPS
break terminate a case in a switch statement; force immediate termination of a loop example: #include <stdio.h> main () { int i; for (i=0; i<=10; i++) { printf (hello%d\n, i); if (i==5) break; } }
LOOPS
OUTPUT:
hello0 hello1 hello2 hello3 hello4 hello5
QUIZ
1-3. general format for if-else-if ladder 4-5. general format for ternary operator 6-10. Make a program to find the biggest of 3 inputted numbers. 11-15. Trace the output of the ff. program:
#include<stdio.h> main() {int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("%d",i); }printf("\n"); } }
Answers: 1-3. if (expression) statements; else if (expression) statements; else if (expression) statements; . . . else statement; 4-5. Exp1 ? Exp2 : Exp3
QUIZ
1-3. general format for if-else-if ladder 4-5. general format for ternary operator 6-10. Make a program to find the biggest of 3 inputted numbers. 11-15. Trace the output of the ff. program:
#include<stdio.h> main() {int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("%d",i); }printf("\n"); } } 6-10. #include<stdio.h>main(){ int a,b,c; printf("enter three numbers"); scanf("%d%d%d",&a,&b,&c);if (a>b && a>c){printf("the biggest number is:%d",a);} else if(b>a && b>c) {printf(" the biggest number is:%d",b);} else{printf(" the biggest number is:%d",c);} }
QUIZ
1-3. general format for if-else-if ladder 4-5. general format for ternary operator 6-10. Make a program to find the biggest of 3 inputted numbers. 11-15. Trace the output of the ff. program:
#include<stdio.h> main() {int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("%d",i); }printf("\n"); } }
STRINGS
Strings are stored in memory as ASCII codes of characters that make up the string appended with \0(ASCII value of null)
The last character is the null character having ASCII value zero.
INITIALIZING STRINGS
general format:
char string_name[size]; example: char month[10]; char address[100];
strcmpi(string1,string2);
strlwr(string);
strupr(string);
strrev(string);
QUIZ
1. It is a sequence of characters.
2. General format of declaring a string variable. 3. This function counts and returns the number of characters in a string. 4. This function compares 2 strings but not case sensitive. 5. In this function, you add the characters of one string to the end of other string
QUIZ
6. This function compares two strings (case sensitive)
7. This function converts all characters in a string from uppercase to lowercase. 8. This function converts all characters in a string from lower case to uppercase. 9. This function reverses the characters in a string. 10. This function assigns the contents of one string to another string.
QUIZ
11-15. Write a C program to find the length of the string using strlen() function.
16-20. Write a C program that will concatenate two strings and display the output.
QUIZ
1. It is a sequence of characters. string 2. General format of declaring a string variable. char string_name[size]; 3. This function counts and returns the number of characters in a string. strlen() 4. This function compares 2 strings but not case sensitive. strcmpi() 5. In this function, you add the characters of one string to the end of other string strcat()
QUIZ
6. This function compares two strings (case sensitive) strcmp() 7. This function converts all characters in a string from uppercase to lowercase. strlwr()
8. This function converts all characters in a string from lower case to uppercase. strupr()
9. This function reverses the characters in a string. strrev() 10. This function assigns the contents of one string to another string. strcpy()
QUIZ
11-15. Write a C program to find the length of the string using strlen() function. #include < stdio.h > #include < string.h > void main() { char name[100]; int length; printf(Enter the string); gets(name); length=strlen(name);
QUIZ
16-20. Write a C program that will concatenate two strings and display the output. #include < stdio.h > #include < string.h > void main() { char s1[20],s2[20]; printf(Enter the strings); scanf(%s%s,s1,s2); printf(The concatenation of the strings is %s, strcat(s1,s2)); }
QUIZ
16-20. Write a C program that will concatenate two strings and display the output.
OUTPUT: s1 hi s2 world The concatenation of the strings is hiworld
ARRAYS
its type and dimensions must first be declared array elements start with 0 data is entered into an array using loops size: maximum number of elements that can be stored inside an array
ARRAYS
Syntax: type arrayname[size] = {list value}; example: int n[10]; int n[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
MULTIDIMENSIONAL ARRAYS
"arrays of arrays For example, a bidimensional array can be imagined as a bidimensional table made of elements, all of them of a same uniform data type.
MULTIDIMENSIONAL ARRAYS
int array [3][5];
0 0 1 1 2 3 4
MULTIDIMENSIONAL ARRAYS
array [1][2]
0 0 1 1 2 3 4
MULTIDIMENSIONAL ARRAYS
Multidimensional arrays are not limited to two indices (i.e., two dimensions). They can contain as many indices as needed. But be careful! The amount of memory needed for an array rapidly increases with each dimension.
MULTIDIMENSIONAL ARRAYS
Multidimensional arrays are just an abstraction for programmers, since we can obtain the same results with a simple array just by putting a factor between its indices: int jimmy [3][5]; // is equivalent to int jimmy [15]; // (3 * 5 = 15)
Output:
1 2
2 4
3 6 9
4 8 12
10
3 6
MULTIDIMENSIONAL ARRAYS
We have used "defined constants" (#define) to simplify possible future modifications of the program. For example, in case that we decided to enlarge the array to a height of 4 instead of 3 it could be done simply by changing the line:
#define HEIGHT 3 to: #define HEIGHT 4 with no need to make any other modifications to the program.
Output:
11 14 17 21 24 27 31 34 37 12 15 18 22 25 28 32 35 38 13 16 19 23 26 29 33 36 39
QUIZ
1. What is the size of the array int ict[25]; ? 2. Array elements start with ___ 3. You enter data into array elements using _____ 4. Syntax of One Dimensional Array 5. Syntax of 3D Array
QUIZ
6. Declare an array w/ array name money, size 25, type int 7. Declare the integer variable twenty with elements 3, 4, and 5 8. Declare a define constant day 8 9. In the array hello[2][4][6] , [6] is the ___? 10. They are known as arrays of arrays.
QUIZ
11. Draw a table of an array for integer variable train[3][4]; 12. Using the memory block allocation in #11 let train[0][1] = 1 13. Using the memory block allocation in #11 let train[3][2] = 20 14. Using the memory block allocation in #11 let train[2][3] = 23 15. Using the memory block allocation in #11 let train[3][4] = 17
QUIZ
16-20. Make a code that will display the following output: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
QUIZ
21-25. Trace the output of the following program: #include<stdio.h> #define WIDTH 5 #define HEIGHT 2 int table[HEIGHT][WIDTH]; int x, y; int z=10; int main(){ for (x=0; x<HEIGHT; x++){ for (y=0; y<WIDTH; y++){ table [x][y] = z; printf("array[%d][%d] = %d\n", x, y, table[x][y]); z+=10; } } }
QUIZ
1. What is the size of the array int ict[25]; ? 25 2. Array elements start with ___ 0 3. You enter data into array elements using _____ loops 4. Syntax of One Dimensional Array type arrayname[size] = {list value}; 5. Syntax of 3D Array data_type array_name[s1][s2][s3];
QUIZ
6. Declare an array w/ array name money, size 25, type int int money[25]; 7. Declare the integer variable twenty with elements 3, 4, and 5 int twenty[3][4][5]; 8. Declare a define constant day 8 #define day 8 9. In the array hello[2][4][6] , [6] is the ___? column number 10. They are known as arrays of arrays. multidimensional arrays
QUIZ : 11-15.
int train [3][4];
0 0 1 1 1 20 17 23 2 3
QUIZ 16-20.
#include<stdio.h> main(){ int table[7][5]; int x, y; int z=0; for (x=0; x<7; x++){ for (y=0; y<5; y++){ table [x][y] = z; printf("%d\t", table[x][y]); z++; } printf("\n"); } }
QUIZ 21-25.
array[0][0] = 10 array[0][1] = 20 array[0][2] = 30 array[0][3] = 40 array[0][4] = 50 array[1][0] = 60 array[1][1] = 70 array[1][2] = 80 array[1][3] = 90 array[1][4] = 100