Week 7: Introduction to C Programming PDF

Document Details

Uploaded by Deleted User

Bachelor of Science in Information Technology

Mr. Pink Floyd B. Janeo

Tags

C programming variables constants computer science

Summary

This document provides an overview of the Introduction to C Programming course outline, focusing on core concepts like variables, constants, and literals. It details the course content, objectives, and instructions for learners.

Full Transcript

WEEK 7 COURSE OUTLINE COURSE CODE : IT 122 TITLE : Introduction to C Programming 1 TARGET POPULATION : All BS Information Technology Student...

WEEK 7 COURSE OUTLINE COURSE CODE : IT 122 TITLE : Introduction to C Programming 1 TARGET POPULATION : All BS Information Technology Students INSTRUCTOR : MR. PINK FLOYD B. JANEO Overview: C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard. The UNIX operating system, the C compiler, and essentially all UNIX application programs have been written in C. Content  Variables  Constant and Literals  Character Literals  String Literals  Defining constant  The const keyword Objectives:  Explain what is a variable.  Differentiate the difference between variable definition and variable declaration.  Discuss the character literals and string literals Instruction to the Learner Each chapter in this module contains a major lesson involving the use of Flowchart and its purpose. The units are characterized by continuity, and are arranged in such a manner that the present unit is related to the next unit. For this reason, you are advised to read this module. After each unit, there are exercises to be given. Submission of task given will be every Monday during your scheduled class hour. 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. Variable 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_char, 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: int i, j, k; char c, ch; float f, salary; double d; 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, f = 5; // declaration of d and f. int d = 3, f = 5; // definition and initializing d and f. byte z = 22; // definition and initializes z. char x = 'x'; // 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. 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. A variable 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: #include // Variable declaration: extern int a, b; extern int c; extern float f; int main () { int a, b; int c; float f; a = 10; b = 20; c = a + b; printf("value of c : %d \n", c); f = 70.0/3.0; printf("value of f : %f \n", f); return 0; } When the above code is compiled and executed, it produces the following result: value of c : 30 value of f : 23.333334 Lvalues and Rvalues in C There are two kinds of expressions in C:  lvalue : Expressions that refer to a memory location are called "lvalue" expressions. An lvalue may appear as either the left-hand or right-hand side of an assignment.  rvalue : 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 lvalues and so they may appear on the left-hand side of an assignment. Numeric literals are 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. A character literal can be a plain character (e.g., 'x'), an escape sequence (e.g., '\t'), or 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 (\t). Here, you have a list of such escape sequence codes: ESCAPE SEQUENCE MEANING \\ \ character \’ ‘ character \” “ character \? ? character \a Alert or bell \b Backspace \f Form feed \n Newline \r Carriage return \t Horizontal tab \v Vertical tab \ooo Octal number of one to three digits \xhh… Hexadecimal number of one or more digits Following is the example to show a few escape sequence characters: #include int main() { printf("Hello\tWorld\n\n"); return 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 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 #define 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: const type variable = value; The following example explains it in detail: #include int main() { const int LENGTH = 10; const int WIDTH = 5; const char NEWLINE = '\n'; 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 Note that it is a good programming practice to define constants in CAPITALS.

Use Quizgecko on...
Browser
Browser