A Basic C Program Every C program must have one special function main ().

This is the point where execution begins when the program is running. We will see later that this does not have to be the first statement in the program, but it must exist as the entry point. The group of statements defining the main () enclosed in a pair of braces ({}) are executed sequentially. Each expression statement must end with a semicolon. The closing brace of the main function signals the end of the program. The main function can be located anywhere in the program but the general practice is to place it as the first function. Here is an elementary C program. main () { } There is no way to simplify this program, or to leave anything out. Unluckily, the program does not do anything. Following the "main" program name is a couple of parentheses, which are an indication to the compiler that this is a function. The 2 curly brackets { }, properly called braces, are used to specify the limits of the program itself. The actual program statements go between the 2 braces and in this case, there are no statements because the program does absolutely nothing. You will be able to compile and run this program, but since it has no executable statements, it does nothing. Keep in mind however, that it is a legal C program. main ( ) function should return zero or one. Turbo C accepts both int and void main ( ) and Turbo C coders use both int and void main ( ) in their programs. But in my belief, void main ( ) is not a standard usage. The reason is, whenever a program gets executed it returns an integer to the OS. If it returns 'zero', the program is executed successfully. Otherwise it means the program has been ended with error. So main ( ) shouldn't be declared as void.d as void. main( ) should be declared as

int main( ) { …………….. …………….. return 0 ; } For a much more interesting program, load the program int main () { printf (“Welcome to C language”); return 0; } and display it on your monitor. It is same as the previous program except that it has one executable statement between the braces. The executable statement is another function. Again, we won't care about what a function is, but only how to use this one. In order to output text to the monitor, it's placed within the function parentheses and bounded by quotes. The end result is that whatever is included between the quotes will be showed on the monitor when the program is run. Notice the semi-colon; at the end of the line. C uses a semi-colon as a statement terminator, so semi-colon is required as a signal to the compiler that this line is complete. This program is also executable, so you'll be able to compile and run it to see if it does what you think it should. With some compilers, you may get an error message while compiling, indicating that the

This means that I can arrange libraries of my own routines and use them in my programs. A function is simply a group of related statements that we can use later.h> int main () { printf (“Welcome to C language”). at this time clrscr(). One more thing to remember while using clrscr() is that it should be called only after the variable declaration. what things it can work on and what value it returns.h. Your program will almost certainly want to send stuff to the screen and read things from the keyboard. If I had given the name "abc. It is a pre-processor directive.h. Imagine you run above program and then change it and run it again you may find that the previous output is still stucked there itself. Here the function we used is printf .function printf () should have a prototype.e. #include<stdio. This is the name of the standard library definition file for all Standard Input Output. The <> characters around the name tell C to look in the system area for the file stdio. It says the C compiler to include the contents of a file. a common cause of programs crashing. return 0.h> #include<conio. To use printf correctly C needs to know what it looks like. would clear the previous screen.h" instead it would tell the compiler to look in the current directory.h is the name of the file in which the functions that we want to use are defined. It's not a part of our program. like . it's an instruction to the compiler to make it do something. in this case the system file stdio. The actual code which performs the printf will be tied in later by the linker. i. } Here you'll be able to see #include at the beginning of the program. This can lead to the call failing when the program runs. Note that without the definition of what printf looks like the compiler makes a guess when it sees the use of it. stdio.

int p.If you want to see the line you can use getch() function just below the printf() statement. return 0. Actually it waits until a key is pressed. } When you execute above program you won’t see ‘Welcome to C language’ on the console because the screen will just flash and go away .q.r. clrscr() Example : #include<stdio.h> #include<conio. printf (“Welcome to C language”).h> int main () { clrscr(). Example: .

