Вы находитесь на странице: 1из 42

Computer

Programming
INTRODUCTION TO
PROGRAMMING

Instructor : M. Aqib Bajwa


• Program is a precise sequence of steps to solve a particular
problem

• Computer Program is a set of instructions which tell


computer what to do

• Programming is the process of writing computer programs


• Developed by M. Dennis Ritchie at the Bell laboratories in
the year of 1972.

• C is a high level language

• C is machine independent

• It’s a structured programming language

• Compiler is needed to compile the C programs


• Source Language is the language in which we write
computer program (High Level Language)
• Target Language is the language that computer understand
(Binary Language/ Low Level Language)
• Compiler is a software which can read the source language
and translate it to target language
• Compiler also detect and report the errors in source
language
• C Free 5.0, Turbo C++, Dev C++ etc.
#include<stdio.h>

#include<conio.h>

Int main()

Program code will be written here

getch();

}
• Header files are the predefined files in the C library using
which we create new program

• C Library means the database include all the functions

• STDIO stands for Standard Input/Output

• CONIO stands for Console Input/output

• .h is the extension of header file


• int main() is the main() method/ function of a C program.
Actual execution of a program starts from main method

• getch(); Function defined within conio.h file It holds the final


output screen until any key is pressed by user.
• Printf( “What you want to display”);

