Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Introduction To C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Topic: Introduction to C Programming Language

 Overview of C – efficient, portable, power and flexibility, programmer oriented.


 Language Features – Imperative language, top-down planning, structured
programming and modular design.
 Advantage of using C – Small, fast programs, reliable easy, to learn and
understand
 How to use a modern, cross-platform integrated development environment (MS
Visual Studio Code) to write, edit, and debug.
 Basic C concepts – structure of a program, comments, output using printf, “Hello
World”.
 Makefiles – How to build
 Variables – Declaring and using
 Data Types – int, float, double char and etc.
 Basic Operators – Logical, arithmetic and assignment
 Conditional Statements – Making decisions (if, switch)
 Reoeating code – Looping (for, while, and do-while)
 Arrays – defining and initializing, multi-dimensional.
 Functions – declaration and use, arguments and parameters call by values vs.
call by reference.
 Debugging – call stack, common mistakes, understanding compiler messages.
 Structs – initializing nested structures and variants.
 Character strings – basics, arrays of chars, character operations.
 Pointers – definition and use, using with functions and arrays, malloc, pointer
arithmetic
 The preprocessor - #define, #include
 Input and Output – getchar, scanf
 File input/output – reading and writing to a file, file operations
 Standard c library – string functions, math functions, utility functions, standard
header files.
Course Outcomes
 You will be able to write beginner C programs
 You will be able to write efficient, high quality C code
o Modular
o Low coupling
 You will be able to find and fix your errors
o Understand compiler messages
 You will understand fundamental aspects of the C programming language.
 You will have fun.
Fundamentals of Programming

 Computers are very dumb machines


o They only do what they are told to do.
 The basic operations of a computer will form what is known as the computer’s
instruction set.
 To solve a problem using a computer, you must provide a solution to the problem by
sending instructions to the instruction set.
o A computer program send the instructions necessary to solve a specific
problem.
 The approach or method that is used to solve the problem is known as an algorithm
o So, if we were to create a program that tests if a number is odd or even.
o The statements that solve the problem becomes the program.
o The method that is used to test if the number is even or odd is the algorithm.
 To write a program, you need to write the instructions necessary to implement the
algorithm.
o These instructions would be expressed in the statements of a particular
computer language, such as Java, C++, objective-C or C.
Terminology
 CPU – Central Processing Unit
o Does most of the computing work
o Instructions are executed here.
 RAM – Random Access Memory
o Stores the data of a program while it is running.
 Hard Drive – Stores files that contain program source code, even while the
computer turned off.
 Operating System - Developed to help make it more convenient to use computers.
o A program that controls the entire operation of a computer.
o All input and output.
o Manages the computer resources and handles the execution of programs.
 Fetch / Execute Cycle (life of a CPU)
o Fetches an instruction from memory (using registers) and executes it (loop)
o A gigahertz CPU can do this about a billion times a second.
High-level programming languages make it easier to write programs.
 Opposite of assembly language
 C is a higher level programming language that describes actions in a more
abstract form.
 The instructions (statements) of a program look more like problem solving
steps.
 Do not have to worry about precise steps a particular CPU would have to take
to accomplish a particular tasked
o Total= x + vs. mv ax, 5, mv cx 4 etc…….

Compilers
 A program that translates the high-level language source code into the detailed
set of machine language instructions the computer requires.
 The program does the high-level thinking and the compiler generates the tedious
instructions to the CPU.
 Compilers will also check that your program has valid syntax for the
programming language that you are compiling.
o Finds errors and it reports them to you and doesn’t produce an executable
until you fix them.
 High-level languages are easier to learn and much easier to program than
machine languages.

Writing a Program
 The act of writing a C program can be broken down into multiple steps.
