0% found this document useful (0 votes)
163 views21 pages

CS 120 Variable Declaration

This document discusses variable declaration in C. It explains that variables must be declared before use and have a type and name. Variables can be declared locally inside functions, as function parameters, or globally outside functions. The basic data types in C are int, float, double, and char. Variables can be initialized or assigned values later. Input/output statements like printf and scanf are used to display output and get user input. Arithmetic, relational, and logical operators are covered. Examples of arithmetic operations and conditional statements are provided.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
163 views21 pages

CS 120 Variable Declaration

This document discusses variable declaration in C. It explains that variables must be declared before use and have a type and name. Variables can be declared locally inside functions, as function parameters, or globally outside functions. The basic data types in C are int, float, double, and char. Variables can be initialized or assigned values later. Input/output statements like printf and scanf are used to display output and get user input. Arithmetic, relational, and logical operators are covered. Examples of arithmetic operations and conditional statements are provided.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Variable Declaration in C

Variables are simply names used to refer to some location in memory-allocation that holds a value with which we are working. We must declare every variable before we use. Every variable has a type and a name. Variables should be declared in three basic places:

Inside functions local variables In the definition of function parameters formal parameters Outside of all functions global variables

Declarations of types should always be together at the top of main program or a function.

Variable Declaration in C (type)


l 1. 1. 1.

In standard C, there are four basic data types: int : uses integer numbers (e.g. int a=3, b=-2;) float : uses floating point numbers (e.g. float d=3.141159, e=-5.01;) double : uses big floating numbers (e.g. double g=6.002e23;) char : uses single character (e.g. char x=a; )

4.

Other types are signed, unsigned, long, short and const.

Variable Declaration in C (name)


l

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. int 2mark; char for; illegal

l l

A good name for your variables is important foo_bar _foo42 BAZ valid but not descriptive (lots of comments are needed)

start_time student_no course_mark Very descriptive (fewer comments are needed)

Variable Declaration in C
l

Multiple variables can be declared with one statement. int a, b, c, d; Initializing: declare and assign some content to a variable at the same time. int a=3; Assigning: after declaring variables, you can assign a value to a variable later on. int a; a=6; You can also assign a variable the value of another variable. a=b; You can assign multiple variables the same value with one statement. a=b=c=d=7;

Input / Output statements


#include<stdio.h> /* addition program */ int main () { int integer1, integer2, sum; printf( Enter first integer \n); scanf( %d, &integer1); printf( Enter second integer \n); scanf( %d, &integer2); sum=integer1 + integer2; printf( sum is %d \n, sum); return 0; }

/* Variable Declaration */ /* prompt : tells the user to take a specific action */ /* read an integer : to obtain a value from the user*/

Enter first integer 45 Enter second integer 72 Sum is 117

Input / Output statements


scanf( %d, &integer1); /* scanf has 2 arquments */ %d : format control string which indicates the type of data that should be input by the user. d stands for decimal integer

%d : int %f : float %c : char & : address operator integer1 : variable name The combination tells scanf the location in memory in which the variable integer1 is stored

& integer1 : consists of :

Note
The following two statements can be written in one statement:

sum=integer1 + integer2; printf( sum is %d \n, sum);


as

printf( sum is %d \n,integer1 + integer2);


l

Common programming error


integer1 + integer2 = sum /* calculation on the left side */

Examples : printf
#include<stdio.h> int main () { int x = 6; float y = 5.127, b=5; char z = 'A'; printf(" Welcome to C course "); printf(" \n %d \n %3.2f \n %2.1f \n %c", x, y, b, z);

Welcome to C course 6 5.13 5.0 A

/* % 3.2f means floating point number with a minimum of 3 characters wide and 2 decimal places*/

return 0; }

Arithmetic in C
remainder division % X mod Y / X Y X/Y multiplication * 5X subtraction X-5 addition + X+5 operator Algebraic expression C expression

X%Y

5*X

X-5

X+5

Arithmetic in C
() is the 1st precedence, if there are several pairs of parentheses on same level and not nested, they are evaluated left to right. (5+3) * (6/2) (5+3*(6/2)) *, /, % are the 2nd precedence, if they are several, they are evaluated left to right. +, - are the 3rd precedence, if they are several, they are evaluated left to right.
l

Arithmetic in C (examples)
Algebra : y= mx+b C : y=m*x+b; Algebra : z=pr mod q+w/x-y C : z=p*r%q+w/x-y int X, Y; X=5; Y=2; printf(%d, X/Y); printf(%d, X%Y);

/* will display 2 */ /* will display 1 */

(1) Arithmetic operators


l l

Increment operator (++) Decrement operator (--)

B++ or ++B is equivalent to B=B+1; B-- or --B is equivalent to B=B-1; Example: int x=8, y; y=x--; y=--x;

/* x=7 & y=8*/ /* x=7 & y=7*/

Arithmetic assignment statement i+=10; i-=10; i*=10; is equivalent to i=i+10; is equivalent to i=i-10; is equivalent to i=i*10;

Arithmetic operators (2)


#include <stdio.h> int main() { int i = 7 , j = 5 ; float x , y = 1.3 ; x = --j + i / 2.0 ; printf("j=%d x=%.2f \n", j, x) ; y += 7 % j * 1.1 ; printf("y=%.2f \n", y) ; return 0 ; }

Arithmetic operators (3)


#include <stdio.h> int main() { int i = 3 , j = 5 ; float x , y = 1.5 ; x = --j + i / 2.0 ; printf("j=%d x=%.2f \n", j, x) ; y += i++ - i % 4 * 2 ; printf("y=%.2f \n", y) ; return 0 ; }

Arithmetic operators (4)


#include <stdio.h> int main() { int i = 7 , j = 5; float x , y = 1.5; x = --j/3.0 + i++ ; printf("j=%d x=%.2f \n", j, x); y += i-- % 4 + ++i * 2; printf("i=%d y=%.2f \n", i, y); return 0; }

Relational operators(1)
less or equal greater or less equal greater not equal equal

XY

XY

X<Y

X>Y

XY

X=Y

Algebra expression

X<=Y

X>=Y

X<Y

X>Y

X!=Y X==Y

C expression

Relational operators(2)

> greater than < less than >= greater than or equal <= less than or equal == equal to != not equal to

5 > 4 is TRUE 4 < 5 is TRUE 4 >= 4 is TRUE 3 <= 4 is TRUE 5 == 5 is TRUE 5 != 4 is TRUE

Relational operators(3)
l

Relational operators are lower in precedence than the arithmetic operators. example: 10> 1+12 evaluated as 10>(1+12)

Common programming errors: 1- if the two symbols in any of the operators (==, !=, >=,<=) are separated by spaces. 2- if they are reversed as in =!, =>, =<

Logical operators:

if ( x < y && y < z ) printf( "x is less than z\n" ); printf( "%d" , (x == w || x == y || x == z) );

&& (AND) || (OR) ! (NOT)

/*The value 1 is printed if x is equal to either w, y, or z, otherwise it prints 0 */

Exercise
1- Write a C program that reads two positive integers x and y and calculate the following equation:

2- Write a C program to calculate area of a rectangle.

Exercise
Write a statement (or comment) to accomplish each of the following: - State that a program will calculate the product of three integers. - Declare the variables x, y, z and result to be of type int. - Prompt the user to enter three integers. - Reads three integers from the keyboard and store them in the variables x, y, and z. - Compute the product of the three integers contained in variables x, y and z, and assign the results to the variable result. - Print The product is followed by the value of the variable result.

Exercise
Write a C program to perform the following: A. Read the distance in miles. B. Convert the distance to kilometers. ( 1 mile = 1.609 kilometers ) C. Display the distance in kilometers. Write a C program to perform the following: A. Read the temperature in degree Celsius B. Convert the degree Celsius to degree Fahrenheit. (F=(9/5)C+32)

You might also like