Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 38

Programming With C/C++

Chapter 1: Basics of the Basics

Dr. Umair Ali Khan


Associate Professor & Chairman,
CSE Department, QUEST, Nawabshah
Email: umair.khan@quest.edu.pk
Website: https://umair-khan.quest.edu.pk
About Me
Course Slides
• My website
– https://umair-khan.quest.edu.pk
– Downloads  C Programming
Course Trainers
• Dr. Umair Ali Khan
– Assoc., Prof., & Chairman, CSE Dept.,
– IT Coordinator EDC
• Prof. Dr. Adnan Manzoor Rajper
– Professor, IT Dept.,
– Director EDC
• Dr. Adnan Ahmed Arain
– Assoc., Prof., Telecom Dept.,
– Deputy Director EDC
• Dr. Ubaidullah Rajput
– Assoc., Prof., CSE Dept.,
– Manager EDC
Contents
• Computer language
• Types of computer languages
• Introduction to C language
• Why use C language?
• C development system
• Files used in C program development
• Writing a program in C language
• Saving and compiling the program in C language
• Processing a high level language program
• The basic structure of C program
Computer Language
Types of Computer Languages
HL / LL / ML comparison
Introduction to C Language

• C, a high-level language programming language was


developed in early 1970s by Dennis Ritchie at Bell
Laboratories.
• Over the years, the power and flexibility of C, together with
the availability of high quality C compilers for computers of
all sizes, have made it a popular language in industry for a
wide variety of applications.
C Vs. C++
C C++
When compared to C++, C is a subset of C++. C++ is a superset of C. C++ can run most of C
code while C cannot run C++ code.
C supports procedural programming paradigm C++ supports both procedural and object
for code development. oriented programming paradigms.
No support for polymorphism, encapsulation, Supports polymorphism, encapsulation, and
and inheritance. inheritance.
Function driven Object driven
No support for function and operator Supports function and operator overload
overloading
C uses functions for input/output. For C++ uses objects for input output. For
example scanf and printf. example cin and cout.
Data is hidden by the Encapsulation to ensure
that data structures and operators are used as
C does not support information hiding. intended.
File format: .c File format: .cpp
Why Use C/C++ Language?

1. Popularity
2. Convenience of a high level language and speed of a low-level
language
3. Much closer control of a computer's hardware and peripherals
4. Fast code
5. Easy to understand and maintain code / syntax
Real-World Applications of C
• Operating systems
– Unix, MS Windows, Apple Mac, Symbian, Android, etc
• Development of new languages
– C#, Java, Limbo, JavaScript, Perl, PHP, Python, Verilog
• Computation Platforms
– MATLAB, Mathematicia
• Embedded Systems
Real-World Applications of C++
• Games
– Counter Strike, Doom, World of WarCraft, Birthright, Hellfire, Football, etc
• Web Browsers
– Google chrome, Mozilla Firefox, Internet Explorer, Safari, Opera, etc.
• Office Products
– MS Office, Apache OpenOffice, etc.
• Websites
– Facebook, YouTube, Amazone, Paypal
• Database Systems
– Oracle, MySQL, MS SQL Server, SAP DB, etc.
C/C++ Integrated Development Environment
• Turbo C
• Dev C++
• Eclipse
• Netbeans
• Code::Blocks
• GNAT programming studio
• Visual studio
• CodeLite
• Qt Creator
• Sublime Text
• C++ Builder
• CLion
• MonoDevelop
Turbo C - IDE
Dev-C++ IDE
C Compiler
C Development Envionment
C Execution Envionment
Files used in C Program Development

1. Programmer-Generated Files (.c, .cpp)


2. Header Files (.h)
3. Object files (.obj)
4. Library Files (.lib, .dll)
5. Executable Files (.exe)
Introduction to Dev-C++ IDE
Structure of a C Program
Comments in C
Pre-processor Directive: #include
<stdio.h>
• Name of standard library file, stands for STanDard Input
Output.
• .h is extension called header file. Header file contains the
information used by compiler when compiling library
function.
• Stdio.h contains the functions that we want to use (e.g in our
program printf( ) function).
printf ( )
• Instructs the compiler to perform an action i-e to print on the
screen the string of characters marked by quotation marks (“
”).
• Entire line including pritnf( ), its argument within parenthesis
and semicolon is called “statement”.
Semicolon ; - statement terminator

• Tells the compiler where a given statement ends.

• Every statement must end with semicolon.

• Also known as statement terminator.


Saving the Program in C Language

• After you have typed in the source file for the program, you
should save it on your disk or hard disk. To do this, select the
Save option from the File menu. You can also accomplish the
same effect simply by pressing the Ctrl+s key combination.
• You can save your source file as first.c or first.cpp.
• Its a good idea and programming practice to save your source file
before compiling and running your program.
Compiling and Making an .exe file
• After you have written the source file for your program, you need to
turn it into an executable file. The compiler, which is a part of the
IDE, translate the source code into another file known as (.obj)
Object file, consisting of the machine language.
• The linker then links the entire necessary object files together to
produce a final executable program with the extension (.exe).
• Compiling and linking can be performed together in one step
Exploring the printf ( ) Function
• The printf( ) function uses a unique format for printing constants
and variables. For example

void main (void)


{
printf ( “ I am %d years old.”, 20 ) ;
}
 Output:
I am 20 years old.
Exploring the printf ( ) Function
• The function printf( ) can be given more than one argument. For
example in the above program we have taken 2 arguments in the
printf( ) function.
• The two arguments are separated by a comma.
• The printf( ) function takes the vales on the right of the comma
and plugs it into the string on the left.
– Why we are using it ?
– Why does it plug in it ?
– Where it finds a format specifier such as %d ?
Format Specifier
• The format specifier tells the printf( ) where to put a value in a
string and what format to use in printing the values.
– In the previous program, the %d tells the printf( ) to print the value 20 as a
decimal integer.
• Then what will be the method of writing a character or floating
point number?
• Why not simply put the decimal integer into the original string as
compared to the format specifier?
List of Format Specifier for printf ( )
• %c single character
• %s string
• %d signed decimal integer
• %f floating point ( decimal notation )
• %e floating point ( exponential notation )
• %u unsigned decimal integer
• %x unsigned hexadecimal integer
• %o unsigned octal integer
• l prefix used with %d, %u, %x, %o to specify long
integer, for example, %ld.
Using Format Specifier in printf ( )
void main (void)
{
printf ( “ My name is %s and I am %d years old.”,
“Ahmed”, 20 );
}
 
• Output:
My name is Ahmed and I am 20 years old.
Using Format Specifier in printf ( )
void main (void)

{
printf ( “ The letter %c is” , ‘ j ’ ) ;

printf ( “ pronounced as %s” , “ jay ” ) ;


}
 
• Output:
The letter j is pronounced as jay.
Using Format Specifier in printf ( )
• In the above program, there are two different new things to note.
• First we have used %c for the printing of a character which is
surrounded by single quotes where as %s is used for printing a
string which is surrounded by double quotes.
• This is how C Language recognizes the difference between a
character and a string.
Using Format Specifier in printf ( )
• Second thing to note is that even though the output statement is
printed by two separate program lines, it does not consists of
two line of text on the output screen. That is because printf( )
does not automatically prints a new line character at the end of a
line.
• So what to do for inserting a new line in a printf( ) statement?
Learning Resources
Books:
1. Object-Oriented Programming in C++ By Robert Lafore
2. Let Us C By Yashavant P. Kanetkar

Online Resources:
1. https://www.learncpp.com/
2. C Programming For Beginners

Online C Compiler:
https://www.onlinegdb.com/

You might also like