0% found this document useful (0 votes)
40 views

A Variable Is A Named Memory Location in - The Contents of A Variable Can Change, Thus - User Defined

The document discusses variable declaration and basic data types in C programming. It covers: - Variables must be declared before use, and declaration reserves memory for the variable. - The four basic data types are integer, float, double, and char. - Integer variables store whole numbers, float stores numbers with decimals, double has higher precision than float, and char stores single characters. - Expressions in C combine constants, variables, and operators, and statements end with semicolons. - Variables can be initialized with a value when declared.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

A Variable Is A Named Memory Location in - The Contents of A Variable Can Change, Thus - User Defined

The document discusses variable declaration and basic data types in C programming. It covers: - Variables must be declared before use, and declaration reserves memory for the variable. - The four basic data types are integer, float, double, and char. - Integer variables store whole numbers, float stores numbers with decimals, double has higher precision than float, and char stores single characters. - Expressions in C combine constants, variables, and operators, and statements end with semicolons. - Variables can be initialized with a value when declared.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Lecture 2

A variable is a named memory location in which data of a certain type can be stored. The contents of a variable can change, thus the name. User defined variables must be declared before they can be used in a program. It is during the declaration phase that the actual memory for the variable is reserved.

The declaration of variables is done after the opening brace of main(). main( ) { int sum; The basic format for declaring variables is data_type var, var, ;

where data_type is one of the four basic types, an integer, character, float, or double type. Examples are int i,j,k; float length,height; char midinit;

BASIC DATA TYPE : INTEGER


INTEGER: These are whole numbers, both positive and negative. Unsigned integers(positive values only) are also supported. EXP : int age

BASIC DATA TYPE : FLOAT


FLOATING POINT: These are numbers which contain fractional parts, both positive and negative, and can be written in scientific notation. The keyword used to define float variables is float Typical floating point values are 1.73 and 1.932e5 (1.932 x 105). An example of declaring a float variable called x is float x;

BASIC DATA TYPE : DOUBLE


DOUBLE: These are floating point numbers, both positive and negative, which have a higher precision than float variables. The keyword used to define double variables is double An example of declaring a double variable called voltage is double voltage;

BASIC DATA TYPE : CHAR


CHARACTER: These are single characters. The keyword used to define character variables is char Typical character values might be the letter A, the character 5, the symbol , etc. An example of declaring a character variable called letter is char letter;

Expressions and Statements


An expression in C is some combination of constants, variables, operators and function calls. Sample expressions are: a+b 3.0*x - 9.66553 tan(angle) Most expressions have a value based on their contents. A statement in C is just an expression terminated with a semicolon. For example: sum = x + y + z; printf("Go Buckeyes!");

Initializing Variables
C Variables may be initialized with a value when they are declared. Consider the following declaration, which declares an integer variable count which is initialized to 10. int count = 10;

Initializing Variables Example

EXERSICE
Based on previous example, initializing three fixed variable used in Electrical Engineering. For example, one coulomb of charges equal to 6.241 x 10 power 18 electrons. You may search in IE. [ 10 minutes]

Format Specifiers Table


The following table show what format specifiers should be used with what data types:

Basic Input

An integer called pin is defined. A prompt to enter in a number is then printed with the first printf statement. The scanf routine, which accepts the response, has a control string and an address list. In the control string, the format specifier %d shows what data type is expected. The &pin argument specifies the memory location of the variable the input will be placed in.

After the scanf routine completes, the variable pin will be initialized with the input integer. This is confirmed with the second printf statement. The & character has a very special meaning in C. It is the address operator. (Much more with & when we get to pointers)

Quiz 3

You might also like