Steps in writing a program
1. Define the program objectives
a. Understand the requirements of the program
b. Get clear idea of what you want the program to accomplish.
2. Design
a. Decide how the program will meet the above requirements
b. What should the user interface be like?
c. How should the program be organized?
3. Write the Code
a. Start implementation, translate the design in the syntax of C
b. You need to use a text editor to create what is called a source code file.
4. Compile
a. Translate the source code into machine code (executable code).
b. Consists of detailed instructions to the CPU expressed in a numeric code.
5. Run the Program
a. The executable file is a program you can run.
6. Test and Debug
a. Just because a program is running, does not mean it works as intended
b. Need to test, to see that your program does what it is supposed to do
(may find bugs)
o Debugging is the process of finding and fixing program errors
o Making mistakes is a natural part of learning.
7. Maintain and Modify the Program
a. Programs are released and used by many people
b. Have to continue to fix new bugs or add new features

For the above steps, you may have to jump around steps and repeat steps
E.g. When you are writing code, you might find that your plan was impractical.
 Many new programmers ignore steps 1 and 2 and go directly to writing code
o A big mistake for larger programs, may be ok for very simple programs
o The larger and more complex the program is, the more planning it requires
o Should develop the habit of panning before coding.
 Also, while you are coding, you always want to work in small steps and constantly
test (Divide and Conquer).
Overview of C programming Language
 C is a general-purpose, imperative computer programming language that
supports structured programming
o Uses statements that change a program’s state, “focuses on how”
In C programming language, you can write Operating System Applications. Also, you
can write general programs.
 Currently, it is one of the most widely used programming languages of all time.
C is a modern Language
 Has most basic control structures and features of modern languages
 Designed for top-down planning
 Organized around the use of functions (modular design) structured programming
 A very reliable and readable program.
 C is used on everything from minicomputers, Unix/Linux systems to pc’s and
mainframes.
 C is the preferred language for producing word processing programs,
spreadsheets and compilers.
 C has become popular for programming embedded systems.
o Used to program microprocessors found in automobiles, camera, DVD
players and etc.
 C has and continues to play a strong role in the development of Linux.
 C programs are easy to modify and easy to adapt to new models or languages.
 In the 1990’s, many software houses began turning to the C++ language for large
programming projects.
 C is a subset of C++ with object-oriented programming tools added
o Any C program is valid C++
o By learning C, you also learn much of C++
 C remains a core skill needed by corporations and ranks in the top10 of desired
skills.
 C provides constructs that map efficiently to typical machine instructions and thus
is used by programs that were previously implement in assembly language
o Provides low-level access to memory
o Requires minimal run-time support.
History of C language

 Was invented in 1972 by Dennis Ritchie of Bell Laboratories


o Ritchie was working on the design of the UNIX operating System.
 It was created as a tool for working programmers
o Main goal is to be a useful language
o Easy readability and writability
 C initially became widely known as the development language of the UNIX
operating system
o Virtually all new major operating systems are written in C and/or C++
 C evolved from a previous programming language named B
o Uses many of the important concepts of B while adding data typing and
other powerful features.
o B was a typeless language – every data item occupied one “word” in
memory, and the burden of typing variables fell on the shoulders of the
programmer.
 C is available for most computers
 C is also hardware independent.
 By the late 1970’s, C had evolved into what is now referred to as “traditional C”.
 The rapid expansion of C working on many different hardware platforms led to
many variations that were similar but often incompatible
o A standard version of C was created (C89/C90, C99, C11).
 A program written only in standard C and without any hardware-dependent
assumptions will run correctly on any platform with a standard C compiler
o Non-standard C programs may run only on a certain platform or with a
particular compiler.
 C89 is supported by current C compilers
o Most C code being written today is based on it.
 C99 is a revised standard for the C programming language that refines and
expands the capabilities of C
o Has not been widely adopted and not all popular C compiler support it.
-Our class will base its examples and concepts of C11-

 C is one of the most important and popular programming languages


 It has grown because people try it and like it
 In the past decade or two, many have moved from C to languages such as C++,
Objective C and Java.
o C is still an important language in its own right, as well a migration path to these
others.

You might also like