04 - Computer fundamentals - Zero Semester
04 - Computer fundamentals - Zero Semester
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;
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;
#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
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;
}
details etc.
• Non-executable statement.
printf(“%d", z);
getchar();
return 0;
}
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");
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;
}
getchar();
return 0;
}
#include <stdio.h>
int main ()
{
float radius, area;
printf ("Enter the value of radius: ");
scanf ( "%f", &radius);
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);
2024
} Computer Fundamentals and Programming 43
CHARACTERS
#include <stdio.h>
int main()
{
char x = ‘h’; // Compare it with int x = 10
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”);
• Generic Form
typename varname1, varname2, ...;
• Examples:
int count, x, y, z;
float a, b;
double percent, total, average;
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:
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
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