P. 1


|Views: 5|Likes:
Published by Laxman Thapa

More info:

Published by: Laxman Thapa on Aug 19, 2013
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less





C Programming

C is a procedural programming language developed in 1972 by Denis Ritchie at AT and T’s Bell Laboratory in USA.

Character Set:
o o o o o C uses the uppercase (A-Z) Lowercase (a-z) Digits(0-9) Special Symbols: - ~ ‘! @ # % ^ & * () _ - + = | \ { } [] : ; “ ‘ <> , . ? / White space, blank space, horizontal tab.

Keywords are reserved words in c that have predefined meaning. Keywords are in lower case, they can be used only for their intended purpose. E.g. for, do, int, if etc.

Identifiers are the names given to various program elements such as variables, function etc. These are user defined names and consist of sequence of letters and digits. The first character of an identifier should always be a letter. Underscore (_) character may also be used. Upper & lowercase are distinct.

A program elements that have fixed value in C i.e. value do not change during the executing of the program.

Basic Data Types:
Data types decide the amount of memory to be allocated to a variable to store particular types of data. The data types used in C are as follows:  Integers (int):-It stores number value without any decimal point. The OS allocates 16 bits (2 bytes) to a variable that is declared to be of type int.  Floating point (float):-It stores only numeric value with decimal point. It can hold the decimal point upto 6 digits. The variable that is declared to be of type float is allocated 4 bytes of memory.  Double:-It stores only numeric value but the size is large than that of int and float. It can hold decimal point upto 10 digits. A variable declared of type double is allocated 8 bytes of memory.  Character (char):- It stores only one character at a time. Its size is in bit. 1 char =1 Bit  Boolean:-Besides text and numeric data, we also often come across logical data such as True and False. Such data is referred to as logical data. To be able to store this type of data, a variable needs to be declared of type Boolean. It occupies a space of 2 bytes.

Derived data types:
The data types which have been derived from basic data types are known as derived data types. There are three types of derived data types:  Unsigned:-This data type can be used with the int data types. Declaring a variable using unsigned permits only positive numbers to be stored. It is declared as: unsigned int num; num=12345; 1

For string.Scanf returns the number of data items which have successfully been assigned a value. gets():-The gets function takes a string as its argument. Escape Characters: Escape Characters are used with the printf( ). getch( ) / Getche( ):-Getch( ) and getche( ) wait for a keypress and the return . \n =For new line \t = For horizontal tab. It occupies only 1 bytes space. “%s”:. Conio:-console input output.h> 2 . Int – Data type a Variable Header Files in ‘C’ Programming:    #include:-It is a preprocessor directives. Stdio:-Standard input output. “%lf”:. It is declared as: long int num. User Input and Output commands:       “%d”:. & =ampersand is address of operator or the location of the variable where the variable is stored.For single character. The escape characters are always preceded with a backspace (\). Clrscr( ):. When it is used with int. “%c”:.Clear the screen. “%ld”:.For double. it occupies 4 bytes and when used with double. getchar ( ):-Reads a character from the keyboard. Scanf( ):. waits for carriage and then return. Input output Statements:         Void main():-This line of the program indicates the point at which the working of the program begins. Program-1 #include<stdio.h> #include<conio.  Long:-This data type can be used with int as well as double data types. it occupies 16 bytes space.For integer input and output.For long integer. Printf( ):-Printf is used to print or display data on the console in a formatted form. Escape Characters are specified in the first argument of the printf( ) and are used mainly for screen formatting of the output. Variable: A variable is a named location in memory where a particular value is stored. puts( ):-Thes puts function takes a string as its argument and writes it on the screen. Short:-This data type can be used with int data type. It modifies the basic int data type so that it occupies less memory place.For floating point value. or long double num. It returns the number of characters written or a negative value if an error occurs. “%f”:.

\n:-new line: #include<stdio. The symbol + in the statement is called the operator and the variables on whom the operation is performed are called operands.h> #include<conio. } Program-2 The use of: \t:-tab. getch(). The combination of the operands is known as an expression.void main() { printf(“Welcome to C programming”). Operators can be classified as: o Arithmetic Operators :• + Plus • Minus • * Multiply • / Divide • % Modulus (To get the remainder) o Assignment Operators:• = Equal to • += Plus and Equal to • -= Minus and Equal to • *= Multiply and Equal to • /= Divide and Equal to • %= Modulus and Equal to o Comparison operators:1) > Greater than 2) < Less than 3) >= Greater and Equal to 4) <= Less and Equal to o Equality Operator:1) == Equal to 2)! = Not Equal to o Logical Operator:1) And (&&) 2) Or (or) 3) Not (!) 3 . Consider the following statement: A+B This would add the contents of the two variables A and B.h> void main() { printf(“My\t name\t is\t abc”). } Operator in ‘C’: Operators are a set of symbols that help manipulate or perform some sort of function on data. printf(“I Live in Nepal\n”).

