Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2015 C Tutorial CIS5027.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Bill Tucker Austin Community College COSC 1315
Information and Computer Sciences University of Hawaii, Manoa
C++ First Steps.
The need for Programming Languages
Programming what is C++
User-Written Functions
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Chapter 7 User-Defined Methods.
REPETITION CONTROL STRUCTURE
Lesson #6 Modular Programming and Functions.
Introduction to Python
Chapter 2 - Introduction to C Programming
Lesson #6 Modular Programming and Functions.
Debugging and Random Numbers
Quiz 11/15/16 – C functions, arrays and strings
Getting Started with C.
Module 4 Functions – function definition and function prototype.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Chapter 2 - Introduction to C Programming
Programmazione I a.a. 2017/2018.
Engineering Innovation Center
Lesson #6 Modular Programming and Functions.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Arrays, For loop While loop Do while loop
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Functions, Part 1 of 3 Topics Using Predefined Functions
PHP.
Introduction to Primitive Data types
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Coding Concepts (Basics)
Classes and Objects.
Chapter 2 - Introduction to C Programming
CPS120: Introduction to Computer Science
CISC124 Labs start this week in JEFF 155. Fall 2018
Lesson #6 Modular Programming and Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
Chapter 2 - Introduction to C Programming
Programming Languages and Paradigms
Functions, Part 1 of 3 Topics Using Predefined Functions
Introduction to C Programming
CPS125.
Introduction to Primitive Data types
Switch Case Structures
Presentation transcript:

Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017 C Tutorial CIS5027 Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017

What is C? C is a structured, procedural programming language C has been widely used both for operating systems and applications It has become one of the most widely used programming languages of all time Many later languages have borrowed directly or indirectly from C, including  C++, Java, JavaScript, C#, Perl, PHP, Python, … C was originally developed by Dennis Ritchie between 1969 and 1973 at AT&T Bell Labs,and used to (re-)implement the Unix operating system.It has since become one of the most widely used programming languages of all time

Main function in C Every full C program begins inside a function called "main“ A function is simply a collection of commands that do "something" The main function is always called when the program first executes From main, we can call other functions, 

Simple Example To access the standard functions that comes with your compiler, you need to include a header with the #include directive The #include is a "preprocessor" directive that tells the compiler to put code from the header called stdio.h into our program before actually creating the executable. By including header files, you can gain access to many different functions--both the printf and getchar functions are included in stdio.h.  The next important line is int main(). This line tells the compiler that there is a function named main, and that the function returns an integer, hence int. The "curly braces" ({ and }) signal the beginning and end of functions and other code blocks. The printf function is the standard C way of displaying output on the screen. The quotes tell the compiler that you want to output the literal string as-is (almost). The '\n' sequence is actually treated as a single character that stands for a newline  Notice the semicolon: it tells the compiler that you're at the end of a command, such as a function call. The next command is getchar(). This is another function call: it reads in a single character and waits for the user to hit enter before reading the character. This line is included because many compiler environments will open a new console window, run the program, and then close the window before you can see the output. This command keeps that window from closing because the program is not done yet because it waits for you to hit enter. Including that line gives you time to see the program run.  Finally, at the end of the program, we return a value from main to the operating system by using the return statement. This return value is important as it can be used to tell the operating system whether our program succeeded or not. A return value of 0 means success. 

Variables in C There are several different types of variables: char int float <variable type> <name of variable> In programming, input and data are stored in variables. There are several different types of variables; when you tell the compiler you are declaring a variable, you must include the data type along with the name of the variable. Several basic types include char, int, and float. Each type can store different types of data.  A variable of type char stores a single character, variables of type int store integers (numbers without decimal places), and variables of type float store numbers with decimal places. Each of these variable types - char, int, and float - is each a keyword that you Before you can use a variable, you must tell the compiler about it by declaring it and telling the compiler about what its "type" is. To declare a variable you use the syntax <variable type> <name of variable> While you can have multiple variables of the same type, you cannot have multiple variables with the same name. Moreover, you cannot have variables and functions with the same name.  A final restriction on variables is that variable declarations must come before other types of statements in the given "code block" (a code block is just a segment of code surrounded by { and }). So in C you must declare all of your variables before you do anything else use when you declare a variable. Some variables also use more of the computer's memory to store their values. 

Arrays in C syntax for declaring an array: For example: Arrays are useful critters that often show up when it would be convenient to have one name for a group of variables of the same type that can be accessed by a numerical index. This would make an integer array with 100 slots (the places in which values of an array are stored) will allow you to declare a char array of 100 elements, or slots. Then you can receive input into it from the user, and when the user types in a string, it will go in the array, the first character of the string will be at position 0, the second character at position 1, and so forth. It is relatively easy to work with strings in this way because it allows support for any size string you can imagine all stored in a single variable with each element in the string stored in an adjacent location

If statements in C Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false. An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. nested if statements You can use one if or else if statement inside another if or else if statement(s).

Loops in C Loops are used to repeat a block of code for while do … while DO..WHILE - DO..WHILE loops are useful for things that want to loop at least once. You may encounter situations, when a block of code needs to be executed several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. 1) while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. 2)for loop Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. 3)do...while loop It is more like a while statement, except that it tests the condition at the end of the loop body. 4)nested loops You can use one or more loops inside any other while, for, or do..while loop.

Functions in C functions are blocks of code that perform a number of pre-defined commands to accomplish something productive In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive. You can either use the built-in library functions or you can create your own functions. Functions that a programmer writes will generally require a prototype. Just like a blueprint, the prototype gives basic structural information: it tells the compiler what the function will return, what the function will be called, as well as what arguments the function can be passed. When I say that the function returns a value, I mean that the function can be used in the same manner as a variable would be. For example, a variable can be set equal to a function that returns a value between zero and four.   Return is the keyword used to force the function to return a value. Note that it is possible to have a function that returns no value. If a function returns void, the return statement is valid, but only if it does not have an expression. In other words, for a function that returns void, the statement "return;" is legal, but usually redundant. (It can be used to exit the function before the end of the function.) 

Function example in C A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions. You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task. A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. Return Type − A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void. Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature. Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters. Function Body − The function body contains a collection of statements that define what the function does.

Recursive Function in C  recursion is when a function calls itself. Every recursion should have the following characteristics. A simple base case which we have a solution for and a return value. Sometimes there are more than one base cases. A way of getting our problem closer to the base case. I.e. a way to chop out part of the problem to get a somewhat simpler problem.  A recursive call which passes the simpler problem back into the function. That is, in the course of the function definition there is a call to that very same function.

Recursive Function Example in C /* Fibonacci: recursive version */ int Fibonacci_R(int n) { if(n<=0) return 0; else if(n==1) return 1; else return Fibonacci_R(n-1)+Fibonacci_R(n-2); } /* iterative version */ int Fibonacci_I(int n) int previous = 1; int current = 1; int next = 1; for (int i = 3; i <= n; ++i) next = current + previous; previous = current; current = next; return next; recursion is when a function calls itself. That is, in the course of the function definition there is a call to that very same function. 

C and C++Tutorial http://www.cprogramming.com/tutorial/c-tutorial.html http://www.cprogramming.com/tutorial/c++-tutorial.html C++, as the name suggests, is a superset of C. As a matter of fact, C++ can run most of C code while C cannot run C++ code C follows the procedural programming paradigm while C++ is a multi paradiagram language(procedural as well as object oriented) C is a low-level language (difficult interpretation & less user friendly, concentration on whats going on in the machine hardware) while C++ is a middle-level language C is function-driven while C++ is object-driven