4 C Language Elements_MAV.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

C Language Elements Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon...

C Language Elements Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming C Library Libraries – C implementations that contain collections of useful functions and symbols that a program may access. Note: A C system may expand the number of operations available by supplying additional libraries. Each library has a standard header file whose name ends with the symbol.h Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming C Library T To open a predefined library, a pair of angle brackets () is used to enclose the header file. Example: #include A pair of double quotes (“”) is used to open a user-defined library. Example: #include “sample.h” Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming #include Directive ❑ Gives program access to a library. ❑ Causes the preprocessor to insert definitions from a standard header file into the program before compilation. ❑ Tells the preprocessor that some names used in the program are found in the standard header file. Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming C Constants Fixed values that may not be altered by the program are also called constants. Examples: 1. Character constants – enclosed between single quotes. Ex. ‘A’, ‘+’ 2. Integer constants – specified as numbers without fractional components. Ex. 5 and -160 3. Floating constants – require the use of decimal point followed by the number’s fractional components. Ex. 16.234 4. String constants – set of characters enclosed by double quotes. Ex. “bag” and “this is good” 5. Backslash character constants – enclosing all character constants in single quotes that works for most printing characters. example: g = ‘\t’ Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming CONSTANTS Constants are data values that cannot be changed during the execution of a program like variables constants have a type. 2 ways to define constants: Syntax: 1. Using the preprocessor #define Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming #define (global declaration to assign macro constant values) ✓ Allows you to make text substitutions before compiling the program. ✓ By convention, all identifiers that are to be changed by the preprocessor are written in capital letters. Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming #define There are several ways to use #define to assign constant value in a variable with varied data types: #define SCORE 100 #define PI 3.14159 #define code ‘A’ #define MSSG “Hello” Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Using the const keyword (local declaration) int main() { const char MSSG=“Hello”; const float PI=3.1459; const int year=2024; const code=‘X’; Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming General Structure of a C Program Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Structure of a C Program //1. Heading section - preprocessor directives #include //2. Executable section int main(void) { //local declarations //C statements or instructions written in sequence return 0; } Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Sample C Program #include int main() { printf("Hello World!\n"); return 0; } Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming main() ✓ Every C program has a main function. This is where program execution begins. ✓ Body- the remaining line of the program in the body. ✓ Curly Braces {} – enclose the body of the function. - indicates the beginning and end of the function main. Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Two sections of function main() 1.Variable Declarations – the part of the program that tells the compiler the names of memory cells in a program needed in the function, commonly data requirements identified during problem analysis. 2.Executable C statements – derived statements from the algorithm into machine language and later executed. Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming C Language Elements Symbols and Punctuations Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming include, goto, pow, sqrt, getch, gets, switch, case, break, etc… Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Escape Sequence Characters Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Identifier An identifier is name that identifies or labels the identity of an object. Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Identifier Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Variable Declaration Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Variable Declaration A statement that communicates to the C compiler the names of all variables used in the program and the kind of information stored in each variable. It also tells how that information will be represented in memory. Syntax: data type variable_list; Examples: int x,age; float sum,a,b; char middle_intial; Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Data Type in C programming Data type is a system for defining various basic properties about the data stored in memory. Properties such as, type of data, range of data, bytes occupied, how these bytes are interpreted processed, stored, and outputted Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Fundamental Operators and Commands Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Increment (++) and Decrement(--) Operators Prefix increment is when the ++ is placed in front of its operand. For this, the value of the expression is the variable’s value after the increment. Prefix decrement is when the -- is placed in front of its operand. For this, the value of the expression is the variable’s value after the decrement. Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Increment (++) and Decrement(--) Operators Postfix increment is when the ++ is placed immediately after the operand. Postfix increment is when the expression’s value is the value of the variable before it is incremented. Postfix decrement is when the -- is placed immediately after the operand. Postfix decrement is when the expression’s value is the value of the variable before it is decremented. Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Increment (++) and Decrement(--) Operators To summarize, prefix increments or decrements a variable value and then uses it, while postfix uses the variable and then increments or decrements its value. Examples: Solve the given math Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Predefined Mathematical Functions The math handling library is All the functions available in this library take double as an argument and return double as the result. Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Example 1: Write a C program that will compute and print the sum, product, difference, quotient and remainder of 2 integers. Example 2: A car travels at a constant speed of 50 kilometers per hour. Input the TIME in minutes that a trip took and output the DISTANCE in kilometers that the car traveled. Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Assignment: Solve the following C programming problems: 1. There are 12 inches in a foot, and an inch is 2.54cm long. Input two distances in feet and inches and output each of their equivalent distance in centimeters. 2. Write a C program that will compute and print the area and perimeter of a rectangle, using the ff. formulas: area_rectangle = length * width perimeter_retangle = 2* (length+width) Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming 3. Write a C program that will asks from the user to INPUT the radius of a circle. Compute and print its Area, Circumference and Diameter. Note: Declare the constant value Pi=3.1416 using the preprocessor #define or the keyword const ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 4. Input the temperature in Fahrenheit and output its corresponding temperature in Celsius using the formula: C= (F - 32 ) * 5/9 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming Myrliza Prepared by: Dr. Cheryl B.Villamor Pantaleon Fundamentals of Programming

Use Quizgecko on...
Browser
Browser