Lecture 1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

C is a general-purpose high level language that was originally

developed by Dennis Ritchie at Bell Laboratories for the Unix


operating system.
C has now become a widely used professional language for various
reasons.
• Easy to learn
• Structured language
• It produces efficient programs.
• It can handle low-level activities.
• It can be compiled on a variety of computers.

Today C is the most widely used System Programming Language.


Most of the state of the art software have been implemented using C
Analyzing the Program Code Example
#include <stdio.h> Each header file contains one or more function
#include <stdlib.h> declarations, data type definitions etc.
int main()
{ The #include is a "preprocessor" that tells the
printf("Hello World"); compiler to put code from the header called
system("pause"); <stdio.h> into our program before actually
} creating the executable.

<stdlib.h> - standard library definitions. It defines several functions.


The next important line is int main() This line tells the compiler that there is a
function named main. The main function is where a program starts execution.
The "curly braces" { and } signal the beginning and end of functions and other code
blocks.
The printf function is the standard C way of displaying output on the screen. The
quotes tell the compiler that you want to output the literal string as-is

system("pause") - it will pause a program before it exits. This pause is very useful when
your IDE won't wait as you test a program and as soon as the program finished the
window closes taking all your data with it.
C Compilers

When you write any program in C language then to run that program you
need to compile that program using a C Compiler which converts your
program into a language understandable by a computer. This is called
machine language (ie. binary format).

Bloodshed Dev-C++ is a free integrated development environment (IDE)


for programming in C and C++.
The project is hosted by SourceForge. Dev-C++ was originally developed by
programmer Colin Laplace. Dev-C++ runs exclusively on Microsoft
Windows.
Data types

Data types are provided to store various types of data

C supports various data types such as float, int, char, etc., for storing data.

The variables should be declared by specifying the data type.


Variable Declaration
We have to declare all variables, before we can use them in our main program (int
main()). When we declare a variable, we specify its data type. For example:
int num1;
float average;
char letter;

Input Formatting:
scanf("%d", &num1);

Our output formatting is:


printf("The Number you input is %d", num1);
C Operators
C operators are used for assigning the value of an expression to a variable. The
general format for an assignment operator is var = expression.

Arithmetic Operators Relational Operators


> Greater than
+ Addition >= Greater then or Equal to
- Substruction < Less than
* Multiplication <= Less than or equal to
/ Division == Equal to
!= Not Equal to

Logical Operators
&& And
|| Or
Exercise 1:
Write a program that will get the sum two unique numbers.

Think First! What are the things (variables) that we need for we to get
the sum of the two numbers?

Algorithm:
Input: Enter Two Numbers (num1, num2)
Process: Compute the sum (sum = num1 + num2)
Output: Display the sum (sum)
Exercise 2:
Write a program that will calculate the average of 4 numbers.

Think First! What are the things (variables) that we need for we to get
the average of four numbers

Algorithm:
Input: Enter four numbers.
Process: Compute the Average (Average = num1 + num2 + num3 + num4 / 4)
Output: Display the Average (Average)
The Conditional Statements
The if-else statement is used to express decisions

Syntax
if (condition)
{
statement;
}
else
{
statement;
}
Example1:
Write a program that determines if the input age is qualified to vote or
not. The qualifying age is 18 years old and above.
Think First! What are the things (variables) to determine if the user input
is Qualified to Vote or Not?

Algorithm:
Enter Age:
if (age >=18)
printf("Qualified to Vote");
else
printf("Too Young");
Example1:
Write a program that determines if the input number is POSITIVE or
NEGATIVE.
Think First! What are the things (variables) to determine if the inputted
number is a POSITIVE or NEGATIVE number.

Algorithm:
Enter Number (num1):
if (num1 >= 0)
printf(“Positive");
else
printf(“Negative");

You might also like