0% found this document useful (0 votes)
3 views

04 - Computer fundamentals - Zero Semester

Uploaded by

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

04 - Computer fundamentals - Zero Semester

Uploaded by

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

COMPUTER

FUNDAMENTALS
AND PROGRAMMING
By Prof. Dr. Aneela Zameer
LECTURE NO.1
INTRO TO COMPUTER
FUNDAMENTALS
#include <stdio.h>
int main( )
{
int x, y, z;
x = 5;
y = 7;
z = x + y;

printf(“Sum is %d", z); // Compare it with printf(“%d", z);


getchar();
return 0;
}
Sum is 12
2024 Computer Fundamentals and Programming 3
#include <stdio.h>
int main( )
{
int x, y, z, m;
x = 5;
y = 7;
z = x + y;
m = x - y;

printf("Sum is %d", z);


printf("Difference is %d", m);

getchar();
return 0;
}
Sum is 12 Difference is -2
2024 Computer Fundamentals and Programming 4
#include <stdio.h>
int main( )
{
int x, y, z, m;
x = 5;
y = 7;
z = x + y;
m = x - y;

printf("Sum is %d\n", z);


printf("Difference is %d", m);
getchar();
return 0;
}
Sum is 12
Difference is -2
2024 Computer Fundamentals and Programming 5
#include <stdio.h>
int main( )
{
int x, y, z, m;
x = 5;
y = 7;
z = x + y;
m = x - y;

printf("Sum is %d\nDifference is %d", z, m); // two outputs in


//one printf
getchar();
return 0;
}
What’s output?

2024 Computer Fundamentals and Programming 6


C IS HIGHLY CASE SENSITIVE

