180 likes | 304 Vues
Dive into the fundamentals of C programming with an emphasis on string literals and escape sequences. Discover how to utilize double quotes for string literals, implement escape sequences to manage special characters, and maintain proper syntax in your code. Learn the structure of a C program, including the importance of the main function and correct statement termination. This guide also highlights debugging techniques for identifying and fixing common syntax, run-time, and logic errors. Elevate your programming skills with these essential concepts!
E N D
Formatting Output #include <stdio.h> void main (void) { printf(“Welcome to”); printf(“London!”); printf(“\nHow do we\njump\n\ntwo lines?\n”); printf(“\n”); printf(“It will rain\ntomorrow\n”); }
Output Welcome toLondon! How do we Jump two lines? It will rain tomorrow
What if I want to print a \ or “ printf(“How to print a \\ backslash\n”); printf(“How to print a \” quote\n”);
C String Literals • Anything inside double quotes • Use \ to break string literal across a line printf(“This shows how to break \ a string literal across lines\n”); printf(“This shows how to break “ “a string literal across lines\n”);
More Escape Sequences (2.4) • Consist of a backslash followed by a letter, symbol, or combination of digits • Represents a character that has special meaning or specifies an action
Character Escape Sequences • \0 Null character • \a Alert/bell • \n New Line • \0ddd Octal constant • \xddd Hexadecimal constant • \Xddd Hexadecimal constant
Character Escape Sequences • \\ Display a backslash • \’ Display a single quote • \” Display a double quote • \% Display a percent character • \? Display a question mark
Review of Chapter 2 • Basic structure of a C program • Writing comments • Using character escape sequences • Displaying special characters • C string literals • Basic debugging techniques
What are the important features of a C program • The main function name must be main • The prog body must start with { • The prog body must end with } • A C statement must end with ; • A C statement is case sensitive • A C statement is location insensitive
More C program features • In general, it is OK to add blank(s) between tokens in a C statement but it is not OK to add blank(s) within a token • C uses character escape sequencs for special characters and action • Make your comments stand out. Do not hide them.
What is debugging • Fixing problems that cause your program to behave unexpectedly. • 3 types of programming errors • Syntax errors • Run-time errors • Logic errors
Syntax Errors • Violation of C “grammer” rules • Typographical mistakes • Compile time errors
Run-time errors • Semantic errors (errors of meaning) • Not detected by compiler • Violation of the rules during execution of your program
Logic errors • Most difficult errors to find • Program compiles and runs fine it just does not produce the correct results. • Is the input data correct?
How to reduce programming errors • Writing your program neatly • Adding blank lines at natural locations • Lining up your opening and closing braces • Add useful comments • Build your program in sections (layers)
How to debug your program • Think about debugging up front before you write your program • Write your program in layers • Look for the obvious syntax errors • Correct function names • Statement termination • Are my parentheses and braces matched up
Lets do a debugging example • Program 2.2 Debugging (Page 64)