C Programming: Understanding Variables, Data Types, and Arithmetic Operators, Study notes of Computer Programming

An introduction to c programming, focusing on variables, data types, and arithmetic operators. It covers the basics of c syntax, including the use of variables, data types like int, short, long, float, and double, and arithmetic operators such as addition, subtraction, multiplication, division, and modulus. It also includes tips for writing effective c code.

Typology: Study notes

2011/2012

Uploaded on 08/06/2012

anchal
anchal ๐Ÿ‡ฎ๐Ÿ‡ณ

4.6

(9)

95 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lectu
15
re No. 3
Reading Material
Deitel & Deitel โ€“ C++ How to Program chapter 1
1.19, 1.20, 1.21,
<< "Welcome to Virtual University of Pakistan";
d
me being take this line on faith. You have to write this line. The sign #
he
ou
1.22
Summary
First C program
Variables
Data Types
Arithmetic Operators
ence of Operators Preced
Tips
First C program
The best way to learn C is to start coding right away. So here is our very first program
in C.
# include <iostream.h>
main()
{
cout
}
We will look at this code line by line and try to understand them.
# include <iostream.h>
#include: This is a pre-processor directive. It is not part of our program; it is an
instruction to the compiler. It tells the C compiler to include the contents of a file, in
this case the system file iostream.h. The compiler knows that it is a system file, and
therefore looks for it in a special place. The features of preprocessor will be discusse
he tilater. For t
is known as HASH and also called SHARP.
<iostream.h>
This is the name of the library definition file for all Input Output Streams. Your
program will almost certainly want to send stuff to the screen and read things from t
keyboard. iostream.h is the name of the file in which has code to do that work for y
docsity.com
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download C Programming: Understanding Variables, Data Types, and Arithmetic Operators and more Study notes Computer Programming in PDF only on Docsity!

Lectu

re No. 3

Reading Material

Deitel & Deitel โ€“ C++ How to Program chapter 1 1.19, 1.20, 1.21,

<< "Welcome to Virtual University of Pakistan";

d me being take this line on faith. You have to write this line. The sign #

he ou

Summary

First C program

Variables Data Types Arithmetic Operators Precedence of Operators Tips

First C program

The best way to learn C is to start coding right away. So here is our very first program in C.

include

main() { cout }

We will look at this code line by line and try to understand them.

include

#include: This is a pre-processor directive. It is not part of our program; it is an instruction to the compiler. It tells the C compiler to include the contents of a file, in this case the system file iostream.h. The compiler knows that it is a system file, and therefore looks for it in a special place. The features of preprocessor will be discusse later. For the ti is known as HASH and also called SHARP.

This is the name of the library definition file for all Input Output Streams. Your program will almost certainly want to send stuff to the screen and read things from t keyboard. iostream .h is the name of the file in which has code to do that work for y

docsity.com

given a name by the programmer and they refer to each other as the program as a special case and will run this function first. If get to have a main function, or mistype the name, the compiler will give you r.

that there are parentheses (โ€œ ( ) โ€, normal brackets) with main. Here the

d braces(" { } "). For every open brace there se. Braces allows to group together pieces of a program. The

ou will

f

l

does extra semicolons may be put at the end but are useless and aimless. Do

to

bles n be there are ifferent boxes and each has an address. Similarly in memory, there is a numerical

name to these locations. These mes are variables. We call them variables because they can contain different values

main() The name main is special, in that the main is actually the one which is run when your program is used. A C program is made up of a large number of functions. Each of these is runs. C regards the name " main " you for an erro

