Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

C Programming Lesson 1

C is a programming language developed in the early 1970s at Bell Labs. It was designed to be portable, efficient, and allow for modular programming. A simple C program includes preprocessor commands, functions, variables, statements, and comments. The program is compiled into an executable that can be run to output "Hello World".

Uploaded by

rajeev0305
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

C Programming Lesson 1

C is a programming language developed in the early 1970s at Bell Labs. It was designed to be portable, efficient, and allow for modular programming. A simple C program includes preprocessor commands, functions, variables, statements, and comments. The program is compiled into an executable that can be run to output "Hello World".

Uploaded by

rajeev0305
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Introduction to C

 KIIT 2014
Objectives
After completing this lesson, you should be able to do the following:
• Define C language
• List the capabilities of C language
• Define Software Development Method
• Development with C
• Know the types of Languages
• Write Simple C Program
• Use of escape sequence
• Define variable
• Use of arithmetic operators

 KIIT 2014
Introduction to C
• C is a high level language written by Brian Kernighan
and Dennis Ritchie at Bell Labs
• C language was initially designed to make UNIX
operating system. Hence it became the first
"portable" language

Note: In recent years C has been used as a general-purpose language because of


its popularity with programmers.

 KIIT 2014
Why Use C?
• Mainly because it produces code that runs nearly as fast as code
written in assembly language. Some examples of the use of C might
be:
– Operating Systems
– Language Compilers
– Assemblers
– Text Editors
– Network Drivers
– Modern Programs
– Data Bases
– Language Interpreters
– Utilities

 KIIT 2014
 KIIT 2014
History of C
• Evolved from two previous languages
– BCPL , B
• BCPL (Basic Combined Programming Language) used
for writing OS & compilers
• B used for creating early versions of UNIX OS
• Both were “typeless” languages
• C language evolved from B (Dennis Ritchie – Bell
labs)
** Typeless – no datatypes. Every data item occupied 1 word in memory.

 KIIT 2014
History of C
• In 1972 Dennis Ritchie at Bell Labs writes C and in
1978 the publication of The C Programming
Language by Kernighan & Ritchie caused a revolution
in the computing world
• In 1983, the American National Standards Institute
(ANSI) established a committee to provide a modern,
comprehensive definition of C. The resulting
definition, the ANSI standard, or "ANSI C", was
completed late 1988.

 KIIT 2014
Capabilities of C
• Low Level Language Features
• Portability
• Powerful
• Bit Manipulation
• High Level Language Features
• Modular Programming
• Efficient use of Pointers

 KIIT 2014
Software Development Method
• Requirement Specification
– Problem Definition
• Analysis
– Refine, Generalize, Decompose the problem definition
• Design
– Develop Algorithm
• Implementation
– Write Code
• Verification and Testing
– Test and Debug the code

 KIIT 2014
Development with C
• Four stages
– Editing: Writing the source code by using some IDE or editor
– Preprocessing or libraries: Already available routines
– Compiling: translates or converts source to object code for a specific
platform source code -> object code
– Linking: resolves external references and produces the executable
module

• Portable programs will run on any machine

Note: Program correctness and robustness are most important than program
efficiency

 KIIT 2014
Programming languages
• Various programming languages
• Some understandable directly by computers
• Others require “translation” steps
– Machine language
• Natural language of a particular computer
• Consists of strings of numbers(1s, 0s)
• Instruct computer to perform elementary operations one at
a time
• Machine dependant

 KIIT 2014
Programming languages
• Assembly Language
– English like abbreviations
– Translators programs called “Assemblers” to convert assembly
language programs to machine language
– E.g. add overtime to base pay and store result in gross pay

LOAD BASEPAY

ADD OVERPAY

STORE GROSSPAY

 KIIT 2014
Programming languages
• High-level languages
– To speed up programming even further
– Single statements for accomplishing substantial tasks
– Translator programs called “Compilers” to convert high-
level programs into machine language
– E.g. add overtime to base pay and store result in gross pay
grossPay = basePay + overtimePay

 KIIT 2014
C Standard Library
• Two parts to learning the “C” world
– Learn C itself
– Take advantage of rich collection of existing functions
called C Standard Library
• Avoid reinventing the wheel
• SW reusability

 KIIT 2014
Basics of C Environment
• C systems consist of 3 parts
– Environment
– Language
– C Standard Library
• Development environment has 6 phases
– Edit
– Pre-processor
– Compile
– Link
– Load
– Execute

 KIIT 2014
Basics of C Environment
Program edited in
Editor and stored
Phase 1 Editor Disk on disk

Preprocessor
program processes
Phase 2 Preprocessor Disk the code

Creates object code


and stores on disk
Phase 3 Compiler Disk

Links object code


with libraries and
Phase 4 Linker Disk stores on disk

 KIIT 2014
Basics of C Environment
Primary memory
Puts program in
memory
Phase 5 Loader

Primary memory
Takes each instruction
and executes it storing
Phase 6 CPU new data values

 KIIT 2014
C Program Example
A C program basically consists of the following parts:
• Preprocessor Commands
• Functions
• Variables
• Statements & Expressions
• Comments

 KIIT 2014
Simple C Program
/* A first C Program*/
#include <stdio.h>
void main()
{
printf("Hello World \n");
}

 KIIT 2014
Simple C Program
• Line 1: #include <stdio.h>
– As part of compilation, the C compiler runs a program
called the C preprocessor. The preprocessor is able to add
and remove code from your source file.
– In this case, the directive #include tells the preprocessor
to include code from the file stdio.h.
– This file contains declarations for functions that the
program needs to use. A declaration for the printf function
is in this file.

 KIIT 2014
Simple C Program
• Line 2: void main()
– This statement declares the main function.
– A ‘C’ program can contain many functions but must always
have one main function.
– A function is a self-contained module of code that can
accomplish some task.
– Functions are examined later.
– The "void" specifies the return type of main. In this case,
nothing is returned to the operating system.

 KIIT 2014
Simple C Program
• Line 3: {
– This opening bracket denotes the start of the program.

 KIIT 2014
Simple C Program
• Line 4: printf("Hello World \n");
– printf is a function from a standard C library that is used to
print strings to the standard output, normally your screen.
– The compiler links code from these standard libraries to the
code you have written to produce the final executable.
– The "\n" is a special format modifier that tells the printf to
put a line feed at the end of the line.
– If there were another printf in this program, its string would
print on the next line.

 KIIT 2014
Simple C Program
• Line 5: }
– This closing bracket denotes the end of the program.

 KIIT 2014
Compile & Execute C Program
Following are the simple steps:
• Open a text editor and add the above-mentioned code.
• Save the file as hello.c
• Open a command prompt and go to the directory where you saved
the file.
• Type gcc hello.c and press enter to compile your code.
• If there are no errors in your code, the command prompt will take
you to the next line and would generate a.out executable file.
• Now, type a.out to execute your prog ram.
• You will be able to see "Hello World" printed on the screen

 KIIT 2014
Summary
In this lesson, you should have learned how to:
• Define C language
• List the capabilities of C language
• Define Software Development Method
• Develop software with C
• Write programs in different types of Languages
• Write Simple C Program
• Compile & Execute C Program

 KIIT 2014

You might also like