View
21
Download
0
Category
Tags:
Preview:
DESCRIPTION
The C Language. B. Ramamurthy University at Buffalo bina@buffalo.edu. Introduction. About C language: “.. features economy of expression..”; written for Unix Operating system (1978) The C Language since then has taken a life of it own and has become the foundation for many modern languages. - PowerPoint PPT Presentation
Citation preview
04/19/2023cse321-2014
1
B. RAMAMURTHYUNIVERSITY AT BUFFALO
BINA@BUFFALO.EDU
The C Language
04/19/2023cse321-2014
2
Introduction
About C language: “.. features economy of expression..”; written for Unix Operating system (1978)
The C Language since then has taken a life of it own and has become the foundation for many modern languages.
It has also become a language of choice for RTOS.Reference: The C Programming Language by Kernighan
& Ritchie (available online)We will learn C by repeated spiral mode hands-on
exposure to various elements of the languageWe will also try to work on the Linux system and another
system called Nexos (Next generation embedded operating system) at the CSE department.
04/19/2023cse321-2014
3
Program Structure
A C program is a collection of functions with at least one function called “main”
Here is the classical example that has become a metaphor for a first program in any language.
Hello World: lets compile it and see what happens.#include <stdio.h>int main (){ printf(“Hello World \n”); return 0;}
04/19/2023cse321-2014
4
Processing the C program
Save the program in a file called “hello.c”Compile it using an appropriate compilerCC or cc or gcc or g++ (where G/g stands for
“gnu” organization)Compiler parses the input, checks for syntax
correctness and if syntax is correct generates code;
This code is further linked and loaded to generate the executable.
Source code Compile loader/linker executable code Object code
04/19/2023cse321-2014
5
C program structure
Program
Directives (#include libraries)functions
statements
Different types of statements
Variables/constants
Sequential, assignment, selection, iterative, input/output
04/19/2023cse321-2014
6
Variables and arithmetic expressions
#include <stdio.h>/* print Fahrenheit-Celsius tablefor fahr = 0, 20, ..., 300 */main(){
int fahr, celsius;int lower, upper, step;lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */fahr = lower;while (fahr <= upper) {
celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;}
}
04/19/2023cse321-2014
7
Lets analyze the program
1. #include directive2. Comment // single line comment/* multiple line comment */3. Main function4. Variable declarations {variable type, variable name} int step;5. Initialization: step= 20;6. Statements: computations; arithmetic operations {+, -, *, /, %}7. Repeat computation using a “while loop”8. Condition for repetition9. Output results using “printf”10. Semicolon (;) as a terminator for statements
04/19/2023cse321-2014
8
Variable types
int: integer; for representing whole numbersfloat : floating point or real numbers; for
representing fractional numbers, vary large and very small numbers (32 bits)
double: double precision real number; double the size of float (64 bits)
char: single ASCII (American Standard Code of Information Interchange) character
long: longer integer
04/19/2023cse321-2014
9
Variable names
A sequence of characters used for identifying an entity/item used in the program
Example: partNumvoltage portNummyNameECUNum
04/19/2023cse321-2014
10
Assignment statement
Assignment operator =SyntaxVariable = expresion;partNum = 84560;double temp = 89.5;int age = 78;
pay = salary + bonus;
04/19/2023cse321-2014
11
Arithmetic operators
Addition +Subtraction –Multiplication *Division /Modulus %Precedence of operators:
*, /, % + -
Left to right associavityOverride precedence using ( )
04/19/2023cse321-2014
12
Arithmetic expression
celsius = 5 * (fahr-32) / 9;3 + 5 – 10 * 2 / 3 % 4=3 + 5 -20 /3 % 4= 3 + 5 – 6% 4= 3+5 – 2= 8 – 2= 6
04/19/2023cse321-2014
13
Iteration/repetition
While loopSyntax:Initialize condition; while (condition){ statements; update condition; }Execution semantics
04/19/2023cse321-2014
14
If..else: selection
Often we need to make choices in the execution path.
If ..else statement if (sensedTemp > refTemp) //cool the roomelse if (sensedTemp < refTemp) // heat the room
04/19/2023cse321-2014
15
Multi-way selection
Case or switch statement:switch (grade){ case ‘A’ : printf (“Very good \n”); break; case ‘B’ : printf (“Good\n”); break; case ‘C’ : printf(“not bad\n”); break; case ‘F’: printf(“Bad\n”); default: printf(“Grade out of range \n”);}
04/19/2023cse321-2014
16
Putting it all together
Lets solve the problem below using C.Consider the number game shown in the next
few slides.
cse321-2014
The Number Game (1)
1 3 5 7
9 11 13 15
17 19 21 23
25 27 29 31
04/19/2023
17
cse321-2014
The Number Game (2)
2 3 6 7
10 11 14 15
18 19 22 23
26 27 30 31
04/19/2023
18
cse321-2014
The Number Game (4)
4 5 6 7
12 13 14 15
20 21 22 23
28 29 30 31
04/19/2023
19
cse321-2014
The Number Game (8)
8 9 10 11
12 13 14 15
24 25 26 27
28 29 30 31
04/19/2023
20
cse321-2014
The Number Game (16)
16 17 18 19
20 21 22 23
24 25 26 27
28 29 30 31
04/19/2023
21
cse321-2014
Analysis
What is theory /concept behind this game?How did I arrive at the number you guessed?How can I automate this process?What is the data and what is the algorithm?How can we convey these to a computing machine?While a computer talks binary, we humans write
programs in languages such as Java, C#, C++, Basic etc.
Binary numbers (1’s and 0’s) is the number system used by the computer systems.
We humans use decimal number system that has 10 distinct symbols (0,1,2,3,4,5,6,7,8,9)
Your task: Write a C program to computerize this game.
04/19/2023
22
cse321-2014
1 3 5 7
9 11 13 15
17 19 21 23
25 27 29 31
2 3 6 7
10 11 14 15
18 19 22 23
26 27 30 31
16 17 18 19
20 21 22 23
24 25 26 27
28 29 30 31
4 5 6 7
12 13 14 15
20 21 22 23
28 29 30 31
8 9 10 11
12 13 14 15
24 25 26 27
28 29 30 31
04/19/202323
04/19/2023cse321-2014
24
CH.4 IN KERNIGHAN AND RITCHIE C TEXTBOOK
C Language: Functions
04/19/2023cse321-2014
25
Topics
Purpose of functionsFunction designFunction definitionFunction call
04/19/2023cse321-2014
26
Functions
Functions are modular units that perform a specific operation
It provides the ability to divide the program into coherent modules
Functions can be parameterized providing a general solution that can be customized with specific parameters
Functions offers a method for spreading the code around many files, thus providing a method for organization of code (into libraries, say)
Once defined, a function can be called from anywhere accessible and any number of times resulting in reusability, standardization and uniform application of operations
04/19/2023cse321-2014
27
Function Design
Lets say you want to turn Fahrenheit to Celsius converter into a function rather than dumping all the code in the main function.
fcel
Value in F
Converted value in C
celf
Value in C
Converted value in F
04/19/2023cse321-2014
28
Function Definition
1. Define the prototypeType function name (parameter type, parameter type…);
2. Define the headerReturn type function name (param type name, param type name…)
3. Define the body of the function{ local variable statements one or more return statements}Example:float fcel (float);
float fcel (float fah){ float celsius = (5.0/9.0) * (fah - 32.0); return celsius;}
04/19/2023cse321-2014
29
Function Call
It is not enough defining the function: it needs to be activated.
This is done by calling the function.Calling a function involves specifying its
name and actual parameter values. If there is a return value the call needs to be assigned to a variable.
Examplefloat fahren = 35.0;float celsius = fcel(fahren);
04/19/2023cse321-2014
30
Complete Example
Lets write another function for Celsius to Farenheit.
float celf (float celsi){ float fahr = celsi * 9.0/5.0 + 32.0; return fahr;}
Call: float fa = celf (35.0);
04/19/2023cse321-2014
31
Demos
Lets add an input statement to the functions and complete the example.
Scanf is the input statement. You provide the format(type) as well as the location (address) with name the input will be loaded into.
Example: scanf(“%f”, &celsi);scanf(“%f”, &fahr);
celsi
fahr
04/19/2023cse321-2014
32
Summary
We studied basics of a function design.We learned function definition and function
call.Standard IO using printf and scanf was also
illustrated.Parameter passing by value and reference
was introduced. We will discuss this in detail later.
There is lot more C language: pointers, memory management (allocation, deallocation, etc.) memory leaks, struct, separate compilation, makefile… ( we will discuss all these.)
Recommended