LESSON-1-1.pdf
Document Details
Uploaded by TriumphantSodium310
Related
- PCSII Depression/Anxiety/Strong Emotions 2024 Document
- A Concise History of the World: A New World of Connections (1500-1800)
- Human Bio Test PDF
- University of Santo Tomas Pre-Laboratory Discussion of LA No. 1 PDF
- Vertebrate Pest Management PDF
- Lg 5 International Environmental Laws, Treaties, Protocols, and Conventions
Full Transcript
Programming Fundamentals LESSON 1: TOPIC 2 Output Operations Output Operations Output operations are an essential part of programming as they allow programs to display information to the user. In C programming, displaying output to the screen or console can be achieved using...
Programming Fundamentals LESSON 1: TOPIC 2 Output Operations Output Operations Output operations are an essential part of programming as they allow programs to display information to the user. In C programming, displaying output to the screen or console can be achieved using the printf function. This guide will cover the basics of output operations in C, including displaying output and using printf for formatted output Displaying Output to the Screen/Console To display output to the screen or console in C, you can use the printf function, which stands for "print formatted." It is part of the standard I/O library (stdio.h) and allows you to output text, variables, and other data to the screen. The printf function takes a format string as a parameter, which specifies the text to be displayed and how variables should be formatted and inserted into the string. In the above code, the printf function is used to display the string "Hello, world!" to the screen. The \n at the end represents a newline character, which moves the cursor to the next line. Using printf for Formatted Output One of the powerful features of the printf function is its ability to format output. This allows you to control the appearance of the displayed data, such as specifying the number of decimal places for floating-point numbers or padding strings with spaces. Format specifiers are used within the format string to indicate how the variables should be formatted. BASIC DATA TYPES BASIC FORMAT SPECIFIERS Using \n for Newlines In C, the \n character is used to insert a newline in the output. This special character moves the cursor to the beginning of the next line, allowing you to format your output text neatly. In the above example, each printf statement includes the \n character, ensuring that each piece of text is printed on a new line. The output will be: Programming Fundamentals LESSON 1: TOPIC 3 Basic Escape Sequences Basic Escape Sequences Escape sequences are special character combinations that allow you to represent characters that are difficult to type or have a special meaning within a string. In C programming, escape sequences are used to insert characters that cannot be typed directly into a string. This guide will cover the common escape sequences in C, their usage, and provide examples to illustrate their functionality. Common Escape Sequences Here are some of the common escape sequences in C: v \n: Newline - Moves the cursor to the beginning of the next line. v \t: Tab - Inserts a horizontal tab. v \": Double quote - Inserts a double quote character. v \': Single quote - Inserts a single quote character. v \\: Backslash - Inserts a backslash character. v \b: Backspace - Moves the cursor back one position. v \r: Carriage return - Moves the cursor to the beginning of the current line. v \f: Form feed - Moves the cursor to the next logical page. Usage and Examples Here are some examples that demonstrate the usage of escape sequences in C: In the above code, each printf statement demonstrates the usage of a different escape sequence. The output will display the corresponding special characters and their effects. Programming Fundamentals LESSON 1: TOPIC 4 Placeholders Placeholders When displaying output in C programming, it is common to include variable values within the output statements. Placeholders, also known as format specifiers, allow you to specify where and how the values of variables should be inserted into the output. In C, the printf function provides format specifiers for different data types, allowing you to control the formatting of the displayed values. This guide will cover the usage of placeholders in output statements and the format specifiers available in printf for different data types. Placeholders in Output Statements Placeholders are used within output statements to indicate the position and format of variable values. They are specified by using format specifiers, which start with the % symbol, followed by a letter that represents the data type of the variable. The format specifiers are replaced with the corresponding values when the output is displayed. In the above code, %d is the placeholder for an integer value, and %f is the placeholder for a floating-point value. The values of the variables age and height are inserted into the output at the corresponding placeholders. Format Specifiers in printf for Different Data Types printf provides a range of format specifiers to accommodate different data types. Here are some commonly used format specifiers: %d or %i: Signed integer %u: Unsigned integer %f: Floating-point number %c: Character %s: String %p: Pointer address %x or %X: Hexadecimal number You can also use modifiers with format specifiers to control the width, precision, and other formatting options. Here's an example that demonstrates the usage of format specifiers for different data types: In the above code, the format specifiers %d, %.2f, %c, and %s are used to display the values of variables of different data types. In this example, it demonstrates how to use percision with %f: In this C code, we include the header for standard input and output functions. We define a floating-point variable value = 3.14159265359, and then we use the printf function with %f format specifier to print the value with different precisions. The output will show the number with the specified number of decimal places. Programming Fundamentals LESSON 1: TOPIC 5 Input Operations Input Operations User input is an essential aspect of many programs, as it allows the program to interact with the user and process dynamic data. In C programming, the scanf function is commonly used to accept user input. This guide will explain the process of accepting user input in C, demonstrate the usage of scanf for input operations, and provide guidance on handling user input errors. Accepting User Input in C Programming To accept user input in C, you can use the scanf function from the stdio.h library. scanf allows you to read formatted input from the user and store it in variables. Here's an examples that demonstrate the usage of scanf to accept user input: FOR INTEGER In the above code, the scanf function is used to accept an integer value from the user. The %d format specifier is used to specify that the input should be treated as an integer. The & operator is used to provide the memory address of the variable where the input will be stored. Here's an examples that demonstrate the usage of scanf to accept user input: FOR STRING In the above code, the scanf function is used to accept an string value from the user. The %s format specifier is used to specify that the input should be treated as a string. The & operator is used to provide the memory address of the variable where the input will be stored. The char data type is used to store a single character. THANK YOU!