Printf(“%f”.floats  %h:./. it is evaluated in the beginning and the rest of the process takes place and if we have a block of parenthesis.= Whenever we use the parenthesis”()” in any of the expression.n1n2. N1=4. the innermost block is evaluated first and then the outer one and then the next. N2=5. Program-1(i) #include<stdio.integer  %f:.then printf(“the addition of %d and %d is %d”. n1=4.res). //to show the value of n1 and n2. Conversion character: They indicate the type of data item.h> #include<conio.  %c:. } //To keep only 2 digits after decimal type Printf(“%2f”.*./ 2nd priority:.+.5.res).5.string character  %d:. printf(“%d. //after printf.-. } Program-1(ii) Float n1.res.octal integer  %x:.h> void main() { int a.h> void main() { int n1.b.res).string  %o:. Res=n1+n2.3rd priority:.short integer  %s:.n2. res=n1+n2. //To get the result after executing type Getch().=): 1st priority:. n2=6.n2.*.c.Hierarchy of operators (+.res.hexadecimal. Program-2 #include<stdio. 4 .res).

a) If else statement b) switch statement c) Continue statement d) Goto statement (2) Looping statement. a) For loop b) While loop c) do-while loop. b=a. printf(“Enter the second number: “). and the rest of the ladder is bypassed. If –else – if Statement:This is another common programming construct adapted from the initial if statement. &a).h> void main() { int num. } Program-3 #include<stdio.b. If the condition is true it goes to the very first output statement otherwise it goes to else statement. scanf(“%d”. c=a+b. printf("Enter a number"). the statement evaluates and execute. a=5.c). If the condition is true.printf(“Enter the first number : “ ). printf(“value of b : %d”.h> #include<conio.b). &b). printf(“value of b : %d”. scanf("%d".&num). i=num%2. clrscr(). scanf(“%d”. } Control statements: (1) Conditional statements. printf(“The sum of numbers is %d” .i. b=a++. if(i==0) { 5 .h> void main() { int a. If-else:In this conditional statement we compare the two or more in a single or multiple statement.b). Example of if:#include<stdio.

} Example of if-else:#include<stdio. clrscr().h> #include<conio.i. } Switch Statement:Switch is a multiple secession maker. if(i==0) { printf(“Even number!”). break. 6 . printf("Enter the days of a week:"). Break also terminate the whole program.h> void main() { int day. In switch the conditions are applied in case statement. scanf("%d".h> #include<conio.&num). In switch there is a default case which is used to store default value. break. clrscr(). break. } else { printf(“Odd number!”). } getch(). The case will be integer or character type not a float integer string type. switch(day) { case 1: printf("Sunday"). Every case statement is terminate with break statement. If all the conditions are false the default output will be display. case 3: printf("Tuesday"). scanf("%d". Example: #include<stdio. i=num%2. case 2: printf("Monday").printf(“Even number!”).h> void main() { int num.&day). printf("Enter a number"). } getch().

