History of C? Why we use C programming language Martin Richards developed a high-level computer language called BCPL in the year 1967. The intention was to develop a language for writing an operating system(os) OS is software which controls the various processes in a computer system. This language was later improved by Ken Thompson and he gave it a new name B. The basic ideas about some topics such as arrays, etc., which were later inherited by C were developed in BCPL and B. Differentiate Between Keywords and Identifiers Keywords are special words that have special meaning in the C language and are reserved by the language. An identifier is a string of alphanumeric characters that begins with an alphabetic character or an underscore character that are used to represent various programming elements such as variables, functions, arrays, structures, unions and so on. Actually, an identifier is a user-defined word. e.g identifiers: total sum average _x y_ mark_1 x1 What is the importance of header files The main role of header file is it is used to share information among various files. To put it brief, if we have several functions say 4 functions named as f1, f2, f3, f4 placed in file say sample c and if all the functions want to get accessed each other all must be placed in the same file sample c Anatomy of C Program We have some compiler preprocessor commands. This includes various #include files. Then comes the main function. Some name can also be given to the main function. Then, we have the variable declarations used in the main code. Then we have sub-functions.
Basic Data Types and Operators The type of a variable determines what kinds of values it may take on. An operator computes new values out of old ones. An expression consists of variables, constants, and operators combined to perform some useful computation. There are only a few basic data types in C char a character int an integer, in the range -32,767 to 32,767 long int a larger integer (up to +-2,147,483,647) float a floating-point number double a floating-point number, with more precision and perhaps greater range than float Constants Constants are also like normal variables. But, only difference is, their values can not be modified by the program once they are defined. Constants refer to fixed values. They are also called as literals. Constants may be belonging to any of the data type We can define constants in a C program in the following ways. By const keyword By #define preprocessor directive
TYPES OF C CONSTANT: 1. Integer constants 2. Real or Floating point constants 3. Octal & Hexadecimal constants 4. Character constants 5. String constants 6. Backslash character constants RULES FOR CONSTRUCTING C CONSTANT: 1. INTEGER CONSTANTS IN C: An integer constant must have at least one digit. It must not have a decimal point. It can either be positive or negative. No commas or blanks are allowed within an integer constant. If no sign precedes an integer constant, it is assumed to be positive. The allowable range for integer constants is -32768 to 32767. 2. REAL CONSTANTS IN C: A real constant must have at least one digit It must have a decimal point It could be either positive or negative If no sign precedes an integer constant, it is assumed to be positive. No commas or blanks are allowed within a real constant.
3. CHARACTER AND STRING CONSTANTS IN C: A character constant is a single alphabet, a single digit or a single special symbol enclosed within single quotes. The maximum length of a character constant is 1 character. String constants are enclosed within double quotes 4. BACKSLASH CHARACTER CONSTANTS IN C: There are some characters which have special meaning in C language. They should be preceded by backslash symbol to make use of special function of them. Given below is the list of special characters and their purpose.
#include <stdio.h> void main() { const int height = 100; /*int constant*/ const float number = 3.14; /*Real constant*/ const char letter = 'A'; /*char constant*/ const char letter_sequence[10] = "ABC"; /*string constant*/ const char backslash_char = '\?'; /*special char cnst*/ printf("value of height :%d \n", height ); printf("value of number : %f \n", number ); printf("value of letter : %c \n", letter ); printf("value of letter_sequence : %s \n", letter_sequence); printf("value of backslash_char : %c \n", backslash_char); } Variable 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. e.g. int i, j, k; char c, ch; float f, salary; double d;
#include <stdio.h> // Variable declaration: extern int a, b; extern int c; extern float f; int main () { /* variable definition: */ int a, b; int c; float f; /* actual initialization */ 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
Arithmetic Operators The basic operators for performing arithmetic are the same in many computer languages: Assignment Operators The assignment operator = assigns a value to a variable. For example, x = 1 a = b sets x to 1, and sets a to whatever b's value is.
Relational Operators Logical Operators
Increment and Decrement Operators Increment and decrement operators are unary operators that add or subtract one from their operand, respectively. They are commonly implemented in imperative programming languages-like languages feature two versions (pre- and post-) of each operator with slightly different semantics. Pre Increment, Decrement & Post Increment, Decrement Operators int x; int y; // Increment operators x = 1; y = ++x; // x is now 2, y is also 2 y = x++; // x is now 3, y is 2 // Decrement operators x = 3; y = x--; // x is now 2, y is 3 y = --x; // x is now 1, y is also 1