#include <stdio.h>
int main( )
{
int x, y, z, Z;
x = 5;
y = 7;
z = x + y;
Z = x - y;
printf(“%d\n", z); // 12
printf(“%d", Z); // -2
getchar();
return 0;
}
//Note: Z is different from z.
2024 Computer Fundamentals and Programming 7
ESCAPE SEQUENCES

Sequence Meaning
\a Bell (alert)
\b Backspace
\n Newline
\t Horizontal tab
\\ Backslash
\' Single quote
\" Double quotation

2024 Computer Fundamentals and Programming 8


ESCAPE SEQUENCES

• The name reflects the fact that the backslash


causes an “escape” from the normal way
characters are interpreted.

• In this case, the n is interpreted not as the


character ‘n’ but as the new line character.

2024 Computer Fundamentals and Programming 9


#include <stdio.h>
int main( )
{
Exercise
Try out various escape
int x, y, z, m;
sequences in this program.
x = 5;
y = 7;
z = x + y;
m = x - y;

printf("Sum is %d\nDifference is %d", z, m);

getchar();
return 0;
}
Sum is 12
Difference is -2
2024 Computer Fundamentals and Programming 10
#include <stdio.h>
int main( )
{
int x = 5, y = 7, z; // initialization with declaration

z = x + y;

printf(“%d", z);

getchar();
return 0;
}

2024 Computer Fundamentals and Programming 11


“HELLO WORLD”
//Most of the people write this program first
#include <stdio.h>
int main ( )
{
printf ("Hello World");
getchar();
return 0;
}

2024 Computer Fundamentals and Programming 12


2024 Computer Fundamentals and Programming 13
MORE ABOUT PRINTF()
• The printf function is used to output information
(both data from variables and text) to standard
output.
• It takes a format string and parameters for output.
a = 10;
b = 20;
• printf("The number is %d", a);

• printf("The numbers are %d and %d",a,b);

• printf("The numbers are %d and %d",a,b*3);

• printf("The numbers are %d and %d",a,b);


2024 Computer Fundamentals and Programming 14
MORE ABOUT PRINTF()
• The printf function is used to output information
(both data from variables and text) to standard
output.
• It takes a format string and parameters for output.
a = 10;
b = 20;
• printf("The number is %d", a);
The number is 10
• printf("The numbers are %d and %d",a,b);
The numbers are 10 and 20
• printf("The numbers are %d and %d",a,b*3);
The numbers are 10 and 60
• printf("The numbers are %d and %d",a,b);
The numbers are 10 and 20
2024 Computer Fundamentals and Programming 15
MORE ABOUT PRINTF()
The format string contains:
– Literal text: is printed as is without variation
– Escaped sequences: special characters
preceeded by \
– Conversion specifiers: % followed by a single
character
• Indicates (usually) that a variable is to be
printed at this location in the output stream.
• The variables to be printed must appear in the
parameters to printf following the format
string, in the order that they appear in the
format string.

2024 Computer Fundamentals and Programming 16


C DOESN’T CARE ABOUT SPACES
#include <stdio.h> #include <stdio.h>
Both of these
int main ( ) int programs are
{ main
printf ("Hello World”); (
the same as far as
) your compiler is
return 0; { concerned.
} printf
(
"Hello World" We SHOULD lay
) out our C program
;
return to make them look
0 nice.
;
}

2024 Computer Fundamentals and Programming 17


C DOESN’T CARE ABOUT SPACES
In the most general sense, a statement is a part of
your program that can be executed.
a = 10;
An expression is a statement.
a = a+1;
A function call is also a statement.
printf("%d”, a);
Other statements ……
C is a free form language, so you may type the
statements in any style you feel comfortable:
a=
a+
1;
a = a + 1; a = 6;
2024 Computer Fundamentals and Programming 18
C STATEMENTS
Some Suggestions
• DO: Use block braces on their own line. It is
easy to find the beginning and end of a block.
– This makes the code easier to read.
{
printf ("Hello, ");
printf ("world");
}
• AVOID: spreading a single statement across
multiple lines if there is no need.
– Try to keep it on one line.
2024 Computer Fundamentals and Programming 19
2024 Computer Fundamentals and Programming 20
NAMES OF C VARIABLES

• A good name for your variables is important


• Variables in C can be given any name made from numbers,
letters and underscores which is not a keyword and does
not begin with a number.
• Names may contain letters, digits and underscores
• The first character must be a letter or an underscore.
• First 31 characters are significant
(too long name is as bad as too short).
• Are case sensitive:
– abc is different from ABC
• Must begin with a letter or underscore and the rest can be
letters, digits, and underscores.

2024 Computer Fundamentals and Programming 21


NAMES OF C VARIABLES

present, hello, y2x3, r2d3, ... /* OK */


_1993_tar_return /* OK but not good */
Hello#there /* illegal */
int /* shouldn’t work */
2fartogo /* illegal */

2024 Computer Fundamentals and Programming 22


NAMES OF C VARIABLES

int a; int chairs;


int d; int no_students;
/* It is like
cryptic */ /* It is better */

2024 Computer Fundamentals and Programming 23


NAMES OF C VARIABLES

Suggestions regarding variable names


• DO: use variable names that are descriptive
• DO: adopt and stick to a standard naming convention
– sometimes it is useful to do this consistently for the
entire software development site

• AVOID: variable names starting with an underscore


– often used by the operating system and easy to miss
• AVOID: using uppercase only variable names
– generally these are pre-processor macros (later)

2024 Computer Fundamentals and Programming 24


NAMES OF C VARIABLES

• C keywords cannot be used as variable


names.
• Sometimes called reserved words.
• Are defined as a part of the C language.
• Can not be used for anything else!
• Examples:
– int
– while
– for
2024 Computer Fundamentals and Programming 25
KEYWORDS OF C
• Flow control (6) – if, else, return, switch,
case, default

• Loops (5) – for, do, while, break, continue


• Common types (5) – int, float, double, char,
void

• Structures (3) – struct, typedef, union

2024 Computer Fundamentals and Programming 26


2024 Computer Fundamentals and Programming 27
COMMENTS

• Can be used to write title of the program, author

details etc.

• To guide a programmer. To write a note for function,

operation, logic etc. in between a program.

• Non-executable statement.

2024 Computer Fundamentals and Programming 28


COMMENTS
Comments: /* This is a comment */
Use them!
Comments should explain:
• special cases
• the use of functions (parameters, return values, purpose)
• explain WHY your code does things the what it does.
• Can’t be nested.
e.g:- /* Hello /* abc */ Hi */ ERROR.

2024 Computer Fundamentals and Programming 29


COMMENTS

• Ideally, a comment with each variable name

helps people know what they do.

• In Lab work, I like to see well chosen variable

names and comments on variables.

2024 Computer Fundamentals and Programming 30


MORE ON COMMENTS

For a single line comments, you may use //


For a single line or multiple lines comments, /* comments */ is
used.
A few examples of comments
/* This program calculates area of a rectangle
This program is developed by Mr. XYZ */
length = 5; // in km
width = 3; // in km

2024 Computer Fundamentals and Programming 31


#include <stdio.h>
int main( ) //OUR FIRST C
{ PROGRAM
int x, y, z;
x = 5;
y = 7;
z = x + y;

printf(“%d", z);

getchar();
return 0;
}

2024 Computer Fundamentals and Programming 32


2024 Computer Fundamentals and Programming 33
#include <stdio.h>
int main( )
{
int x, y, z;

scanf ( "%d", &x);

scanf ( "%d", &y);

z = x + y;
printf("Sum is %d", z);
getchar();
getchar(); return 0;
2024 } Computer Fundamentals and Programming 34
#include <stdio.h>
int main( )
{
int x, y, z;
printf("This program adds two integer values\n");

printf ("Enter 1st Integer Value: ");


scanf ( "%d", &x);

printf ("Enter 2nd Integer Value:");


scanf ( "%d", &y);

z = x + y;
printf("Sum is %d", z);
getchar();
getchar(); return 0;
2024 } Computer Fundamentals and Programming 35
#include <stdio.h>
EXAMPLE
int main ()
{
int num, square;
printf ("Enter an integer value please: ");
scanf ( "%d", &num);
square = num*num;
printf ("Square of your entered number %d is %d\n", num,
square);

getchar();
return 0;
}

2024 Computer Fundamentals and Programming 36


READING NUMERIC DATA WITH SCANF

• Reading input from keyboard


• scanf can be used like printf but to read instead of write.
• The scanf function is the input equivalent of printf
– A C library function in the <stdio.h> library
– Takes a format string and parameters, much like printf
– The format string specifiers are nearly the same as those used
in printf
• Examples:
scanf ("%d", &x); /* reads a decimal integer */
• The ampersand (&) is used to get the “address” of the
variable (Later)
– If we use scanf("%d",x) instead, the value of x is passed. As a
result, scanf will not know where to put the number it reads.

2024 Computer Fundamentals and Programming 37


READING NUMERIC DATA WITH SCANF
• Reading more than one variable at a time:
– For example:
int n1, n2, n3;
scanf("%d%d%d",&n1,&n2,&n3);
– Use white spaces to separate numbers when input (or
enter each value one by one; both are OK)
5 10 22
• In the format string:
– You can use other characters to separate the
numbers int no_students, no_chairs;
scanf(“%d%d", &no_students, &no_chairs);
2024 Computer Fundamentals and Programming 38
#include <stdio.h> EXAMPLE
int main( )
{
int value1, value2, sum, product ;
printf("Enter two integer values: ") ;
scanf("%d %d", &value1, &value2) ;
sum = value1 + value2 ;
product = value1 * value2 ;
printf("Sum is = %d \n\nProduct = %d\n", sum, product) ;
getchar();
getchar();
return 0 ;
}

2024 Computer Fundamentals and Programming 39


2024 Computer Fundamentals and Programming 40
EXAMPLE
#include <stdio.h>
int main ()
{
float radius = 3.1, area;

area = 3.14159 * radius * radius;

printf ("Area = %f", area);

getchar();
return 0;
}

2024 Computer Fundamentals and Programming 41


EXAMPLE

#include <stdio.h>
int main ()
{
float radius, area;
printf ("Enter the value of radius: ");
scanf ( "%f", &radius);

area = 3.14159 * radius * radius;


printf ("Area = %f", area);
getchar();
return 0;

2024
} Computer Fundamentals and Programming 42
EXAMPLE
#include <stdio.h>
int main ()
{
double radius, area; // double is used for more precision
printf ("Enter the value of radius ");
scanf ( "%lf", &radius);

area = 3.14159 * radius * radius;


printf ("Area = %lf", area);
getchar();
return 0;

2024
} Computer Fundamentals and Programming 43
CHARACTERS
#include <stdio.h>

int main()
{
char x = ‘h’; // Compare it with int x = 10

printf(“Character x is %c\n", x); // note the use of %c

getchar();
return 0;

}
2024 Computer Fundamentals and Programming 44
// How to read a character and store in a variable
#include <stdio.h>
int main()
{
char x ;

printf(“Enter a character”);

scanf(“%c",&x); // note the use of %c and &

printf(“ You entered %c\n", x); // note the use of %c


getchar();
return 0;
}
2024 Computer Fundamentals and Programming 45
DATA TYPES IN C

• We must declare the type of every variable we


use in C.
• Every variable has a type (e.g. int) and a name
(e.g. no_students), i.e. int no_students
• Basic data types in C
– char: a single byte, capable of holding one
character
– int: an integer of fixed length, typically reflecting
the natural size of integers on the host
machine (i.e., 32 or 64 bits)
– float: single-precision floating point
– double: double precision floating point
2024 Computer Fundamentals and Programming 46
DATA TYPES IN C

• Floating-point variables represent numbers


with a decimal place—like 9.3, 3.1415927,
0.0000625, and –10.2.
• They have both an integer part to the left of
the decimal point, and a fractional part to the
right.
• Floating-point variables represent what
mathematicians call real numbers.

2024 Computer Fundamentals and Programming 47


CONVERSION SPECIFIERS
#include <stdio.h>
int main( )
{
int x = 5;
printf(“\n x is %d", x); // %d is format specifier
getchar();
return 0;
}
Format specifiers are used in printf function for printing
numbers and characters. A format specifier acts like a place
holder, it reserves a place in a string for numbers and
characters.
2024 Computer Fundamentals and Programming 48
CONVERSION SPECIFIERS
Specifier Meaning
%c Single character
%d Decimal integer
%f Decimal floating point number
%lf Decimal floating point number (double)
There must be one conversion specifier for each argument
being printed out.
• Ensure you use the correct specifier for the type of data you
are printing.
• Format specifiers are used in printf function for printing
numbers and characters. A format specifier acts like a place
holder, it reserves a place in a string for numbers and
characters.
2024 Computer Fundamentals and Programming 49
VARIABLE DECLARATION

• Generic Form
typename varname1, varname2, ...;

• Examples:
int count, x, y, z;
float a, b;
double percent, total, average;

2024 Computer Fundamentals and Programming 50


VARIABLE DECLARATION

Initialization
ALWAYS initialize a variable before using it
Failure to do so in C is asking for trouble
The value of an uninitialized variables is undefined in the C standards
Examples:

int count; /* Set aside storage space for count */


count = 0; /* Store 0 in count */
• This can be done at definition:
int count = 0;
double percent = 10.0, rate = 0.56;

2024 Computer Fundamentals and Programming 51


EXAMPLE
#include <stdio.h>
int main ()
{
float radius, area;
printf ("Enter the value of radius ");
scanf ( "%f", &radius);

area = 3.14159 * radius * radius;


printf ("Area = %f", area);

getchar();
return 0;
}
2024 Computer Fundamentals and Programming 52
READING NUMERIC DATA WITH SCANF
– For example:
int n1, n2,x;
float f, rate;
scanf ("%d",&x); /*reads a decimal integer */
scanf ("%f",&rate); /*reads a floating point value*/
scanf("%d%d%f",&n1,&n2,&f);
Use white spaces to separate numbers when
input.
5 10 20.3 then press Enter Key

OR, you can also enter these values one by one


by pressing enter Key. Both are OK.

2024 Computer Fundamentals and Programming 53


#include <stdio.h>
int main( )
{
int x;
x = 5;
printf(“%d\n", x);

x = x + 3; // updation of x value
printf(“%d\n", x);

printf(“%d\n", x*6);

printf(“%d\n", x);
getchar();
return 0;
}
2024 Computer Fundamentals and Programming 54
#include <stdio.h>
int main( )
{
int x;
x = 5;
printf(“%d\n", x); // 5

x = x + 3;
printf(“%d\n", x); // 8

printf(“%d\n", x*6);// 48

printf(“%d\n", x); // 8
getchar();
return 0;
}
2024 Computer Fundamentals and Programming 55

You might also like