1.0 C Language Overview The C programming language 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 applications programs have been written in C. The C has now become a widely used professional language for various reasons. Easy to learn Structured language It produces efficient programs. It can handle low-level activities. It can be compiled on a variety of computer platforms. C program A C program can vary from 3 lines to millions of lines and it should be written into one or more text files with extension ".c"; for example, hello.c. You can use "vi", "vim" or any other text editor to write your C program into a file. This tutorial assumes that you know how to edit a text file and how to write source code using any programming language. C Hello World Example A C program basically consists of the following parts: Preprocessor Commands Statements & Expressions Comments Let us look at a simple code that would print the words "Hello World": Let us look various parts of the above program: The first line of the program #include <stdio.h> is a pre-processor command, which tells a C compiler to include stdio.h file before going to actual compilation. The next line int main () is the main function where program execution begins. The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments in the program. So such lines are called comments in the program. The next line printf (...) is another function available in C which causes the message "Hello, World!" to be displayed on the screen. The next line return 0; terminates main () function and returns the value 0. 2.0 Steps in learning C Alphabet Digits Special symbol Constant Variables Key words Instructions Programme To learn how to write programs we must 1st know alphabet, digits and special symbols are used in C. Then by using them how constants, variable and keywords are constructed. And finally how these are combined to form an instruction. A group of instruction form a known as programme. Alphabet – A, B, C, ……………………. Y, Z A, b, c, …………………….. y,z Digits – Special symbols`– 0, 1, 2, …………………..7, 8, 9 ~‘!@#$%^ 2.1 Identifiers Identifiers files are user defined words used to name variables function etc. Names only consist of alphabetic digits and underscores sign. The first character cannot be a digit. It should not be a key word It is case sensitive 2.2 Key word There are certain words reserved for doing specific tasks. These words are known as keywords. These are predefined and always writing in low case. Example : int, default, double, flot, for, continue. 2.3 Data Types Data types refer to an extensive system used to declare variables or functions of different types before its use. The variable type determines how much space it occurs in storage and how the bit pattern stored is interpreted. Example: int, float, double, char 1 Char = 1 byte 1 int = 2 byte 1 short int = 1 byte 1 long int = 4 byte 1 float = 4 byte 2.4 Constants A constant is a value that cannot be changed during programme execution. In C any number, single character or character strip is known as a constant. It’s an entity that doesn’t change where as a variable in as entity that will change. Numeric constant These consist of digits. It’s required minimum size 2 bytes and maximum of 4 bytes. The allowance range for integer constants is – 32768 to + 32767 Real constant These have a decimal point and either be positive or negative. Character constant Character constant represented as a single character enclosed within a single quote. Example: ‘q’ , ‘$’ String constant A set of characters are called string and when a sequence of characters are enclosed within a double quote, it’s called a string constant. Example: “ Hello World ! ” Symbols constant A symbolic constant is a name that substitute for a sequence of character that cannot be changed. These are generally defined at the beginning of the programme. Example: #define Max 10 2.5 Variables A variables is a data name which is used to store some data value or symbolic name for string programme, computation and results. The value of the variable can be changed using the programme institution. The rules for naming variables is the same as making the identity file Declaration of variables specify its name and data type. When a variable is declared but contain undefined values are called garbage values. Values can only be assigned when declaring variables. This is called initialization of variable. Example: int v1; V1=0; 2.6 Expressions An Expressions is a combination of variables, constant, operators and functions. It’s can be arithmetic logical and relational. Example: int v1; v1 = 50 +25 a > b; a = = b; 2.7 Operators An operator is a symbol that tells the compiler to perform specific mathematical or logical functions.. Arithmetic operator This operator is used for numeric calculations. These are of eight numeric arithmetic operator and binary arithmetic operator. Example: Following table shows all the arithmetic operators supported by C language. Assume variable A holds 10 and variable B holds 20, then: Assign Operators A value can be stored in a variables with the use of an assignment operator. The assignment operator (=) is used in assignment statement and assignment expression. Increment and discriminant The increment operator increases the value of variables by 1 and the decrement operator decreases the value of a variable by 1. Example: Increment operator: Decrement operator: ++ x ; or x ++ ; – – x ; or x – – ; Relational operator This is used to compare value of two expressions depending on the relation. Expressions that contain a relation operator is called a relation expression. Example: Following table shows all the relational operators supported by C language. Assume variable A holds 10 and variable B holds 20, then: Conditional operator Conditional operators return one value if condition is true and returns another value is condition is false. This operator is also called as ternary operator. Example: (Condition? true_value: false_value); (A > 100 ? 0 : 1); In above example, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if else conditional statements. Operator precedence This is used to permits different expressions to appear in a situation where only one expression would be used. Size of operator This gives the size of operand in terms of the amount of memory it occupies. The operand may be a variable, constant or data type qualifier. Bit wise operator Bit wise operator permits a programmer to access and manipulate data at a bit level. Example: The Bitwise operators supported by C language are listed in the following table. Assume variable A holds 60 and variable B holds 13, then: These operator can operate on integer and character value but not on float and double. In ones complement all zeros (0) change to one (1) and all ones (1) change to zero (0) in bitwise OR it compare each no with their binary equivalent and outputs a value after the OR operations. Logic operator This operator used with one or more operand will return either 0 for false or 1 for true. The expression that combines two or more expressions is termed as a logical operator. Example Following table shows all the logical operators supported by C language. Assume variable A holds 1 and variable B holds 0, then: Summary on Types of C Operators: Types of Operators Description Arithmetic operators These are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus Assignment operators These are used to assign the values for the variables in C programs. Relational operators These operators are used to compare the value of two variables. Logical operators These operators are used to perform logical operations on the given two variables. Bit wise operators These operators are used to perform bit operations on given two variables. Conditional (ternary) operators Conditional operators return one value if condition is true and returns another value is condition is false. Increment/decrement operators These operators are used to either increase or decrease the value of the variable by one. Special operators &, *, sizeof( ) and ternary operators. 2.8 If Statement This statement executes a set of commands when the given condition is true. An if statement consists of a boolean expression followed by one or more statements. Syntax of an if statement in C programming language is: if (boolean_expression) { /* statement(s) will execute if the boolean expression is true */ } Exercise: