Lecture 1

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

Introduction to Basic Computer

Programming

1
OVERVIEW

2
Introduction
General-purpose, procedural, imperative computer programming
language.
Developed in 1972 by Dennis M. Ritchie.

Why learn C
Easy to learn
Structured language
It produces efficient programs
It can handle low-level activities
It can be compiled on a variety of computer platforms 3
Applications
Operating Systems
Language Compilers
Text Editors
Network Drivers
Databases
Language Interpreters
4
ENVIRONMENT SETUP

5
If you want to set up your environment for C
programming language, you need the following two
software tools available on your computer:
– Text Editor - VSCode, Codeblocks, Sublime,
Xcode(MacOS)
– The C Compiler - GNU C/C++

https://sourceforge.net/projects/codeblocks
6
Hello World using C Programming.
#include <stdio.h>
int main() {
/* my first program in C */
printf("Hello, World! \n");
return 0;
}
https://onecompiler.com/c 7
PROGRAM STRUCTURE

8
 Preprocessor Commands #include <stdio.h>
 Functions int main() {
 Variables /* my first program in C */
 Statements & Expressions printf("Hello, World! \n");
 Comments return 0;
}

9
 The first line of the program #include <stdio.h> is
a preprocessor command, which tells a C compiler
to include stdio.h file before going to actual
compilation.
 The next line int main() is the main function
where the program execution begins.

10
 The next line /*...*/ (comment) will be ignored by
the compiler and it has been put to add additional
comments in the program
 The next line printf(...) is another function
available in C which causes the message "Hello,
World!" to be displayed on the screen.
 The next line return 0; terminates the main()
function and returns the value 0. 11
BASIC SYNTAX

12
Semicolons
In a C program, the semicolon is a statement
terminator. That is, each individual statement must
be ended with a semicolon. It indicates the end of
one logical entity.
Given below are two different statements −
printf("Hello, World! \n");
return 0;
14
Comments
Comments are like helping text in your C program
and they are ignored by the compiler. They start
with /* and terminate with the characters */ as
shown below
/* my first program in C */
Or for single line comment-
//My first program in C
You cannot have comments within comments
15 and
Identifiers and Keywords
Identifiers are names used to identify a variable,
function, or any other user-defined item.
– Starts with a letter A to Z, a to z, or an underscore '_'
followed by zero or more letters, underscores, and
digits (0 to 9).
– C does not allow punctuation characters such as @, $,
and % within identifiers.
– C is a case-sensitive programming language.
16
Keywords are reserved auto
words inelse
C long switch
break enum register typedef
case extern return union
char float short unsigned
const for signed void
continue goto sizeof volatile
default if static while
do int struct _Packed
double
17
Task
Run the program below:

#include<stdio.h>
int main() {
printf(“My name is … \n"); //complete with your
name
return 0;
18
DATA TYPES AND VARIABLES

19
Basic variable types
 char (‘F’)
 int (4)
 float (224.6)
 double (8724557854.255)
 void (none)

20
char - Typically a single octet(one byte).
#include <stdio.h>
int main() {
char gender = ‘M’;
printf(“%c \n“,gender);
return 0;
}
21
int - The most natural size of integer for the
machine.
#include <stdio.h>
int main() {
int age = 12;
printf(“%d \n“,age);
return 0;
} 22
float - A single-precision floating point value.
#include <stdio.h>
int main() {
float temp = 350.7;
printf(“%.2f \n“,temp);
return 0;
}
23
VARIABLES

29
Variable definition
A variable definition tells the compiler where and
how much storage to create for the variable.
Specifies a data type and contains a list of one or
more variables of that type.
type variable_list;
– int i, j, k;
– char c, ch;
– float f, salary;
– double d; 30
Variables can be initialized (assigned an initial
value) in their declaration.
type variable_name = value;
int d = 3, f = 5; // definition and initializing d
and f.
float z = 2.2; // definition and initializes z.
char x = 'x'; // the variable x has the value
'x'. 31
#include <stdio.h> b = 20;
int main () { c = a + b;
/* variable definition: */ printf("value of c : %d \n",
int a, b; c);
int c; f = 70.0/3.0;
float f; printf("value of f : %f \n",
/* actual initialization */ f);
a = 10; return 0;
} 33
Task
Write a program that declares your age, height and
gender (int, float, char) and initializes them with
values then print them.

34
Lvalues and Rvalues in C
There are two kinds of expressions in C −
lvalue − Refer to a memory location. Appear on left-
hand or right-hand side of an assignment.
rvalue − Data value stored in memory. Cannot have a
value assigned to it which means an rvalue may
appear on the right-hand side but not on the left-hand
side of an assignment.
– int g = 20; // valid statement
– 10 = 20; // invalid statement; would generate
35
Task
Write a C program to define 3 variables ‘salary’,
‘tax’ and ‘expenditure’ and then use them to
calculate the value of another variable ‘savings’.
savings = salary - tax – expenditure
savings = salary – (10% of salary) -
expenditure
Example: Salary-1000
Tax-200 36
CONSTANTS AND LITERALS

37
Character Constants
Character literals are enclosed in single quotes, e.g.,
'x' can be stored in a simple variable of char type. A
character literal can be:-
– a plain character (e.g., 'x'),
– an escape sequence (e.g., '\t'),
– or a universal character (e.g., '\u02C0').
There are certain characters in C that represent
special meaning when preceded by a backslash
38 for
#include <stdio.h>
int main() {
printf("Hello\tWorld\n\n");
return 0;
}

39
String Literals
String literals or constants are enclosed in double
quotes “x".
A string contains characters that are similar to
character literals: plain characters, escape
sequences, and universal characters.

You can break a long line into multiple lines using


string literals and separating them using40 white
“Hello World!”
"hello, dear"
"hello, \
dear"
"hello, " "d" "ear"

41
Defining Constants(const)
The const Keyword (const type variable =
value;)

#include <stdio.h>
int main() {
const int LENGTH = 10;
const int WIDTH = 5;
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
return 0;
}

42
Task
Write a C program to calculate BMI index (weight
and height are constant variables)
BMI = weight(kg) / height(meters) x
height(meters)

43
STORAGE CLASSES

44
Storage Class
A storage class defines the scope (visibility) and life-
time of variables and/or functions within a C
Program
– auto
– Register
– extern

45
The auto and register storage
classes
Auto Register
The auto storage class is The register storage class
the default storage class for is used to define local
all local variables. variables that should be
stored in a register instead
{ of RAM.
int mount;
auto int month; {
} register int miles;
46
The extern storage classes
The extern storage class is used to give a reference
of a global variable that is visible to ALL the
program files.
When you use 'extern', the variable cannot be
initialized however, it points the variable name at a
storage location that has been previously defined.

48
In file one; In file two
#include <stdio.h> #include <stdio.h>
#include “filename.c" extern int count;
int count ; void write_extern(void) {
extern void write_extern(); printf("count is %d\n", count);
main() { }
count = 5;
write_extern();
}
49
The End

50

You might also like