C Language Fundamentals PDF
Document Details
Uploaded by FestiveCornet
Tags
Summary
This document covers fundamental concepts in C programming, including variables, data types (integers, real numbers, characters), and rules for naming variables.
Full Transcript
Chapter #3, Fundamentals of C Language Variable During programming we need to store data. This data is stored in variables. Variables are locations in memory for storing data. The memory is divided into blocks. It can be viewed as pigeon-holes. You can also think of it as PO Boxes. In post offices...
Chapter #3, Fundamentals of C Language Variable During programming we need to store data. This data is stored in variables. Variables are locations in memory for storing data. The memory is divided into blocks. It can be viewed as pigeon-holes. You can also think of it as PO Boxes. In post offices there are different boxes and each has an address. Similarly in memory, there is a numerical address for each location of memory (block). It is difficult for us to handle these numerical addresses in our programs. So we give a name to these locations. These names are variables. We call them variables because they can contain different values at different times. The variable names in C may be started with a character or an underscore (_). But avoid starting a name with underscore (_). C has many libraries which contain variables and function names normally starting with underscore ( _ ). So variable name starting with underscore (_) may conflict with these variables or function names. In a program every variable has: o Name o Type o Size o Value E.g. int x; OR int x=10; Rules for Name of Variable Rules Example CANNOT start with a number 2i CAN contain a number elsewhere h2o CANNOT contain any arithmetic operators r*s+t CANNOT contain any other punctuation marks #@x%£!!a CAN contain or begin with an underscore a_b _height_ CANNOT be a C keyword struct CANNOT contain a space ab, a b im stupid CANNOT be of mixed cases (Small and Caps) XSquared Data Types of C Language A variable must have a data type associated with it, for example it can have data types like integer, decimal numbers, characters etc. Different data types have different size in memory depending on the machine and compilers. These also affect the way they are displayed. There are few data types in C language. These data types are reserved words of C language. The reserve words can not be used as a variable name. 1 Chapter #3, Fundamentals of C Language Integer Numbers The C language provides three data types to handle int (whole) numbers. o Int: The data type int is used to store whole numbers (integers). The integer type has a space of 2 bytes Range -32768 to +32767 for signed 0 to 65535 for un-signed. Format Specifiers %d for Signed %u for un-singed. o long: On the other side if we have a very large whole number that can not be stored in an int then we use the data type long provided by C. So when we are going to deal with very big whole numbers in our program, we use long data type long int takes 4 bytes. Range 2147483648 to +2147483647 for signed 0 to 4294967295 for un-signed. Format Specifiers %ld for Signed %lu for un-singed. o char: In programming we do need to store characters like a,b,c etc. For storing the character data C language provides char data type. By using char data type we can store characters in variables. While assigning a character value to a char type variable single quotes are used around the character as ‘a’. char data stores characters in form of integer ascii code. Char data type takes 1 byte. Range -128 to 127 for signed 0 to 255 for un-signed. Format Specifiers %c for Signed and Un-singed 2 Chapter #3, Fundamentals of C Language Real Numbers The C language provides two data types to deal with real numbers (numbers with decimal points e.g. 1.35, 735.251). The real numbers are also known as floating point numbers. o float: To store real numbers, float data type is used. The float data type uses four bytes to store a real number. Range 3.4e-38 to 3.4e+38 Format Specifiers %f o double: If we need to store a large real number which cannot be store in four bytes, then we use double data type. The size of double is 8 bytes. Range 1.7e-308 to 1.7e +308 Format Specifiers %lf o long double: If we need to store a large real number which cannot be store in 8 bytes bytes, then we use long double data type. The size of double is 10 bytes. Range 3.4e-4932 to 1.1e+4932 Format Specifiers %Lf Signed and Unsigned Integers All integer types come in two varieties: signed and unsigned. The idea here is that sometimes we need negative numbers, and sometimes we don't. Integers without the word "unsigned" are assumed to be signed. Signed integers are either negative or positive. Unsigned integers are always positive. E.g. signed int x; OR unsigned int x; // unsigned keyword is optional 3 Chapter #3, Fundamentals of C Language Constants Like variables, constants are data storage locations. Unlike variables, and as the name implies, constants don't change. You must initialize a constant when you create it, and you cannot assign a new value later. C has two types of constants: literal and symbolic. Literal Constants A literal constant is a value typed directly into your program wherever it is needed. For example int a = 39; a is a variable of type int; 39 is a literal constant. You can't assign a value to 39, and its value can't be changed. or printf(“Bahira College Karsaz”); In the above statement “Bahria College Karsaz” is a string constant. Symbolic Constants A symbolic constant is a constant that is represented by a name, just as a variable is represented. Unlike a variable, however, after a constant is initialized, its value can't be changed. Symbolic constants can be defined by using two techniques: By using const key word: E.g: const float pi=3.14; By using #define statement E.g: #define float PI 3.14; The difference between const and #define is: const can be used with local as well as global variables and data type is mentioned with declaration. #define always declare before main() function and there is no such data type is required to mentioned with declaration. 4 Chapter #3, Fundamentals of C Language 5