Notice parentheses contain nothing. There may be something written inside the parentheses. It will be discussed in next lectures. { } Next, there is a curly bracket also calle must be a matching clo body of main is enclosed in braces. Braces are very important in C; they enclose the blocks of the program. cout << โ€œ Welcome to Virtual University of Pakistanโ€ cout : This is known as out put stream in C and C++. Stream is a complicated thing, y learn about it later. Think a stream as a door. The data is transferred through stream, cout takes data from computer and sends it to the output. For the moment it is a screen of the monitor. hence we use cout for output. << The sign << indicates the direction of data. Here it is towards cout and the function o cout is to show data on the screen. โ€œ Welcome to Virtual University of Pakistanโ€ The thing between the double quotes (โ€œ โ€) is known as character string. In C programming character strings are written in double quotes. Whatever is written after << and within quotation marks will be direct it to cout, cout wil l display it on the screen. ; There is a semicolon ( ; ) at the end of the above statement. This is very important. Al C statements end with semicolon ( ; ). Missing of a semicolon ( ; ) at the end of statement is a syntax error and compiler will report an error during compilation. If there is only a semicolon ( ; ) on a line than it will be called a null statement. i.e. it nothing. The not put semicolon ( ; ) at a wrong place, it may cause a problem during the execution of the program or may cause a logical error. In this program we give a fixed character string to cout and the program prints it the screen as: Variables During programming we need to store data. This data is stored in variables. Varia are locations in memory for storing data. The memory is divided into blocks. It ca viewed as pigeon-holes. You can also think of it as PO Boxes. In post offices d address for each location of memory (block). It is difficult for us to handle these numerical addresses in our programs. So we give a na at different times.

docsity.com

he C language provides three data types to handle whole numbers.

e has a

this

cout << x; cout << โ€œ y=โ€œ; ; cout << โ€œ z = x + y = โ€œ;

int z;

T

int short long

int Data Type

The data type int is used to store whole numbers (integers). The integer typ space of 4 bytes (32 bits for windows operating system) in memory. And it is mentioned as โ€˜ int โ€™ which is a reserved word of C, so we can not use it as a variable name.

In programming before using any variable name we have to declare that variable with its data type. If we are using an integer variable named as โ€˜ i โ€™, we have to declare it as int i ; The above line is known as declaration statement. When we declare a variable in way, it reserves some space in memory depending on the size of data type and labels it with the variable name. The declaration statement int i ; reserves 4 bytes of memory and labels it as โ€˜ i โ€™. This happens at the execution time.

Sample Program 1

Letโ€™s consider a simple example to explain int data type. In this example we take two integers, add them and display the answer on the screen. The code of the program is written below.

#include main() { int x; int y; int z; x = 5; y = 10; z = x + y; cout << โ€œx = โ€œ;

cout << y

cout << z; }

The first three lines declare three variables x, y and z as following.

int x; int y;

docsity.com

can also be written on one line. C provides us the comma r (,). The above three lines can be written in a single line as below int x, y, z; s we semicolon (;) indicates the end of the statement. So we can write on a single line. In this way we can also write the above declarations form i y; int z; or goo mming practice, write a single statement on a single line. ow w lues to variables x and y by using assignment operator. The lines x 5; an e values 5 and 10 to the variables x and y, respectively. These tatements put the values 5 and 10 to the memory locations labeled as x and y. x + y; evaluates the expression on right hand side. It takes alues iables x and y (which are 5 and 10 respectively), adds them and by ), puts the value of the result, which is 15 in this case, tion labeled as z. re a thing to be noted is that the values of x and y remains the same after this peration. In arithmetic operations the values of variables used in expression on the ght hand side are not affected. They remain the same. But a statement like x = x + 1; e. In this case the value of x is changed.

ere comes the affect of data type on cout. The previous statement cout << โ€œx = โ€œ ; fter << sign and cout simply displays the string. In the tatement cout << x; there is a variable name x. Now cout will not display โ€˜xโ€™ but the t interprets that x is a variable of integer type, it goes to the emory and takes its value and displays it in integer form, on the < y;

e variable not name of the variable. The next two lines cout << โ€œz = x + y = cout << z; are written to display โ€˜z = x + y = โ€™ and the value of z that is 15. en we execute the program after compiling, we get the following output.

These three declarations separato

