Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017"— Presentation transcript:

1 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

2 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

3 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, 

4 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. 

5 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. 

6 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

7 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).

8 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.

9 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.) 

10 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.

11 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.

12 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. 

13 C and C++Tutorial 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


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

Similar presentations


Ads by Google