














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to the c programming language, covering topics such as the history and development of c, its basic structure, data types, decision-making constructs, loops, and functions. It explains the concepts of third-generation and fourth-generation programming languages, the role of compilers, and the file structure of c programs. The document also includes examples of basic c programs demonstrating the use of various language features and constructs, such as escape sequences, data type conversions, and currency/measurement conversion programs. Additionally, it covers decision-making statements, loops, and functions in c. The document serves as a comprehensive introduction to the c programming language, providing a solid foundation for understanding its core concepts and syntax.
Typology: Summaries
1 / 22
This page cannot be seen from the preview
Don't miss anything!















Programing
Programing means arrangement and management for create software. Creation of software is called programing. Computer Programing Computer programing means creation of software through computer programing languages. Computer Programing Languages Language means way of communication. Programing languages are languages used to create softwares (Programing). Generations of Computer Programing Languages There are five generations of Computer Programing Languages
Introduction and History of C language C language was developed by Dennis Ritchie. C language was developed in 1972 at Bell Telephone Laboratory. C language came after the B language. It is third generation programing language. C language used to professional softwares. C language is case sensitive language it means small (a) is not equal to capital (A). In c language we used functions and keywords for create software. In c language used compiler for translation, because c language is third generation programing language.
IDE and Compiler of C Language IDE stands for Integrated Development Environment, any type work we have need any Environment for work or any platform for work. So C language is the language and we have need platform and Compiler (Translator). Platform for work and Compiler for translate human language code into Machine language and machine language output into Human language output.
We used Turbo C++ for IDE and Compiler How to Install Turbo C++ into our Computer? First of backup copy into USB and then backup paste into My Computer (Local Disk C……Windows Drive). How to open Turbo C++ after the install? First of all go to My Computer, then open Local Disk C, after click on Turbo C++ folder and then click on “DOSBOX” file. How to make a new file? After open the “DOSBOX” go to file menu and click on “New” option. How to save a file of C language program? After the work of C program, go to file menu and click on “Save” option and then give name to file and Extension of file. Example Meraj.c In this “Meraj” is the name of file and “.C” is the extension of c language program file. Reason is that we used C++ language IDE and our file is simple C language file, so at the time of save file we must extension with file name , otherwise compiler is not translate to our c language program file. How to open a file of C language program? Click on file menu and go to “Open” option and click on this open option, after give name with extension of file and click ok.
Example of Basic Program with using clrscr() and getch() Functions
Code Output
Escape Sequences
Escape Sequences are very important, these are used to change position of current cursor and terminate sequence of cursor/pointer.
\n is used to new line.
\t is used to tab (08 Spaces)
Example of Basic Program using Escape Sequence \n
Code Output
void main(void) { clrscr(); printf(“Meraj uddin”); getch(); }
Meraj uddin
void main(void) { clrscr(); printf(“* \n”); printf(“** \n”); printf(“*** \n”); printf(“****\n”); printf(“***** \n”); getch(); }
Example of Basic Program using Escape Sequence \n and \t
Code Output
Example of Basic Program using Escape Sequence \n and \t
Code Output
Example of Basic Program using Escape Sequence \n and \t
Code Output
void main(void) { clrscr(); printf(“L \n”); printf(“\tA \n”); printf(“L \n”); printf(“\tMeraj\n”); printf(“Baloch \n”); getch(); }
Meraj Baloch
void main(void) { clrscr(); printf(“D \n”); printf(“\tI \n”); printf(“\t\tT\n”); getch(); }
void main(void) { clrscr(); printf(“LAL \n”); printf(“\tCOMPUTER\n”); printf(“INSTITUTE\n”); getch(); }
Variable Initialization Variable Initialization means after declare to the variable then assign initial value to the variable through assignment operator. Example. int a=10;
Note: In this example “int” is the data type and “a” is the name of variable and “=” is assignment operator, “10” is the initial value that assign to the variable and “;” is the statement terminator.
Format Specifies Format Specifies are very important, these are data type controller and used to control current variable value data type. %d is the format specifies, control integer data type variable. %f is the format specifies, control float data type variable. %c and %s are the format specifies, control character data type variable. Example of two values integer data type program, using variable and format specifies %d
Code Output
Example of three values integer data type program, using variable and format specifies %d
Code Output
void main(void) { int a=10; int b=20; clrscr(); printf(“Addition is %d”,a+b); getch(); }
Addition is 30
void main(void) { int a=10; int b=20; int c=80; clrscr(); printf(“Addition is %d”,a+b+c); getch(); }
Addition is 110
Note: If you are design four values addition program, same above code only make one other space / variable and change formula. And if you are design subtract, multiply or division programs only changing in operator into formula. Example “+” change into “-“.
Example of two values float data type program, using variable and format specifies %f
Code Output
Note: If you are design four values addition program, same above code only make one other space / variable and change formula. And if you are design subtract, multiply or division programs only changing in operator into formula. Example “+” change into “-“.
Scanf Scanf is the input statement of C language program. It is very important function because Scanf is used to scan (Input) values into variable after variable declaration. Mostly Scanf is used when you not assign variable initial value. This time Scanf scan values into variable at the time of user output screen. Syntax of Scanf
Scanf (format specifies, address operator variable name) Scanf(“%d”,&a)
Note: In this syntax “%d” is the format specifies “&” is address operator and “a” is the name of variable.
void main(void) { float a=1.0; float b=3.5; clrscr(); printf(“Addition is %f”,a+b); getch(); }
Addition is 4.
Example of age calculator program using float data type, using Scanf……………(Dynamic Method)
Code Output
Example of currency convertor program using float data type, using Scanf……(Dynamic Method)
Convertor USA dollar to Pak Rupees
Code Output
void main(void) { float year; clrscr(); printf(“Enter your age years\n”); scanf(“%f”,&year); printf(“total months are %f\n”,year12); printf(“total weeks are %f\n”,year52); printf(“total days are %f\n”,year365); printf(“total hours are %f\n”,year8760); printf(“total minutes are %f\n”,year525600); printf(“total seconds are %f\n”,year31536000); getch(); }
Enter your age years ? User value Total months are? User output Total weeks are? User output Total days are? User output Total hours are? User output Total minutes are? User output Total seconds are? User output
void main(void) { float dollar; clrscr(); printf(“Enter USA Dollors\n”); scanf(“%f”,&dollar); printf(“total Pak Rupees are %f\n”,dollar*165); getch(); }
Enter USA Dollars ? User value Total Pak Rupees are? User output
Example of currency convertor program using float data type, using Scanf……(Dynamic Method)
Convertor Pak Rupees to USA Dollar
Code Output
Example of Measurement convertor program using float data type, using Scanf……(Dynamic Method)
Convertor Feet to Inch
Code Output
void main(void) { float rs; clrscr(); printf(“Enter Pak Rupees\n”); scanf(“%f”,&rs); printf(“total USA Dollars are %f\n”,rs/165); getch(); }
Enter Pak Rupees ? User value Total USA Dollars are? User output
void main(void) { float feet; clrscr(); printf(“Enter the Feet\n”); scanf(“%f”,&feet); printf(“total inches are %f\n”,feet*12); getch(); }
Enter Feet ? User value Total inches are? User output
Example of If else decision maker(Password Program), in this using Scanf…… (Dynamic Method).
Code Output
Else if is the decision maker, used to made decision. In else if decision maker check Multiple conditions at a time and show true and false both condition answers.
Example of else if decision maker(Mark sheet Program), in this using Scanf… (Dynamic Method).
Code Output
void main(void) { int pass; clrscr(); printf(“Enter the password”); scanf(“%d”,&pass); if(pass==786) printf(“Welcome ”); else printf(“Invalid Password ”); getch(); }
In this program, if user input password “786”. It shows “Welcome” but if user input other password number ,it shows “Invalid Password”.
void main(void) { Float obt,max,per; clrscr(); printf(“Enter your total obt marks”); scanf(“%f”,&obt); printf(“Enter your total max marks”); scanf(“%f”,&max); per=obt/max*100; printf(“Your percentage is %f\n”,per); if(per<33) printf(“You are Fail ”); else if(per<40) printf(“Grade is E ”); else if(per<50) printf(“Grade is D ”); else if(per<60) printf(“Grade is C ”); else if(per<70) printf(“Grade is B ”);
In this program, User Input total obt marks, total max marks. Then Show percentage and according to percentage show grade from conditions.
else if(per<80) printf(“Grade is A ”); else if(per<100) printf(“Grade is A-1 ”); else printf(“Wrong Input”); getch(); }
Example of else if decision maker(Months Program), in this using Scanf… (Dynamic Method).
Code Output void main(void) { int month; clrscr(); printf(“Enter the number between 1 to 12\n”); scanf(“%d”,&month); if(month==1) printf(“January ”); else if(month==2) printf(“February ”); else if(month==3) printf(“March ”); else if(month==4) printf(“April ”); else if(month==5) printf(“May ”); else if(month==6) printf(“June ”); else if(month==7) printf(“July ”); else if(month==8) printf(“August ”); else if(month==9) printf(“September ”); else if(month==10) printf(“October ”); else if(month==11) printf(“November ”); else if(month==12) printf(“December ”); else printf(“Invalid Number”); getch(); }
In this program, User Input Number between 1 to 12. Then Show month according to Number conditions.
Switch Statement is the multiple selection statement, used to made decision according to cases.
Example of Switch Statement decision maker (Calculator Program), in this using Scanf… (Dynamic Method) and Two data types used to gather.
Code
Code Output
Loop
Loop is very important for any language, because loop means repetition. Loop is used to repeat any value again and again.
There are three main types of Loop
void main(void) { float a,b; char op; clrscr(); printf(“Enter the first value , operator and Second value\n”); scanf(“%f”%c%f,&a,&op,&b); switch(op) { case ‘+’: printf(“Addition is %f”,a+b); break; case ‘-‘: printf(“Subtraction is %f”,a-b); break; case ‘’: printf(“Multiply is %f”,ab); break; printf(“Division is %f”,a/b); break; default: printf(“Invalid Operator”); } getch(); }
In this program, User Input first value, operator and second value. Like 13+ “13” is first value “+” is operator “14” is second value Answer show according to operator
Example of for loop, repeat name 10 times
Code Output
Example of for loop, Counting 1 to 10
Code Output
Note: If Change to the counting starting and ending point, so change to condition and variable value
Example: You are start the counting 5 and Ending 15. Change variable values replace “1” into “5” and replace “10” into “15”.
void main(void) { int a; clrscr(); for(a=1; a<=10; a++) printf(“Lal Computer\n”); getch(); }
Lal Computer Lal Computer Lal Computer Lal Computer Lal Computer Lal Computer Lal Computer Lal Computer Lal Computer Lal Computer
void main(void) { int a; clrscr(); for(a=1; a<=10; a++) printf(“%d\n”,a); getch(); }
Example of While loop, Counting 1 to 10
Code Output
Note: If Change to the counting starting and ending point, so change to condition and variable value
Example: You are start the counting 5 and Ending 15. Change variable values replace “1” into “5” and replace “11” into “16”.
Example of Do While loop, repeat name 10 times Code Output
Note: If Change to the counting starting and ending point, so change to condition and variable value Example: You are start the counting 5 and Ending 15. Change variable values replace “1” into “5” and replace “11” into “16”.
void main(void) { int a=1; clrscr(); while(a<11) { printf(“%d\n”,a); a++; } getch(); }
void main(void) { int a=1; clrscr(); do { printf(“DIT\n”); a++; } while(a<11); getch(); }
Array
Array means collection of variables. Variable is the single space but array is space of spaces.
Types of Array
Example of Simple Array Program
Code Output
C Language Operators
C language operators are operators, used to multiple problems solution like, Addition, Subtraction, Less than and greater than etc.
Operator Description Operators Arithmetic Operators +,-,*,/ Logical Operators <,>,= = Assignment Operator = Address Operator & Increment Operator ++ Decrement Operator -- Comments //
void main(void) { char a[50]; clrscr(); printf(“Enter your Good Name\n”); scanf(“%s”,&a); printf(“Your Name is so good”); getch(); }
In this array program “50” Characters we can write for Input “Enter your Good Name”to any name and program show greeting “Your Name is so good”.