This document provides an overview of C programming concepts such as data types, variables, constants, keywords, operators, functions, and comments. It includes examples of simple C programs and explanations of concepts like #include directives, printf and scanf functions, and return types. The document is presented as a slideshow by Pradeep Dwivedi and was intended to teach basic C programming.
6. Some program and explanations.TOPIC:- Monday, August 30, 2010 2 PRADEEP DWIVEDI [pur.B.TECH-IT]
7. SAMPLE PROGRAM(SEE-SAMPLE.C) Monday, August 30, 2010 PRADEEP DWIVEDI [pur.B.TECH-IT] 3 #include<stdio.h> #include<conio.h> void main() { clrscr(); printf(“hello, I am pradeep”); getch(); }
8. #include directve Monday, August 30, 2010 PRADEEP DWIVEDI [pur.B.TECH-IT] 4 c programs are divided in to modules or functions. some functions are written by users, like us , and many other are stored in the c library. library functions are grouped category-wise and stored in different files known as header files. if we want to access the function stored in the library , it is necessary to tell the compiler about the file to be accessed. this is achieved by using the preprocessor directive #include.
9.
10. SAMPLE PROGRAM(SEE-SAMPLE.C) Monday, August 30, 2010 PRADEEP DWIVEDI [pur.B.TECH-IT] 7 this program when executed will produce the following output. hello, I am pradeep the first inform that the execution of our program begin at this line. the main is a predefine(special) function used by the c system to tell the compiler where the program starts. every program must have exactly one main function. if we use more than one main function the compiler can not understand which one marks the beginning of the program.
11. SAMPLE PROGRAM(SEE-SAMPLE.C) Monday, August 30, 2010 PRADEEP DWIVEDI [pur.B.TECH-IT] 8 the opening brace { in the second line marks the beginning of the function main() and the closing brace } in the last line indicates the end of the function. printf() is a standard predefine c function for printing output. predefined means that it is a function that has already been written and compiled and linked together with program at the time of linking. the printf function every thing between the starting and the ending quotation marks to be printed out. semicolon(;) sign is called terminator. every statement in c should end with a semicolon.
12. TERMs RELATED TO PROG Monday, August 30, 2010 PRADEEP DWIVEDI [pur.B.TECH-IT] 9 clrscr(); is a predefine function that clear the output screen. it comes from conio.h header file. getch(); is also a predefine function which is used to pause the output screen.
13. FORMAT OF SIMPLE C PROGRAM Monday, August 30, 2010 PRADEEP DWIVEDI [pur.B.TECH-IT] 10 main() function name { start the program } end the program program statements (body)
14. CHARACTER SET Monday, August 30, 2010 PRADEEP DWIVEDI [pur.B.TECH-IT] 11 The character in c are grouped into the following categories. Letters Digits Special characters White space
15. C TOKENS Monday, August 30, 2010 PRADEEP DWIVEDI [pur.B.TECH-IT] 12 in a passage of text individual words and punctuation are called tokens. Similarly, in c program the smallest individual units are known as c tokens.
17. VARIABLES Monday, August 30, 2010 PRADEEP DWIVEDI [pur.B.TECH-IT] 14 Variables are a place holder that holds the place in computer memory for the value. Because its value may be varied that’s why it is known as variable. int a; (this is called variable declaration). data type variable. int a=10;(this is called initialization of variable.
18. DATA TYPES Monday, August 30, 2010 PRADEEP DWIVEDI [pur.B.TECH-IT] 15 data type decide that what kind of value should be held by the variable. in c data types are classified into three categories. primary(or fundamental data) types. derived data types. user defined data types.
21. DATA TYPES Monday, August 30, 2010 PRADEEP DWIVEDI [pur.B.TECH-IT] 18 void type:- void has no value. this is usually used to specify the type of function.
22. KEYWORDS AND IDENTIFIERS Monday, August 30, 2010 PRADEEP DWIVEDI [pur.B.TECH-IT] 19 KEYWORDS are the reserve words those are having some predefine meaning and those are only use for their predefine meaning. all keywords in c are in lower case. in c we have 32 keywords. eg, if, else, for, while, char, int etc. IDENTIFIERS refers to the names of variable, function and arrays.
23. NAMING RULES AND CONVENTIONS FOR THE VARIABLE NAME(IDENTIFIERS) Monday, August 30, 2010 PRADEEP DWIVEDI [pur.B.TECH-IT] 20 a variable name can be anything but it should be short and meaningful. a variable name can not start with a digit. it must be started with an alphabet letter. but a variable name can be followed by n number of digits. it can be started with an underscore sign(_) also. we can not use any special symbol like @,#,*,& in a variable name. we can not use any space in a variable name. if we are having mare than one word in a variable name and space is required that time we can use the underscore(_) sign between them. my_name ,myName. any keywords can not be a variable name.
24. prog:1 Monday, August 30, 2010 PRADEEP DWIVEDI [pur.B.TECH-IT] 21 /*WRITE A PROGRAM TO ACCEPT A NUMBER FROM THE USER AND PRINT THEM*/ #include<stdio.h> #include<conio.h> void main() { int number; clrscr(); printf("Enter a number"); scanf("%d",&number); printf("the number is: %d",number); getch(); }
25. TERMs RELATED TO PROG Monday, August 30, 2010 PRADEEP DWIVEDI [pur.B.TECH-IT] 22 printf() is a predefine function with the help of that we can print the data in any specified format or agates if it is in double quotes. (“ “); scanf() is again a predefine function that can accept data in any specified format.
26. FORMAT STRING Monday, August 30, 2010 PRADEEP DWIVEDI [pur.B.TECH-IT] 23 format string is used to specify the format of the data . these are following type of:- %d (decimal) %f (float) %c (character) %s (string) %x (hexadecimal)
27. ADDRESS OPERATOR(&) Monday, August 30, 2010 PRADEEP DWIVEDI [pur.B.TECH-IT] 24 It is used to keep the address of that place where the value is placed.
28. prog4 Monday, August 30, 2010 PRADEEP DWIVEDI [pur.B.TECH-IT] 25 /*write a program to convert a character to its ascii value*/ #include<stdio.h> #include<conio.h> void main() { char alph; clrscr(); printf("Enter a character"); scanf("%c",&alph); printf("value of character is %c",alph); printf("value of character equivalent is %d",alph); getche(); }
29. SOME POINTS Monday, August 30, 2010 PRADEEP DWIVEDI [pur.B.TECH-IT] 26 note:- each and every character is internally treated as an integer. getch(); is a predefine function that accept a character on the output screen . it just wait for a character. getche(); is having same functionality like getch() function but different is that it displayed the accepted character on the consol screen. getchar(); is also a predefine function that accept a character but that character must be followed by the enter key. note:-drawback of scanf function is that it never accept the space.
30. COMMENTS Monday, August 30, 2010 PRADEEP DWIVEDI [pur.B.TECH-IT] 27 commets are used to give the better readability of a program. at the time of program execution comments are ignored by the compiler. these are two types of: //single line comments /* multiple line comments */ note:-we can not use nested comments.
31. RETURN TYPE Monday, August 30, 2010 PRADEEP DWIVEDI [pur.B.TECH-IT] 28 It decide what kind of value should be returned by the function. if we don’t use any return type before the function that time by default there is int. return is a keyword with the help of that we can return a value and a value is always return where the function is being called.