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

Computing Fundamentals

Dr. Muhammad Yousaf Hamza


Compound Assignment Operator
• Often we use “update” forms of operators
x=x+1, x=x*2, ...
How to interpret?
• C offers a short form for this:
Operator Equivalent to: e.g.
x += y
x+=y x=x+y
Here,
x *= y x=x*y
+ operator comes
y -= z + 1 y = y - (z + 1) before =

a /= b a=a/b So we first add,


i.e. (x+y)
x += y / 8 x = x + (y / 8) Then we store the
above expression's
y %= 3 y=y%3 value in "x"
Dr. Muhammad Yousaf Hamza
// demonstrates arithmetic assignement operators
#include <stdio.h>
int main()
{
int ans = 27;
ans += 10; //same as: ans = ans + 10;
printf(" %d, ",ans);
ans -= 7; //same as: ans = ans - 7;
printf(" %d, ",ans);
ans *= 2; //same as: ans = ans * 2;
printf(" %d, ",ans);
ans /= 3; //same as: ans = ans / 3;
printf(" %d, ",ans);
ans %= 3; //same as: ans = ans % 3;
printf(" %d, \n",ans);
getchar(); return 0;
} Dr. Muhammad Yousaf Hamza
Increment and Decrement Operators

Dr. Muhammad Yousaf Hamza


Increment and Decrement
• Increment and decrement operators.
– Increment: ++ It increases the value by 1
i = 7; // i is a variable name
++i; // (i = i + 1 or i + = 1). It increases the value of i by 1
i = 7;
i++;

–Decrement: -- (similar to ++) It decreases the value by 1


i = 8;
--i; // --i is the same as : (i = i – 1 or i - = 1).
i = 8;
i--;

Dr. Muhammad Yousaf Hamza


Increment and Decrement
Pre-fix and Post-fix
• ++i means increment i then use it
• i++ means use i then increment it
int i= 6;
printf ("%d\n",i++); /* Prints 6 sets i to 7 */

Note this important difference


int i= 6;
printf ("%d\n",++i); /* prints 7 and sets i to 7 */

All of the above also applies to --.

Dr. Muhammad Yousaf Hamza


Increment and Decrement
#include<stdio.h>
Pre-fix and Post-fix
int main()
{
int a = 7, b = 20, c, d;
c = a++;
printf("%d", c);
printf("\n%d",a);

d = ++b;
printf("\n%d", d);
printf("\n%d",b);

getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
Data Types

Dr. Muhammad Yousaf Hamza


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
Dr. Muhammad Yousaf Hamza
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.

Dr. Muhammad Yousaf Hamza


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.
Dr. Muhammad Yousaf Hamza
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.

Dr. Muhammad Yousaf Hamza


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

• Examples:
int count, x, y, z; multiple variable declaration simultaneously
float a, b, m;
double percent, total, average;

Dr. Muhammad Yousaf Hamza


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;
Declare and initiate multiple variables in 1 line
Dr. Muhammad Yousaf Hamza
Example
#include <stdio.h>
int main ()
{
double radius, area;
printf ("Enter the value of radius ");
scanf ( "%lf", &radius);

area = 3.14159 * radius * radius;


printf ("\nArea = %lf\n\n", area);

getchar();
return 0;
} Dr. Muhammad Yousaf Hamza
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.

Dr. Muhammad Yousaf Hamza


Type Conversion
#include<stdio.h>
int main()
{
int a= 23, b= 4;
float c;
c = a/b; // 5.00
printf("\n%f",c); // 5.00
getchar();
return 0;
}
Actually 23/4 = 5.75, but here output is 5.00 In order to have
correct answer (with decimal value), we need type casting.
Dr. Muhammad Yousaf Hamza
Type Conversion
• C allows for conversions between the basic types, implicitly or
explicitly. It is also called casting.
• A cast is a way of telling one variable type to temporarily look
like another.
• Explicit conversion uses the cast operator.
• Example :
int x=10;
float y, z=3.14;
y=(float) x; /* y=10.0 */
x=(int) z; /* x=3 */
x=(int) (-z); /* x=-3 */

Dr. Muhammad Yousaf Hamza


Casting of Variables
By using (type) in front of a variable we tell
the variable to act like another type of variable.
We can cast between any type usually.

Dr. Muhammad Yousaf Hamza


Casting of Variables
#include<stdio.h>
int main()
{
int a= 23, b= 4;
float c;
c = a/b;
printf("\n%f",c); // 5.00
Cast ints a and b to be float
c= (float)a/(float)b;
// c = (float)a/b; or c= a/(float)b; are also same.

printf("\n%f",c); // 5.75
getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
More types: const
const means a variable which doesn't vary – useful for
physical constants or things like pi or e
– You can also declare variables as being constants
– Use the const qualifier:
const double pi=3.1415926;
const long double e = 2.718281828;
const int maxlength=2356; #include<stdio.h>
const int val=(3*7+6)*5; int main()
•(scientific) notation {
(mantissa/exponent) const float n = 6.18e2;
const double n = 6.18e2; printf("%f",n); 618.00
getchar();
return 0;
• 6.18e2 = 6.18x10^2 }

Dr. Muhammad Yousaf Hamza


More types: const

Constants
– Constants are useful for a number of reasons
• Tells the reader of the code that a value does not
change
• Tells the compiler that a value does not change
– The compiler can potentially compile faster code

• Use constants where appropriate

Dr. Muhammad Yousaf Hamza


More types: const
#include<stdio.h>
int main()
{

const double pi=3.1415926;


double radius = 4.5, circum ;

circum = 2*pi*radius;

printf("\nCircumference is= %lf", circum); // 28.274334

getchar();
return 0;
}

Dr. Muhammad Yousaf Hamza


More types: const
#include<stdio.h>
int main()
{
const double pi=3.1415926;
float radius = 4.5,circum ;
circum = 2*pi*radius;
printf("\n%f", circum);

radius = 7.3;
pi = 2.9; // Error
circum = 2*pi*radius;
printf("\n%f", circum);
getchar();
return 0;
} Dr. Muhammad Yousaf Hamza
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;

}
Dr. Yousaf, PIEAS
// 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;
}
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
char x = 'h', y;
printf("Character x is %c\n", x); // note the use of %c
printf("Please type a character and then press enter\n");

y = getchar(); // It is valid. It will give correct output

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


getchar(); return 0; }

Note:
y = scanf("%c",&y); is not correct, as discussed in the int (or
float) case, previously. Though, it will run but will give you
incorrect output when you print the value of y.

Dr. Yousaf, PIEAS

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