Introduction to C Programming PDF
Document Details
Uploaded by CourageousMossAgate2513
Southern Luzon State University
Tags
Summary
This document provides an introduction to C programming. It covers basic concepts like syntax, simple programs, and common functions such as `printf`. The document also discusses important concepts like comments, operators, and keywords.
Full Transcript
Introduction to C Programming Objectives Write simple C programs. Use simple input and output statements. Use the fundamental data types. Learn computer memory concepts. Use arithmetic operators. Learn the precedence of arithmetic operators. Write simple decision making statements....
Introduction to C Programming Objectives Write simple C programs. Use simple input and output statements. Use the fundamental data types. Learn computer memory concepts. Use arithmetic operators. Learn the precedence of arithmetic operators. Write simple decision making statements. Begin focusing on secure C programming practices. How to create a Simple C Program using printing a line text The C language facilitates a structured and disciplined approach to computer-program design. Example No. 1. Hello World Display 1. // Fig. 2.1: fig02_01.c 2. // A first program in C. 3. #include 4. 5. // function main begins program execution 6. int main( void ) 7. { 8. printf ( "Hello World!\n" ); 9. } // end function main Comments - is begin with // to improve program readability - do not cause the computer to perform any action when the program is run and they are ignored by the C compiler. - it help other people read and understand your program - // the shorter and they eliminate common programming errors that occurs on multi-line comments represented by Preprocessor Directive - line begin with # and tells the preprocessor to include the contents of standard input/output header() in the program - header contains information used by the compiler when compiling calls to standard inoput/output library functions such as printf. Syntax: #include White space - this character is normally ignored by the compiler. The main functions Syntax: int main (void) Function - is the parenthesis after the main indicate that main is a program building block. - it can return information. - it can receive information when they are called upon to execute. Output Statement Syntax: printf(“Hello World!\n”); String - it sometimes called character string, message or literal. Statement - the entire line, including the printf, its argument within the parenthesis and the semicolon. Argument - it is within the parenthesis and the semicolon(;). Semicolon - is the statement terminator Escape Sequences Escape character - is the backslash (\) Common Escape Sequences Escape Description Sequence \n Newline. Position the cursor at the beginning of the next line. \t Horizontal tab. Move the cursor to the next tab stop. \a Alert. Produces a sound or visible alert without changing the current cursor position. \\ Backslah. Insert a backslash character in a string. \” Double quote. Insert double-qoute character variables - locations in memory where values can be stored for use by a program. identifier - is a series of characters consisting of letters, digits and underscores ( _ ) that does not begin with a digit. Arithmetic in C C Operation Arithmetic Algebraic C Expression Operator Expression Addition + F+7 F+ 7 Subtraction - P-c P-c Multiplicatio * Bm b*m n Division / x/y x/y Remainder % R mod s R%s Rules of operator precedence for the operators Operator( Operation Order of evaluation (precedence) s) (s) () Parentheses Evaluated first. If the parentheses are nested, thew expression in the innermost pair is evaluated first. If there are several pairs of parentheses “on the same level” (i.e. not nested), they’re evaluated left to right. * Multiplicatio Evaluated second. If there are several, they’re / n evaluated left to right % Division Remainder + Addition Evaluated third. If there are several, they’re evaluated left to right. Decision Making: Equality and Relational Operators Algebraic C equality or Example of C Meaning of C equality or relational Condition condition relational operator operator Relational operators > > X>Y X is greater than Y < < X >= X >= Y X is greater than or equal to Y Equality operators = == X == Y X is equal to Y ‡ != X != Y X is not equal Y Flow of control is order of statement to execute. Keywords or Reserved Words auto do goto signed unsigned break double if sizeof void case else int static volatile char enum long struct while const extern register switch continue float return typedef Default for short union