Study Material 1.pdf
Document Details
Uploaded by Deleted User
Tags
Full Transcript
Programming in C Characterstics of C program It is a structured programming language (It allows modularization process of dividing problems into sub-problems C is a middle level language (C is a high level language but also inco...
Programming in C Characterstics of C program It is a structured programming language (It allows modularization process of dividing problems into sub-problems C is a middle level language (C is a high level language but also incorporates features of low-level languages like manipulation of bits, bytes, words and addresses.) Helps in development of system software ( C is used for writing application programs and since it has low level languages features it can be used in development of system software) Has rich set of operators and data types and built-in functions. Provides compact representation for expressions. This makes the language simple, orderly and easy to learn. No rigid format. Any number of statements can be typed in a single line. Portability: any C program can be run on different machines with little or no modifications. Less number of reserved words Structure of C program Comments Preprocessor Direcives Global Variable int main() { Local variables Statements ………. ………………. } Func1() { Local variables Statements …………. } Func2() { Local variables Statements …………. } Comments can be placed anywhere in the program enclosed between delimeter OR //. It is used to documentation Purpose Preprocessor directives are preprocessed through preprocessor before C source code passes through Compiler. The commonly used preprocessor is # include, it is used for including Header file Every Program has one or more functions. If a Program has only one function then it must be a main (). Execution of every C program start with main() function. The main() function having 2 part. Declaration of local variable and Statements. Statements in the main function executed one by one. At the end of main() the function has terminated sucesfully Other functions called user defined function,which is also having local variable and global variable.They can be defined before or after main(). It may be possible that some variable are used in many function, so it is necessary to declare them globally. VARIABLE : It is a named memory location which is used to store or hold the value. The value which is entered is not fixed. It varies during their execution DATA TYPE: It is used to represent what type of data that the user is going to enter. Ex: int, float ,char, double Keywords or Reserve Words It has predefined meaning ,which is used for specific tasks.32 Keywords are their in C prg Ex: break, if, for, else, do, while, int, switch….. Identifier Identifier are user defined word and are used to give names to entities like variable,array,function etc. Rules for Creating variable 1) The name can consist of alphabets,digits and underscore sign(_) 2) First Character Should be a alphabet or underscore 3) The name should not be a keyword 4) C is a case sensitive so upper case and lower case are different. 5) Multiple Declaration of same name not allowed 6) Blank space (White space)is not allowed between word Valid Variable Num, num1, max_marks, MARKS,_data Invalid 1bca-- First character should not begin with number switch--- Keyword should not be a variable name jss# --- Special character not allowed(only _ can permit) jss college-- Space is not allowed Variable declaration It is must to declare variable before used in the program. Declaration of a variable specifies its name and data type Syntax datatype variblename; datatype may be int,float,char,double etc Ex: int x; float y; char grade; int a,b,c; Initialisation of Variable When a variable is declared it contain undefined value commonly called as Garbage value. We can specify some initial value to the variable during declaration itself that is called as initilisation of the variable Ex: int a=5; float b=6.5; Expression An expression is a combination of operator,constant,variable Ex: sum=a+b; c==d; x>y Control String or Format Specifier %d , %i Integer %c Character %f Float %s String Reading Input Data Input data can be entered into the memory from a standard input device(Keyboard). The scanf() library function can be used for entering input data. This function can take all types of values(Numeric,character,string)as input scanf(“control string”,address1,address2,….); This function should have atleast 2 parameter. First parameter is control string, write that it in double quote “ “ Control string may be one or more, depend on the number of variable we want to input. Address of a variable is found preceding by an ampersand sign(&). The string variable is not preceding by an & sign Ex1: # include int main() { int marks; ----- Scanf(“%d”,&marks); } Ex2: # include int main() { float per; ----- Scanf(“%f”,&per); } Ex 3: # include int main() { char ch; ----- Scanf(“%c”,&ch); } Ex 4: # include int main() { int x,y; ------ scanf(“%d%d”,&x,&y); } Ex 5: # include int main() { int bs; float hra; char grade; ------ scanf(“%d%f%c”,&bs,&hra,&grade); } Ex 6: # include int main() { string name; ------ scanf(“%s”,name); } Writing Output Data Output data can be written from computer memory to the standard output device(Monitor) using printf library function. printf(“control string”,variable1, variable2 ….); Ex1 : #include int main() { printf(“JSS college”); } Output: Jss college Ex2: #include int main() { printf(“JSS college”); printf(“Mysore”); } Output : JSS college Mysore If we use \n(back slash) then cursor will go the next line and print the string Ex3: #include int main() { printf(“JSS college\n”); printf(“Mysore”); } Output: JSS college Mysore Ex 4: #include int main() { int a=5; printf(“%d”,a); } Output : 5 Ex 5: #include int main() { int a=5; printf(“a value is %d”,a); } Output: a value is 5 Ex 6: #include int main() { float a=5.5; printf(“a value is %f”,a); } Output: a value is 5.5 Ex 7: #include int main() { string str=”bca”; printf(“my course is %s”,str); } Output: my course is bca Ex 8: #include int main() { printf(“my college name is \”JSS\” ”); } Output: my college name is “JSS” If we want to print character like single quote(‘),double quote(“ ”) or the back slash character(\), then we have to provide them by backslash character in the format string