A know that many statements in the following nt x; int F d progra N e assign va = d y = 10 assign th s The next statement z = v stored in var using the assignment operator (= to the memory loca He o ri is an exceptional cas

The next line cout << โ€œ x = โ€œ ; is simple it just displays โ€˜ x = โ€˜ on the screen. Now we want to display the value of x after โ€˜x =โ€™. For this we write the statement cout << x ; H has a character string a s value of x. The cou location x in the m screen. The next line cout << โ€y =โ€; displays โ€˜ y = โ€˜ on the screen. And line cout < displays the value of y on the screen. Thus we see that when we write something in quotation marks it is displayed as it is but when we use a variable name it displays the value of th โ€; and Now wh

x = 5 y = 10 z = x + y = 15

short Data type

We noted that the integer occupies four bytes in memo all integer like 5, 10 or 20 four bytes would be used. The C provides another data

ry. So if we have to store a

hole numbers which is called short. The size of short is two d it can store numbers in range of -32768 to 32767. So if we are going to use ariable for which we know that it will not increase from 32767, for example the e of d eople, then we use the data type short for age. We can write the ogram by using short instead of int.

/*This program uses short data type to store values */

sm type for storing small w bytes an a v ag ifferent p above sample pr

docsity.com

x + y;

cout << x;

e use it as:

In programming we do

char x; x = โ€™aโ€™; t << โ€œThe cha = โ€œ; cout << x; }

Arithmetic Operators In C language we have the usual arithmetic operators for addition, subtraction, ultiplication and division. C also provides a special arithmetic operator which is perators are binary operators which means they operate on s for addition, subtraction, multiplication,

x = 12. y = z = cout << โ€œ x = โ€œ;

cout << โ€œ y = โ€œ; cout << y; cout << โ€œ z = x + y = โ€œ; cout << z; }

double Data Type

If we need to store a large real number which cannot be store in four bytes, then we use double data type. Normally the size of double is twice the size of float. In program w

double x = 345624.769123;

char Data Type

So far we have been looking on data types to store numbers, need to store characters like a,b,c etc. For storing the character data C language provides char data type. By using char data type we can store characters in variables. While assigning a character value to a char type variable single quotes are used around the character as โ€˜aโ€™.

/* This program uses short data type to store values */

#include main() {

cou racter value in x

m called modulus. All these o two operands. So we need two value division and modulus.

ARITHMETIC

OPERATION

ARITHMETIC

OPERATOR

ALGEBRAIC

EXPRESSION

C

EXPRESSION

Addition + x + y x + y Subtraction - x โ€“ y x - y

docsity.com

Multiplication * Xy x * y Division / x รท y, x / y x / y Modulus % x mod y x % y

Addition, subtraction and multiplication are same as we use in alg use integer di

ebra. vision (i.e. both ou 2.5. the whole number, the fractional part lt, then we should use float

divided by y. For example, the result of 5 % 2 will be 1, 23 % 5 will be 3 and 107% will be 7. Precedence of Operators The arithmetic operators in an expression are evaluated according to their precedence. The precedence means which operator will be evaluated first and which will be evaluated after that and so on. In an expression, the parentheses ( ) are used to force the evaluation order. The operators in the parentheses ( ) are evaluated first. If there are nested parentheses then the inner most is evaluated first. The expressions are always evaluated from left to right. The operators *, / and % have the highest precedence after parentheses. These operators are evaluated before + and โ€“ operators. Thus + and โ€“ operators has the lowest precedence. It means that if there are

  • and + operators in an expression then first the * will be evaluated and then its result will be added to other operand. If there are * and / operators in an expression (both have the same precedence) then the operator which occurs first from left will be evaluated first and then the next, except you force any operator to evaluate by putting parentheses around it. The following table explains the precedence of the arithmetic operators: OPERATORS OPERATIONS PRECEDENCE (ORDER OF EVALUATION)

There is one thing to note in division that when we operands are integers) yields an integer result. This means that if, for example, y are dividing 5 by 2 (5 / 2) it will give integer result as 2 instead of actual result Thus in integer division the result is truncated to (after decimal) is ignored. If we want to get the correct resu data type. The modulus operator returns the remainder after division. This operator can only be used with integer operands. The expression x % y returns the remainder after x is

( ) Parentheses Evaluated first *, /, or % Multiplication, Division, Modulus

Evaluated second. If there are several, they are evaluated from left to right

  • or - Addition, Subtraction Evaluated last. If there are several, they are evaluated from left to right

Lets look some examples. What is the result of 10 + 10 * 5? The answer is 60 not 100. As * has higher precedence than + so 10 * 5 is evaluated first and then the answer 50 is added to 10 and we get the result 60. The answer will be 100 if we force the addition operation to be done first by putting 10 + 10 in parentheses. Thus the same expression rewritten as (10 + 10) * 5 will give the result

  1. Note that how the parentheses affect the evaluation of an expression.

docsity.com