1 / 24

CSC103: Introduction to Computer and Programming

CSC103: Introduction to Computer and Programming. Lecture No 5. Previous lecture. A first C program Comments Single line Multi line Preprocessor directive main() function keywords Program statement. Today’s lecture outline. Escape sequences C compiler Directives Common errors.

elmo
Download Presentation

CSC103: Introduction to Computer and Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC103: Introduction to Computer and Programming Lecture No 5

  2. Previous lecture • A first C program • Comments • Single line • Multi line • Preprocessor directive • main() function • keywords • Program statement

  3. Today’s lecture outline Escape sequences C compiler Directives Common errors

  4. Components of C Program multi-line comment block single line comment block /* C Programming for the Absolute Beginner */ //by Michael Vine #include <stdio.h> main() { printf("\n C you later \n") ; } standard input output library preprocessor directive program statement begin logical program block end logical program block program statement terminator printf function escape sequence

  5. Escape Sequence • Escape sequences are specially sequenced characters used to format output printf("\nC you later\n"); • This printf() function adds two new lines for formatting purposes. • Before text is shown, the program outputs a new line. • After the text is written to standard output.

  6. Escape Sequence \n Go to Program printf("line 1\nline2\nline3\n"); line 1 line 2 line 3 _ printf("C "); printf("for the "); printf("Absolute Beginner\n"); C for the Absolute Beginner _

  7. Escape Sequence \t Go to Program • Escape sequence \t moves the cursor to the next tab space. printf("\nSun\tMon\tTue\tWed\tThu\tFri\tSat\n"); printf("\t\t\t\t1\t2\t3\n"); printf("4\t5\t6\t7\t8\t9\t10\n"); printf("11\t12\t13\t14\t15\t16\t17\n"); printf("18\t19\t20\t21\t22\t23\t24\n"); printf("25\t26\t27\t28\t29\t30\t31\n");

  8. Escape Sequence \r This escape sequence moves the cursor to the beginning of this line to the beginning of this line cursor Go to Program printf("This escape sequence moves the cursor "); printf("to the beginning of this line\r"); printf("This escape sequence moves the cursor \r"); printf("to the beginning of this line");

  9. Escape Sequence \\ c:\cygwin\bin must be in your system path Go to Program • Escape sequence \\ inserts a single backslash \ in your text. • When a program reads a backslash in a printf() function, it expects to see a valid escape character right after it. • Backslash character (\) is a special character in the printf() function; if you need to display a backslash in your text, you must use this escape sequence. printf("c:\\cygwin\\bin must be in your system path");

  10. Escape Sequence \" "This is quoted text" Go to Program Used to to insert a double quote into your outputted text printf("\"This is quoted text\"");

  11. Escape Sequence \' ' A single quote looks like ' Go to Program Used to insert a single quote into your outputted text printf("\n \'A single quote looks like \' \n");

  12. Directives Program statement that begins with the hash sign (#) e.g. #include <stdio.h> When the C preprocessor encounters the hash sign, it performs certain actions depending on the directive that occurs prior to compiling In the above example, I told the preprocessor to include the stdio.h library with my program stdio.h is short for standard input output header file It contains links to various standard C library functions, such as printf().

  13. C Compiler • C program goes through a lot of steps prior to becoming a running or executing program • C compiler performs a number of tasks for you. Most notable are the following • Preprocesses the program code and looks for various directives • Generates error codes and messages, if applicable • Compiles program code into an object code and stores it temporarily on disk • Links any necessary library to the object code and creates an executable file and stores it on disk

  14. How to debug a program If your program compiles, exits, or executes abnormally, there is almost certainly an error (a bug) in your program. Debugging is an art, the more you practice programming the easier debugging will become! Often a program will compile and execute just fine, but with results you did not expect

  15. Example #include <stdio.h> main() { printf("The format issue can be corrected by using"); printf(" the \n and \\ escape sequences"); } Program output (Program 1) The format issue can be corrected by using the and \ escape sequences Go to Program #include <stdio.h> main() { printf("The format issue can be corrected by using"); printf(" the \\n and \\\\ escape sequences"); } Program output (Program 2) The format issue can be corrected by using the \n and \\ escape sequences

  16. Common Error # 1 #include <stdio.h> main() printf("Welcome to C Programming\n"); } Compiling... hello.c hello.c(13) : error C2061: syntax error : identifier 'printf‘ hello.c(13) : error C2059: syntax error : ';‘ hello.c(13) : error C2059: syntax error : 'string‘ hello.c(17) : error C2059: syntax error : '}' Error executing cl.exe. hello.obj - 4 error(s), 0 warning(s) Missing program block identifier If you forget to insert a beginning or a corresponding ending program block identifier ({ or }), you will see error messages

  17. Common Error # 2 #include <stdio.h> main() { printf("Welcome to C Programming\n") } Compiling... hello.c hello.c(17) : error C2143: syntax error : missing ';' before '}' Error executing cl.exe. Missing statement terminator Parse errors occur because the C compiler is unable to determine the end of a program statement such as print statement.

  18. Common Error # 3 #include <sdio.h> main() { printf("Welcome to C Programming\n"); } Compiling... esp_seq.c hello.c(1) : fatal error C1083: Cannot open include file: 'sdio.h': No such file or directory Error executing cl.exe. hello.obj - 1 error(s), 0 warning(s) Invalid preprocessor directive If you type an invalid preprocessor directive, such as miss spell a library name, you will receive an error

  19. Common Error # 4 #include <stdio.h> main() { printf("Welcome to C Programming \m"); } Compiling... esp_seq.c hello.c(13) : warning C4129: 'm' : unrecognized character escape sequence hello.obj - 0 error(s), 1 warning(s) Invalid escape sequence When using escape sequences it is common to use invalid characters or invalid character sequences.

  20. Common Error # 5 #include <stdio.h> main() { */ printf(“hello world \n”) \; /* printf("Welcome to C Programming \n"); } Compiling... test.c test.c(7) : warning C4138: '*/' found outside of comment test.c(7) : error C2059: syntax error : '/‘ test.c(12) : fatal error C1071: unexpected end of file found in comment Error executing cl.exe. test.exe - 2 error(s), 1 warning(s) Invalid Comment Blocks invalid comment blocks generates compile errors

  21. Summary Functions allow to group a logical series of activities, or program statements, under one name Functions can take in and pass back information An algorithm is a finite step-by-step process for solving a problem Each function implementation requires that you use a beginning brace { and a closing brace } Comments help to identify program purpose and explain complex functions /* signifies the beginning of a comment block and */ identifies the end of a comment block

  22. Summary There are 32 words defined as keywords in the standard ANSI C programming language Keywords have predefined uses and cannot be used for any other purpose in a C program Most program statements control program execution and functionality and may require a program statement terminator ; Program statements that do not require a terminator include preprocessor directives, comment blocks, and function headers

  23. printf() function is used to display output to the computer screen Special characters (such as n) when combined with the backslash (\), make up an escape sequence. stdio.h is short for standard input output and contains links to various standard C library functions, such as printf() C compiler preprocess program code, generate error codes and messages if applicable, compile program code into object code, and link any necessary libraries.

  24. Summary Compile errors are generally the result of syntax issues, including missing identifiers and terminators, or invalid directives, escape sequences, and comment blocks A single error at the top of your program can cause cascading errors during compile time The best place to start debugging compile errors is with the first error

More Related