The underscore character (‘_’) is considered as letter. they're not interchangeable. float. No special characters. symbolic constants and functions.period. Identifier name must be a sequence of letter and digits. 5. However. if . and must begin with a letter. slash or comma are permitted . 3. for etc) 4. No identifier may be keyword. Names shouldn't be a keyword (such as int .Turbo C++ IDE Home Identifiers cIdentifiers Identifiers are the names that are given to various program elements such as variables. Both upper-case letter and lower-case letter characters are allowed.break. 2. such as semicolon. Here are the rules you need to know: 1. 6.blank space.

float for. Keywords are standard identifiers that have standard predefined meaning in C.14. Keywords are all lowercase. first some legal identifiers: float _number. The keywords can't be used as names for variables. Keywords can't be used as programmer defined identifier. The following are illegal (it's your job to recognize why): float :e.3. Keywords can be used only for their intended purpose. int this_is_a_very_detailed_name_for_an_identifier. float a. Sometimes these are called "primitive" types. since uppercase and lowercase characters are not equivalent it's possible to utilize an uppercase keyword as an identifier but it's not a good programming practice. float . Points to remember 1. float 7g. minimal set of basic data types. A lot of complex data structures can be . The standard keywords are given below: Data Types C offers a standard. float 9PI. 3. 2.Examples of legal and illegal identifiers follow.

These qualifiers can be applied to the data types int and char only. The C language defines 4 fundamental data types: character integer floating-point and double floating-point This data types are declared using the keywords char. 1.float and double respectively. . Data type qualifiers can be classified into two types. Typical memory requirements of the basic data types are given below The size and range of these data types may vary among processor types and compilers. size qualifiers 2. The minimum size of a long int is 32 bits. There are two size qualifiers that can be applied to integer: short and long.int.developed from these basic data types. Sign qualifiers: The keywords signed and unsigned are the two sign qualifiers that specify whether a variable can hold both –ve and +ve numbers. Data type qualifiers modify the behavior of variable type to which they are applied. Example: unsigned int I. sign qualifiers Size qualifiers: Size qualifiers alter the size of the basic data types. The size of int must be greater than or equal to that of a short int. The size of long int must be greater than or equal to a short int. or only +ve numbers.two types. The minimum size of short int is 16 bit.

and so on. and signed char permits values from -127 (or -128) to 127. unsigned char permits values from 0 to 255. 67 to C. char char is a special integer type designed for storing single characters. but unsigned on others the sizes of the char types are as follows. E.. Type char unsigned char signed char Example: Bytes 1 1 1 Minimal range -127 to 127 0 to 255 -127 to 127 . The char type is signed by default on some computers. The integer value of a char corresponds to an ASCII character. along with their minimal ranges and typical memory size.g.The following table shows all valid data type combinations supported by C. 66 corresponds to B. As in the table below. a value of 65 corresponds to the letter A.

Float Floating point numbers are numbers with a decimal point. If a number can be calculated to three decimal places. The float type can take large floating point numbers with a small degree of precision (Precision is simply the number of decimal places to which a number can be calculated with accuracy. it is said to have three significant digits.) Memory size : 4 bytes Minimal range : IE-38 to IE+38 with six digit of precision Example: .

The sizes of the double types are as follows.Double Double-precision floating point numbers are also numbers with a decimal point. Type Bytes Minimal range IE-38 to IE+38 with 10 digit of precision IE-38 to IE+38 with 10 digit of precision doubleprecision 8 long double Example: 8 . We know that the float type can take large floating point numbers with a small degree of precision but the double-precision double type can hold even larger numbers with a higher degree of precision.

If either sign does not precede the constant it is assumed to be positive. The value of a constant cannot exceed specified minimum and maximum bounds.String constants Integer and floating-point constants represent numbers. • .Integer constants 2. They are often referred to as numeric-type constants. these bound vary from one C compiler to another. The following rule applies to all numeric type constants: • • Comma and blank spaces cannot be included within the constants.Constants The term constant means that it does not change during the execution of program.Character constants 4. if desired. They are: 1. For each type of constant. There are four basic types of constants in C. Constants can be preceded by a – or + sign.Floating-point constants 3.

.001 103. If the decimal constant contains two or more digits. Octal and Hexadecimal.).Integer constants Integer constants are whole numbers without any fractional part. Illegal decimal point (.0 10 20 30 045 Illegal character (.). The following are valid decimal integer constants. Thus integer constants consist of a sequence of digits. Illegal character (blank space). Integer constants can be written in three different number systems: Decimal. the first digit must be something other than 0. First digit cannot be zero. A decimal integer constant consists of any combination of digits taken from the set 0 through 9. 0 Example: 1 1234 -786 The following decimal integer constants are written incorrectly for the reasons stated. 12.

). 0 Example : 01 0125 0555 Introduction Turbo C++ IDE A Basic C Program Fundamentals Input/Output Functions Control Statements Arrays Strings Functions Storage Classes Structure Union Pointers The following decimal integer constants are written incorrectly for the reasons stated. the first digit must be 0. However.10 Does not begin with 0. Illegal digit (8). in order to identify the constant as an octal number. Illegal character (. It can then be followed by any combination of digits taken from the set 0 through 9 and A through F (either upper-case or lowercase).Example: An octal integer constant can consist any combination of digits taken from the set 0 through 7. The following are valid hexadecimal integer Standard Library Functions ASCII Table . The following are valid octal integer constants. 2010 2008 05. Dynamic memory allocation File Graphics The C Preprocessor A hexadecimal integer constant must begin with either 0x or 0X.

). A floating-point constant can be written in two forms: Factorial form or Exponential form. A floating-point constant in a fractional form must have at least one digit each to the left and . 0x20.10 02008 0xabcdefg Illegal character (. 0X0 Example: 0x1 0XAB125 -0x555 The following hexadecimal integer constants are written incorrectly for the reasons stated. Constants Home Floatingpoint constants Floating-point constants A floating-point constant is a base-10 number that contains either a decimal point or an exponent or both. Illegal character (g). Does not begins with 0x or 0X.constants.

20 Exponent must be an integer. The following are valid floating-point constants.written incorrectly for the reasons stated.</1.).right of the decimal point.&NBSP.&NBSP.&NBSP.00. The exponent must be a decimal integer. The mantissa itself is represented as a decimal integer constant or a decimal floating-point constant in fractional form.&NBSP.&NBSP. 1 2.&NBSP.&NBSP.&NBSP. The letter E or e and the exponent follow the mantissa. A floating-point in exponent form consists of a mantissa and an exponent. span -0. E10 Illegal character (space).0&NBSP. E+10.1 > The following floating-point constants are written incorrectly for the reasons stated.1555e-4< 2E-4 0.&NBSP .&NBSP. Illegal character (. The actual number of digits in the mantissa and the exponent depends on the computer being used.&NBSP.8 2 3 Example: No decimal point or exponent. Integer constants Home Character constants Character constants .

A character constant is a single character. “Welcome to C Programming” “a+b\n” “” “Error\a\a\a” . The value of a character constant is the numeric value of the character in the machine’s character set. ‘A’ ‘B’ ‘1’ Characters are stored internally in computer as coded set of binary digits. Several string constants are given below. which have positive decimal integer equivalents. Example: String constants A string constant consists of zero or more character enclosed in quotation marks. For example. This means that the value of a character constant can vary from one machine to the next.. enclosed in single quotation marks. on ASCII machine the value of ‘A’ is 65 and on EBCDIC machine it is 193.g. e. depending on the character set being used on the particular machine.

the first ‘A’ is a character constant while the second “A” is string constant. we declare a variable of type character with the name i by writing: . For example.that terminates all the string. on personal computer the size of an integer variable is two bytes. The notation ‘A’ is a constant occupying a single byte containing the ASCII code of the character A. and that of a long integer is four bytes.is a constant that occupies two bytes one for ASCII code of A and another for the null character with the value 0. The notation “A” on the other hand . Example: Variables Variables are means for location in memory used by a program to store data. For example. the name of the variable and the type of the variable. The size of that block depends upon the range over which the variable is allowed to vary.There is a difference between the constant ‘A’ and “A” in C. In C the size of a variable type such as an integer need not be the same on all types of machines. When we declare a variable we inform the compiler of two things.

b. 4. the value 'x' will be placed in that memory location reserved for the storage of the value of i. All variables must be declared before they can appear in executable statement. int b. 2. It also sets up a symbol table.c. Following are the rules for naming the variables: 1. we expect that. The above declaration can be written as int a. 5. Example: short int a.c. An integer variable can also be declared to be un signed by writing unsigned int. . Example: int a.b.c. Thus.b. 3. long int a.char i.at run time when this statement is executed. Integer type variables can be declared to be short integer for smaller integer quantities or long integer for larger integer quantities. A declaration consists of a data type followed by one or more variable names separated by commas. In that table it adds the symbol i and the relative address in memory where those one byte was set aside. Variables can be distributed among declarations in any fashion.c. On seeing the "char" part of this statement the compiler sets aside one bytes of memory to hold the value of the character. later if we write: i = 'x'.

The size of that block depends upon the range over which the variable is allowed to vary. Following are the rules for naming the variables: 1. Thus. Example: int a. on personal computer the size of an integer variable is two bytes. For example. the value 'x' will be placed in that memory location reserved for the storage of the value of i. and that of a long integer is four bytes. A declaration consists of a data type followed by one or more variable names separated by commas.b. In that table it adds the symbol i and the relative address in memory where those one byte was set aside. 2. we expect that. On seeing the "char" part of this statement the compiler sets aside one bytes of memory to hold the value of the character.at run time when this statement is executed.c.Example: unsigned int. Variables Variables are means for location in memory used by a program to store data. later if we write: i = 'x'. the name of the variable and the type of the variable. When we declare a variable we inform the compiler of two things. we declare a variable of type character with the name i by writing: char i. In C the size of a variable type such as an integer need not be the same on all types of machines. For example. All variables must be declared before they can appear in executable statement. . It also sets up a symbol table.

For example. 5. Example: unsigned int. For example. on personal computer the size of an integer variable is two bytes.c. Variables Variables are means for location in memory used by a program to store data. Example: short int a.c. long int a. 4.b. When we declare a variable we inform the compiler of two things. . The size of that block depends upon the range over which the variable is allowed to vary. In C the size of a variable type such as an integer need not be the same on all types of machines. and that of a long integer is four bytes. An integer variable can also be declared to be un signed by writing unsigned int. Variables can be distributed among declarations in any fashion. Integer type variables can be declared to be short integer for smaller integer quantities or long integer for larger integer quantities.3. we declare a variable of type character with the name i by writing: char i.c. the name of the variable and the type of the variable. The above declaration can be written as int a.b. int b.

c. Example: int a. Integer type variables can be declared to be short integer for smaller integer quantities or long integer for larger integer quantities. Example: unsigned int. 5. An integer variable can also be declared to be un signed by writing unsigned int. Following are the rules for naming the variables: 1.at run time when this statement is executed. All variables must be declared before they can appear in executable statement.c.b. 3. Thus. the value 'x' will be placed in that memory location reserved for the storage of the value of i. A declaration consists of a data type followed by one or more variable names separated by commas. long int a. In that table it adds the symbol i and the relative address in memory where those one byte was set aside. 4. Variables can be distributed among declarations in any fashion. int b.c.c.b.On seeing the "char" part of this statement the compiler sets aside one bytes of memory to hold the value of the character. we expect that. Example: short int a. It also sets up a symbol table. later if we write: i = 'x'. .b. 2. The above declaration can be written as int a.

. In C every expression evaluates to a value i. Thus logical type expression actually represents numerical quantities. every expression results in some value of a certain type that can be assigned to a variable.14*r*r a*a+2*a*b+b*b Example: Statements The statements of a C program control the flow of program execution. Some examples of expressions are shown in the table given below. They are if statement switch statement goto statement for statement while statement do-while statement break statement continue statement expression statement compound statement . All expression represents a logical connection that's either true or false. An expression may consist of single entity or some combination of such entities interconnected by one or more operators.e. A+b 3.Expressions An expression is a sequence of operators and operands that specifies computation of a value.In C language several kinds of statements are available.

preprocessor constants are written in UPPERCASE. each occurrence of a symbolic constant is replaced by its corresponding character sequence. The symbolic constants may then appear later in the program in place of the numeric constants. character constants. For example.141593 #define TRUE 1 #define FALSE 0 #define PI 3. etc.return statement null statement Expressions Home Symbolic constant Symbolic constants A symbolic constant is name that substitute for a sequence of character that cannot be changed. that the symbolic constants represent.141593 defines a symbolic constant PI whose value is 3. and are not end with a semicolon. a C program consists of the following symbolic constant definitions. . By convention. The character may represent a numeric constant. or a string.. Note that the preprocessor statements begin with a #symbol. Symbolic constants are usually defined at the beginning of the program.141593. a character constant.141593. When the program is compiled. all occurrences of the symbolic constant PI are replaced with the replacement text 3. When the program is preprocessed. #define PI 3.