Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views8 pages

LESSON Week6 InputAndOutput

Input and output

Uploaded by

hanzu0414
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

LESSON Week6 InputAndOutput

Input and output

Uploaded by

hanzu0414
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 8
9129123, 1154 PM aboutblank Save/Print PDF Week 6: Input and Output Variables A variable is nothing but a name given to a storage area that our programs can manipulate, Each variable in C has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable ‘The name of a variable can be composed of letters, digits, and the underscore character. It must begin with, either a letter or an underscore. Upper and lowercase letters are distinct because C is case-sensitive. C programming language also allows to define various other types of variables, which we will cover in subsequent chapters like Enumeration, Pointer, Array, Structure, Union, etc. For this chapter, let us study only basic variable types. Vari Ie Definition in C A variable definition tells the compiler where and how much storage to create for the variable. A variable definition specifies a data type and contains a list of one or more variables of that type as follows: type variable_list; Here, type must be a valid C data type including char, w_chat, int, float, double, bool, or any user-defined object; and variable_list may consist of one or more identifier names separated by commas. Some valid declarations are shown here: inti, jks char ¢, eh; float f, salary; double d; about lank 18 9129123, 1154 PM aboutblank ‘The line int i,j, k; declares and defines the variables i,j and k; which instruct the compiler to create variables named i, j,and k of type int, ‘Variables can be initialized (assigned an initial value) in their declaration, The initializer consists of an equal sign followed by a constant expression as follows: type variable_name = value; Some examples are: extern int d= 3, = 5; i! declaration of d and f. int d=3, f= ; definition and initializing d and f byte z= 22; // definition and initializes z char 1 the variable x has the value x’ For definition without an initializer: variables with static storage duration are implicitly initialized with NULL all bytes have the value 0); the initial value of all other variables are undefined. about lank 28 9129123, 1154 PM aboutblank Variable Declaration in C A variable declaration provides assurance to the compiler that there exists a variable with the given type and name so that the compiler can proceed for further compilation without requiring the complete detail about the variable, A variable declaration has its meaning at the time of compilation only, the compiler needs actual variable declaration at the time of linking the program, Avvariable declaration is useful when you are using multiple files and you define your variable in one of the files which will be available at the time of linking the program. You will use the keyword extern to declare a variable at any place. Though you can declare a variable multiple times in your C program, it can be defined only once in a file, a function, or a block of code. Example: ‘Try the following example, where variables have been declared at the top, but they have been defined and initialized inside the main function: Hinelude 1! Variable declaration: exter int a, b; extern int ¢; extern float f int main () { /* variable definition: * int a, b; int ¢; float f; /* actual initialization * a= 10; b=20; about lank se 9129123, 1154 PM aboutblank printi("value of ¢ : %d.n", ¢); £= 70.0/3.0; printf("value of f: %f n", 1); retum 0; } ‘When the above code is compiled and executed, it produces the following result: value of ¢ : 30 value of f : 23.333334 Lyalues and Rvalues in C ‘There are two kinds of expressions in C: * value : Expressions that refer to a memory location are called "Ivalue” expressions. An Ivalue may appear as either the left-hand or right-hand side of an assignment, ‘value : The term rvalue refers to a data value that is stored at some address in. An rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right-hand side but not on the left-hand side of an assignment, Variables are Ivalues and so they may appear on the left-hand side of an assignment, Numeric literals ate rvalues and so they may not be assigned and cannot appear on the left-hand side, Take a look at the following valid and invalid statements: int g = 20; / valid statement 10 = 20; // invalid statement; would generate compile-time error Constant and Literals Constants refer to fixed values that the program may not alter during its execution, These fixed values are also called literals Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are enumeration constants as well. Constants are treated just like regular variables except that their values cannot be modified after their definition. Character Literals Character literals are enclosed in single quotes, e.g., 'x' can be stored in a simple variable of char type. about lank 48 9129123, 1154 PM aboutblank A character literal can be a plain character (e.g.,'x), an escape sequence (e.g, 't), of a universal character (e.g,,'u02C0. ‘There are certain characters in C that represent special meaning when preceded by a backslash, for example, newline (n) or tab (f). Hete, you have a list of such escape sequence codes: ESCAPE SEQUENCE MEANING [character F character [character [? character [Alert or bell [Backspace [Form feed [Newline [Carriage return [Horizontal tab Vertical tab [Octal number of one to three digits [Hexadecimal number of one or more digits about lank se 9129123, 1154 PM aboutblank Following is the example to show a few escape sequence characters: #include int main() { printi("HellotWorldnn"); retum 0; } ‘When the above code is compiled and executed, it produces the following result Hello World String Literals String literals or constants are enclosed in double quotes "*. A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters, You can break a long line into multiple lines using string literals and separating them using whitespaces. Here are some examples of string literals. All the three forms are identical strings. “hello, dear" “hello, dear" “hello, ""d" "ear" Defining Constant There are two simple ways in C to define constants: '* Using #define preprocessor ‘= Using const keyword about lank oe 929723, 1154 PM aboutblank ‘The #define Preprocessor Given below is the form to use #define preprocessor to define a constant: define identifier value The following example explains it in detail: include define LENGTH 10 define WIDTH 5 #Kdefine NEWLINE 'n’ int main() { int area; area = LENGTH * WIDTH: printf("value of area : %d", area); printf("%c", NEWLINE); return 0; , When the above code is compiled and executed, it produces the following result: value of area : 50 The const Keyword ‘You can use const prefix to declare constants with a specific type as follows: about lank 18 929723, 1154 PM aboutblank const type variable = value; The following example explains it in detail: include int main) { const int LENGTH = 10; const int WIDTH = 5; const char NEWLIN' int area; area = LENGTH * WIDTH; printf("value of area = %d", area); ‘When the above code is compiled and executed, it produces the following result: value of area : 50 Note that itis @ good programming practice to define constants in CAPITALS. Readty SOVSIISISIIS Note: For assessment, your score from first attempt will be your final score. Make sure to study before the first attempt. ‘SavelPrint PDF No assigned assessment assigned for this lesson. about lank ory

You might also like