Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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)

103 documents

1 / 9

Toggle sidebar

Related documents


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 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 16 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 will 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 19 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 cation x in the mlo 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 alue of thv ”; 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 ga ifferent p above sample pr docsity.com 20 includ .h> ain() z = x + y; cout << “x = “; cout << “ y=“; t y s two data types to deal with real numbers (numbers with bers are also known as floating point at Data Type float data type is used. The float data type uses four bytes to ain() z; # e <iostream m { short x; short y; short z; x = 5; y = 10; cout << x; cout << y; cout << “ z = x + y = “; cout << z; } long Data Type On the other side if we have a very large whole number that can not be stored in an in then we use the data type long provided by C. So when we are going to deal with ver big whole numbers in our program, we use long data type. We use it in program as: long x = 300500200; Real Numbers The C language provide decimal points e.g. 1.35, 735.251). The real num mbers. nu float ouble d flo To store real numbers, store a real number. Here is program that uses float data types. /*This program uses short data type to store values */ #include <iostream.h> m { float x; at y; flo float docsity.com 21 5; 25.57; 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.3 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 <iostream.h> 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
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved