C Programming Lecture Notes PDF
Document Details
Uploaded by ContrastyHeliotrope3305
Purdue Polytechnic
2024
Tawfiq Salem
Tags
Summary
These lecture notes cover the basics of C programming, including the lifecycle of a C program, comments, the printf function, string literals, escape sequences, and review questions.
Full Transcript
LECTURE 2 FIRST LOOK AT C PROGRAMMING © August 26, 2024 by Tawfiq Salem. All rights reserved. This presentation may not be duplicated or transmitted, in part or in whole, without the express writt...
LECTURE 2 FIRST LOOK AT C PROGRAMMING © August 26, 2024 by Tawfiq Salem. All rights reserved. This presentation may not be duplicated or transmitted, in part or in whole, without the express written permission of the author. Reminders ‣ Inlab01 (This week) We will have our first lab this week. Remember to attend your lab section at the assigned time and location. ‣ Quizzes: We will have our first-in-class quiz next lecture. Be certain to bring your laptop and ensure that your VLab account is properly configured. 2 Previous Lecture ‣ We talked about C History Computer Structure High-level and low-level languages Hello World program in C #include int main() { printf (“Hello World! \n”); return 0; } Previous Lecture: Computer Components Computer System Software Hardware Operating System Processor Linux, Windows, Mac, CPU Android, iOS Applications Software Input devices Keyboard, Mouse Word Doc Presentation Output devices Excel etc Storage Primary, Secondary Review question An algorithm is: A. A solution to a problem that a computer can execute. B. A step-by-step list of instructions that, if followed exactly, will solve the problem under consideration. C. A series of instructions implemented in a programming language. D. A special kind of notation used by computer scientists. 5 Review question An algorithm is: A. A solution to a problem that a computer can execute. B. A step-by-step list of instructions that, if followed exactly, will solve the problem under consideration. C. A series of instructions implemented in a programming language. D. A special kind of notation used by computer scientists. 6 Review Questions Which of the following are examples of application software? A. Facebook app for Android B. Apple iOS C. Linux D. MS Word 7 Review Questions Which of the following are examples of application software? A. Facebook app for Android B. Apple iOS C. Linux D. MS Word 8 Review Questions ‣ Which of the following is a commonly used measure for memory size in a computer system? A. GB B. RPM C. GHz D. Gb/s 9 Review Questions ‣ Which of the following is a commonly used measure for memory size in a computer system? A. GB B. RPM C. GHz D. Gb/s 10 Review Questions ‣ Which of the following is equivalent to 1MB A. 10KB B. 1024GB C. 1024KB D. 1024Bytes 11 Review Questions ‣ Which of the following is equivalent to 1MB A. 10KB B. 1024GB C. 1024KB D. 1024Bytes 12 Review Questions ‣ What is the extension of a C programming file ? A..c B..c++ C..py D..ja E. None of the Above 13 Review Questions ‣ What is the extension of a C programming file ? A..c B..c++ C..py D..ja E. None of the Above 14 Review Questions What does ‘// ' mean in C programming? A. Converts string to int B. Insert a new line C. Comment line D. Print output 15 Review Questions What does ‘// ' mean in C programming? A. Converts string to int B. Insert a new line C. Comment line D. Print output 16 Today’s Objectives: ‣ Life Cycle of C Program ‣ Comments in C ‣ Printf function 17 Create a new project on VS 2022 ‣ Open Visual Studio 2022. ‣ Select "Create a new project." 18 Create a new project on VS 2022 ‣ Choose "C++" as the programming language. 19 Create a new project on VS 2022 ‣ Select "Empty Project" from the available templates. 20 Create a new project on VS 2022 ‣ Enter a name for your project and specify the location where you want to save it. ‣ Click "Create" to set up your new project. 21 A Simple “C” program: #include int main() { printf (“Hello World! \n”); return 0; } 22 Life Cycle of a C program: Pre-processor Source Code Object File (example.c) Compiler (example.obj) Include Files (stdio.h) Linker Executable File (example.exe) Runtime Library 23 Source Code ‣ Is the computer program written by a programmer. ‣ The source code is saved as a file to the disk. ‣ The extension of the source file for a C program is.c #include int main() { printf (“Hello World! \n”); return 0; } 24 Preprocessor Preprocessor, is a software that goes through the “C” program before compilation. It detects all the lines in the code that start with # Preprocessor directives start with # Ex: #include 25 What is a Standard Library? ‣ The “C” Compiler comes with standard libraries. ‣ Each library is a collection of functions. ‣ Each library has a header file with.h extension. Ex: stdio.h ‣ To use functions in a library, its header file should be included in the program. Ex: #include #include 26 Comments in C ‣ You can place comments in your source code that are not executed as part of the program. ‣ Comments provide clarity to the C source code allowing others to better understand what the code was intended to accomplish and greatly helping in debugging the code.\ Syntax The syntax for a comment is: OR OR // comment goes here 27 Keywords = Reserved Words Examples: return main include int 28 printf ( ) function: ‣ Is a function in stdio library. ‣ It writes to the standard output (monitor). Syntax: printf (“Hello World!”); 29 String Literal (string constant) ‣ A stream of characters enclosed in double quotes. Ex: “C programming is fun!” “CNIT 105” 30 Escape Sequence Character Use it inside the format string in printf( ) to format the output. \n inserts a new line in the output \t inserts a tab \\ displays \ \” displays ” Ex: Display Hallo World! In 2 lines to the screen: printf (“Hello\nWorld!”); 31 Exercise-1: Print One, Two, Three in 3 lines: a) Using 3 printf( ). b) printf(“one”) c) printf(“\nTwo”) d) printf(“\nThree”) e) Using one printf() 32 Exercise-2 ‣ Write a program to print out the following star pattern using one print statement. * ** *** **** ***** ****** 33 Few things about a “C” Program: It must have a function named main( ). Program execution begins with the main() function. Every program statement must end with a semicolon ; The code in the function is enclosed between{…}. C is a case sensitive language. C is not an Object-Oriented Language #include int main() { printf (“Hello World! \n”); return 0; } 34 Reading data into a variable scanf() Function: Reads data into a variable. scanf (“format modifiers”, &variable); &variable, is the address of the variable in the RAM Ex: int age; printf (“Enter your age:”); // Prompt the user scanf (“%d”, &age); // read Note: When you use scanf function you need to put the following line at the top of the code. #define _CRT_SECURE_NO_WARNINGS 35 The end! ‣ Don’t hesitate to visit office hours! ‣ Next lecture: Variables & data types (Next lecture) scanf() function (Next Lecture) Thank You!