– Syntax : printf(“data type", variable name);

• Scanf(“what you want to input”);

– Syntax : scanf(“data type", &variable name);

• Comments are used for description of the program

– Single line comments start with //

– Double line comments start with /* and ends with */


• First of all header files must be included

• Every program should have main()

• Every statement should be end with semicolon(;)

• This Language is case sensitive

• All the letter should be in lowercase

• /n is used to start output from next line


#include<stdio.h>

#include<conio.h>

int main()

printf(“My First C++ Program. \n”);

getch();

}
Comments
• Comments are portions of the code ignored by the
compiler
– used to increase the readability of program for users
• Two types:
– Single line
// This is a C program single line comments example
// Welcome students in your course CP1103.

– Multiple line
/*
As you can see, you can include comments that can
occupy several lines also called multiple line
comment.
*/
/* TITLE :- MY FIRST C PROGRAM
CREATED BY :- XYZ */
#include<stdio.h>
#include<conio.h>
{
// to clear the output screen
printf(“My First C++ Program. \n”);
getch(); //to pause the final output
}
Include Directives and Namespaces
• include: directive copies that file into your
program
• namespace: a collection of names and their
definitions. Allows different namespaces to
use the same names without confusion
Tokens
• Tokens are the smallest individual unit of a
program in any language

– C tokens are:
• Special Symbols

• Words Symbols

• Identifiers
Reserved Words (Keywords)
• The syntax rules (or grammar) of C define
certain symbols to have a unique meaning
within a C program.
• There are 32 keywords in C and written in
lowercase. They can’t be used as variable.
• Reserved words, keywords, or word symbols
What is an Identifier?
• An identifier is the name to denote labels, types,
variables, constants or functions, in a C
program.
• C is a case-sensitive language.
– Work is not work
– Above example tells that “Work” is different variable
name and “work” is different variable, not same.
• Identifiers should be descriptive
– Using meaningful identifiers is a good programming
practice
Identifiers
• Identifiers must be unique
• Identifiers cannot be reserved words (keywords)
– double main return
• Identifier must start with a letter or underscore, and be
followed by zero or more letters (A-Z, a-z), digits (0-9),
or underscores
• VALID
age_of_dog _taxRateY2K
PrintHeading ageOfHorse
• NOT VALID
age# 2000TaxRate Age-Of-Dog main
What is a Variable?
• A variable is a memory address where data
can be stored and changed.

• Declaring a variable means specifying both its


name and its data type.
What Does a
Variable Declaration Do?
• A declaration tells the compiler to allocate
enough memory to hold a value of this data
type, and to associate the identifier with
this location.
• int ageOfDog;
• char middleInitial; 
• float taxRate;
Variable Declaration
• All variables must declared before use.
– At the top of the program
– Just before use.
• Commas are used to separate identifiers of
the same type.
int count, age;
• Variables can be initialized to a starting value
when they are declared
int count = 0;
int age, count = 0;
What is an Expression in C?
• An expression is a valid arrangement of
variables, constants, and operators.

• In C, each expression can be evaluated to


compute a value of a given type

• In C, an expression can be:


– A variable or a constant (count, 100)
– An operation (a + b, a * 2)
Format Specifiers
• Format specifiers can be defined as the
operators which are used in association with
printf() and scanf() functions for printing the
data that is referred by any variable.
• It starts with percentage % operator and
followed by a special character for identifying
the type of data.
• There are mostly six types of format
specifiers that are available in C.
Format Specifier

Format Specifier Description


%d Integer Format Specifier

%f Float Format Specifier

%c Character Format Specifier

%s String Format Specifier

%u Unsigned Integer Format Specifier

%ld Long Int Format Specifier


Some Examples of Format Specifier
• Character Format Specifier
scanf("%c", &variable name);
printf("%c", variable name);
• Integer Format Specifier
scanf("%d", &variable name);
printf("%d", variable name);
• String Format Specifier
scanf("%s", &variable name);
printf("%s", variable name);
Fundamental Data Types
• Data types specify how we enter data into our
programs and what type of data we enter. These
data types have different storage capacities.
• Integer
• Float
• Character
• Void
• Bool
• String
Integer Type
• Integers are whole numbers.
Type Size(bytes) Range
int or signed int 2 -32,768 to 32767
unsigned int 2 0 to 65535
short int or signed 1 -128 to 127
short int
unsigned short int 1 0 to 255
long int or signed 4 -2,147,483,648 to
long int 2,147,483,647
unsigned long int 4 0 to 4,294,967,295
Float Type
• Floating types are used to store real numbers.

Type Size(bytes) Range

Float 4 3.4E-38 to 3.4E+38

double 8 1.7E-308 to 1.7E+308

long double 10 3.4E-4932 to


1.1E+4932
Character Type
• Character types are used to store characters
value.
• Example of characters: – Numeric digits: 0 - 9
– Lowercase/uppercase letters: a - z and A - Z
– Space (blank) – Special characters: , . ; ? Etc.
Type Size(bytes) Range

char or signed char 1 -128 to 127

unsigned char 1 0 to 255


Void Type
• means no value. This is usually used to specify
the type of functions which returns nothing.
We will get acquainted to this data type as we
start learning more advanced topics in C
language, like functions, pointers etc.
Bool Type
• Type bool is a built-in type consisting of just
2 values, the constants true and false
• We can declare variables of type bool
bool hasFever; // true if has high temperature
bool isSenior; // true if age is at least 55
• The value 0 represents false
• ANY non-zero value represents true
String Type
• A string is a sequence of characters enclosed in
double quotes

• string sample values


“Hello” “Year 2000” “1234”
• The empty string (null string) contains no displayed
characters and is written as “”
Types of Operators
• Unary means consisting of single component or
element.
– Increment and decrement operators
• Binary operates on two operands and manipulates
them to return a result.
– Arithmetic operators
– Relational operators
– Logical operators
– Assignment Operators
• Ternary The ternary operator is an operator that takes
three arguments.
– Conditional operator
Increment and Decrement Operators
• Increment operator (++) - can be used instead of c+=1
• Decrement operator (--) - can be used instead of c-=1.
• Preincrement
– Operator is used before the variable (++c or --c)
– Variable is changed, then the expression it is in is evaluated
• Postincrement
– Operator is used after the variable (c++ or c--)
– Expression executes, then the variable is changed
• If c = 5, then
printf( "%d", ++c);
• Prints 6
printf( "%d", c++);
• Prints 5
– In either case, c now has the value of 6
Arithmetic Operators
• An arithmetic operator performs mathematical
operations such as addition, subtraction and
multiplication on numerical values (constants and
variables).
Operator Meaning of Operator

+ addition or unary plus

- subtraction or unary minus

* multiplication

/ division

% remainder after division( modulo division)


Relational Operators
• A relational operator checks the relationship between two
operands. If the relation is true, it returns 1; if the relation is
false, it returns value 0.
• Relational operators are used decision in making and loops.
Operator Meaning of Operator Example

== Equal to 5 == 3 returns 0
> Greater than 5 > 3 returns 1
< Less than 5 < 3 returns 0
!= Not equal to 5 != 3 returns 1
>= Greater than or equal to 5 >= 3 returns 1
<= Less than or equal to 5 <= 3 return 0
Logical Operators
• An expression containing logical operator returns either 0 or 1
depending upon whether expression results true or false.
Logical operators are commonly used in decision making in c
programming.
Operator Meaning of Operator Example

If c = 5 and d = 2 then,
Logial AND. True only if all
&& expression ((c == 5) && (d
operands are true
> 5)) equals to 0.
If c = 5 and d = 2 then,
Logical OR. True only if
|| expression ((c == 5) || (d
either one operand is true
> 5)) equals to 1.
Logical NOT. True only if If c = 5 then, expression !
!
the operand is 0 (c == 5) equals to 0.
Assignment Operator
• An operator to give (assign) a value to a
variable.
• Denote as ‘=‘
• Only variable can be on the left side.
• An expression is on the right side.
• Variables keep their assigned values until
changed by another assignment statement or
by reading in a new value.
Assignment Operator Syntax
• Variable = Expression
– First, expression on right is evaluated.
– Then the resulting value is stored in the memory location of Variable on left.
– “ = “ is commonly used but there also have some other operators.

Operator Example Same as

= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
Bitwise Operators
• During computation, mathematical operations like: addition, subtraction,
addition and division are converted to bit-level which makes processing
faster and saves power.
• Bitwise operators are used in C programming to perform bit-level
operations.

Operators Meaning of operators

& Bitwise AND


| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
Ternary or conditional Operator
• The ternary operator is an operator that takes
three arguments. The first argument is a
comparison argument, the second is the result
upon a true comparison, and the third is the
result upon a false comparison.
• Syntax
– conditionalExpression ? expression1 : expression2

Вам также может понравиться