case 6: printf("Friday").First increment than initialization. break. } Looping Statement: A. 1) Initialization 2) Condition 3) Increment or decrement operator. (2) Inner Loop: .Under First Loop. Example-1 #include<stdio.i++) { printf("%d\n". break. For Loop:The For Loop continues to execute as long as the conditional test evaluates to true. } getch(). Increment 1) Postfix Increment: .i<=10. break.First initialization than increment. Nested For Loop (1) Outer Loop: .h> void main() 7 . clrscr(). } Example-2 #include<stdio. case 5: printf("Thursday"). break. break.i).h> #include<conio.case 4: printf("Wednesday"). } getch(). case 7: printf("Saturday").h> void main() { int i. default: printf("Invalid day number!"). When the condition becomes false the program comes out of the loop. 2) Prefix Increment: .h> #include<conio. for(i=1. There are three expressions in for loop.First Loop.

} C.{ int a.b++) { printf("*"). i=0.b<=a. Example: #include<stdio.h> void main() { int i. then will repeat the loop as long as a condition is true. clrscr(). printf("%d\n". } printf("\n").i).h> void main() { int i.a<=5. Do-while loop:The do-while statement will execute a block of code at least once. } while(i<10).b. } B. printf("%d\n". do { i=i+1. clrscr(). for(a=1.h> #include<conio. While Loop:The while loop repeats a statement or a set of statements while a certain specified condition is true. } getch().a++) { for(b=1. Example loop: #include<stdio. i=0. } getch().h> #include<conio. getch(). while(i<10) { i=i+1. 8 .i).

9 . printf("\nYour phone is :%d". d). int i. getch(). } Example-2 #include<stdio. b). Example-1 #include<stdio. We define the size of array is in square bracket. int marks[5]. printf("Enter your name : "). specifying the array dimension. means the first value of array is in the position of zero. &e). In ‘C’ there are three types of array: (1) One dimensional Array (2) Two dimensional Array (3) Multiple dimensional Array The most commonly used Array is one dimensional array. clrscr(). s=0. printf("\nYour roll no is :%d". printf("\nYour address is :%s". except that the array name is followed by one or more expressions. a). &a).} Array: An array is defined in the same way as a variable is defined. printf("Enter your roll no : ").h> #include<conio. printf("Your name is :%s". printf("Enter your phone no : "). scanf("%d".b. c). scanf("%d". scanf("%s".h> void main() { float a.d[10]. Array always starts with ‘0’ (zero) index. e). Array is a collection of same types of data in a single variable. char c[10]. &b).i++) { printf("Enter the marks : "). scanf("%d". clrscr().h> void main() { int a. scanf("%s". for(i=0.&c).i<=4.e. printf("Enter your age : "). printf("Enter your address : "). printf("\nYour age is :%d". enclosed within square brackets [ ]. Array declares with square bracket. scanf("%d".&d). &marks[i]).

Check the given character is alphanumeric or not.i<=4. (2) strcat( ) :-Concat the two string in a single string.<ctype. (4) islower( ) :.Find the total length of any string. (4) strcmp( ) :-compare the two string equal or not. (3) strlen( ) :. a). Only one parameter in this function.Return the raised to the power. The pointer points the address of another variable. a=s/5. (5) strupr( ) :. 10 . (6) tolower( ) :. sin(a) String Function: (1) strcpy( ) :-Copy the value of one string of another string. (6) strlwr( ) :.Convert upper case character to lower case character. Character Function: (1) isalpha( ) :-Check the given character is alphabet or not.h> 2) String function. Library / System. well defined task and used to break a large program into different parts to solve and find errors. getch().i++) s=s+marks[i]. printf("Total marks = %f". //defines i is a pointer to the integer. Function Declaration. (3) Isalnum():.s).Return the sin value. User defined 2.h> Mathematical Function: (1) abs( ) :. Library Function: There are three types of library functions:1) Mathematical Function. User defined function: 1. (4) sin( ) :.Convert lower case string to upper case. Two parameters in this function.Return square root. Function Definition. Only one parameter in this function. 3. Function Call. sqrt(a) (3) pow( ) :. a[i] is equivalent to *[a+i] Function in ‘C’: A function is a self-contained program segment that carries out a specific. <math. There are two types of functions:1. Pointer declaration: int *i.Return absolute value.Check the given character is digit or not.convert upper case string to lower case. (2) sqrt ( ):. (5) isupper( ) :-Check the given character is in upper case or not. (2) isdigit( ) :.check the given character is in lower case or not. 2. Only parameter in this function. } Pointer: A pointer is a special type of variable because it doesn’t store the values but the address of another variable.h> 3) Character Function. printf("\nAverage marks = %f".<string. read easily.} for(i=0.

In structure keyword we can define all variables if the variables are declare within structure these structure call by an object which is the reference of structure. } Example-2 #include<stdio.h> #include<conio. clrscr(). return(d). getch(). void main() { int a.] operator. printf("%d". sum=calsum(a. } Structure in ‘C’: Structure is collections of variables of different data types that can be accessed as one unit using a common name. Example-1 #include<stdio.h> #include<conio. } int calsum(int x. &a.&c). int y. printf("%d\n". To call the structure in program we use dot [. d=x+y+z.c).h> int calsum(int x.(7) Toupper() :-Convert lower case character to upper case character. b).a).int y.c. getch(). a=30. printf("\n sum=%d".b. int z).b. printf("Enter any three numbers").h> void main() { int a. { b=60. fun(a). scanf("%d%d%d".int z) { int d. return 0.sum. sum). clrscr(). To define any structure we use a struct keyword. Declaring structure: 11 .&b. } fun(b) int b.

printf("\nEnter breadth:").area). } Example: #include<stdio. void main() { int area.length*rect.&rect. printf("Enter length:").h> struct rectangle { int length. char anyname.breadth.struct anyname { int anyname. struct rectangle rect. scanf("%d". printf("The area is:%d". getch(). area=rect.&rect.length). } 12 .breadth). scanf("%d". int breadth. }. clrscr().h> #include<conio.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->