The document discusses variables and data types in C programming. It defines what variables are, how they are declared and initialized, and the different scopes and types of variables. The main types discussed are integer, floating point, character, and local vs global variables. It also covers the sizeof operator and basic rules for naming variables.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
14 views
C Programming Lecture 02
The document discusses variables and data types in C programming. It defines what variables are, how they are declared and initialized, and the different scopes and types of variables. The main types discussed are integer, floating point, character, and local vs global variables. It also covers the sizeof operator and basic rules for naming variables.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31
Lecture 02:
Variable and Data Type
Dr. Md Forhad Rabbi Variable in C programming Language
Variable is name of reserved area allocated in memory. In
other words, it is a name of memory location. Variables are used to store information to be referenced and manipulated in a computer program. It is helpful to think of variables as containers that hold information. 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. Variable in C programming Language Variable You must declare all variables before they can be used. Following is the basic form of a variable declaration Variable Declaration and Initialization in two step; Variable Declaration and Initialization in one step Rules for naming variables: All variable names must begin with a letter of the alphabet, an underscore, or ( _ ), or a dollar sign ($). The convention is to always use a letter of the alphabet. The dollar sign and the underscore are discouraged. Digit at start is not allowed. A variable’s name can be any legal identifier. After the first initial letter, variable names may also contain letters and the digits 0 to 9. No spaces or special characters are allowed. The name can be of any length, but don't get carried away. Remember that you will have to type this name. Uppercase characters are distinct from lowercase characters. Using ALL uppercase letters are primarily used to identify constant variables. Remember that variable names are case-sensitive. You cannot use a C keyword (reserved word) for a variable name. White space is not permitted. Integer Number Variables To declare an int you use the instruction: int variable name; For example: int roll; declares that you want to create an int variable called roll. To assign a value to our integer variable we would use the following C statement: roll=20; The C programming language uses the "=" character for assignment. A statement of the form roll=20; should be interpreted as take the numerical value 20 and store it in a memory location associated with the integer variable a Decimal Number Variables
C uses one of two keywords to declare a variable that is
to be associated with a decimal number: float and double float total; double sum; To assign a numerical value to our floating point and double precision variables we would use the following C statement: total=0.0; sum=12.50; Character Variables
To declare a variable of type character we use the
keyword char. - A single character stored in one byte. For example: char ch; To assign, or store, a character value in a char data type is easy - a character variable is just a symbol enclosed by single quotes. For example, if c is a char variable you can store the letter A in it using the following C statement: ch='A' Variable Declaration and Initialization in C Programming Language C is a strongly typed language. Every variable must be declared, indicating its data type before it can be used. The declaration can also involve explicit initialization, giving the variable a value; a variable that is declared but not explicitly initialized is of uncertain value (and should be regarded as dangerous until it is initialized). Variable Declaration and Initialization in C Programming Language Declaration of Variable Initialization of Variable Type of Variable and their Scope in C Programming Language The C programming language defines the following kinds of variables: ❑ Constant Variables ❑ Volatile Variables ❑ Local Variable ❑ Global Variable Constant Variables Volatile Variables Global Variables Global Variables Global Variable Global Variable Local Variables Local variables are declared within a function definition and thus could only be accessed from anywhere in that function in which it is declared. Thus, local variables have their scope local to the function in which they are declared and are unknown to other functions outside their own function. Local variables are defined and initialized every time a function containing them is called and as soon as the compiler exits from that function, the local variable is destroyed. Local Variables Local Variables Local Variables Datatypes in C Programming sizeof(DataType) Operator