T. Y. B.Sc. Mathematics MTH- 356 (A) : Programming in C Unit 1 : Basic Concepts Syllabus : Introduction, Character set, C token, Keywords, Constants, Variables, Data types, Symbolic constants, Over flow, under flow, Operators of arithmetic, Relational, Logical, Assignment, Increment and decrement, Conditional and special type. In C-language the English characters are used to write any type of program. Uses small case alphabets of English language a,b,c,------,z. Uses basic nos. from mathematics 0,1,2,3,4,-------,9. Uses diff. types of characters. Such as + plus, minus, * star, / slash, = equal to, % percent,! exclamation, & and,, comma, [ ] { } brackets, : colon,. dot, ; semicolon, # hash, inverted comma C tokens : C tokens are the basic bulidings blocks in C language which are constructed together to write a C program. Each and every smallest individual units in a C program are known as C tokens. C tokens are of six types. They are C tokens example program : main() } { Where, 1. Keywords ( eg. Int, while ) 2. Identifiers ( eg. main, total ) 3. Constants ( eg. 10, 20 ) 4. Strings ( eg. total, Hello ) 5. Special symbols ( eg. (), {} ) 6. Operators ( eg. +,,, / ) Int x, y, total; x = 10, y = 20; total = x + y; printf("total=%d", total); main identifier ( ) { } - special symbol Int - keyword 1
x, y, total identifier Main, (), { }, int, x, y, total C tokens Keywords in C language : There are 32 keywords in C programming language auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while Constant : It is a single or multiple digit number formed by using basic numbers whose value Is constant throughout the program. eg. 128, 58.0, 20, - 22.5 The negative number assigned with a negative sign. These are four types of constant used in C language. 1) Float constant : It is a string of number containing decimal values. 2) Integer constant : This constant is a string of numbers. without any decimal values. 3) Exponent constant : The floating point constant can be written in the exponent form if the value is too long or small. 4) Single Character constant : It is constant whose value is a character which may be alphabets number or special character. for eg. 5, a, X 5) String constant : A string constant is a sequence of characters enclosed in double quotes. It consist of any combination of digits, letters, escaped sequences and spaces. for eg. SA, "5+3", "hello". Ex. Following are valid or invalid string constants i) 100 - valid string constant ii) 24, Janpath Road - valid string constant iii) W - invalid string constant iv) Modi - invalid string constant. 2
Difference between single character constant and string constant. Single Character constant Enclosed within single inverted commas The maximum length of a character constant can be one character The character constant A consists of only character A A single character constant occupies one byte. String constant A sequence of characters enclosed in double quotes. A string constant can be any length The character constant A consists of character A and 0. A single string constant occupies two bytes. Variables : A value which varies continuously during the exaction of program in a variable of the program. A variable can be written by a variable name formed by string of alphabets underscore & digits. But the variable name must start with alphabet or underscore. It should not start with digit. The digit can be inserted in the variable name any where. for e.g. sum, piece_ flat, i, number_ of_ movies, whatsapp, hike4 - are valid variable names. Invalid variable names : i) sum value : This is invalid variable due to blank space ii) face book : This is invalid variable due to blank space. iii) 5modi : This is invalid variable because it start with a digit. There are diff. types of variables 1) Integer variable : It is variable whose value during the execution of the program remains as an integer value. This variable can be declared at the starting of the program by the word int eg. int a,b,c; These variable accept the value between 32000 to +32000 2) Float variable: It is a variable name whose value remains real a decimal during execution of the program. This variable name can be declared at the starting of the program by a word float. Eg. float extra, count These variable have up to 6 decimal accuracy. 3) Double variable : This is similar to float variable. It is used when the accuracy provided by a float variable is not sufficient & more accuracy is expected. These are declared by the word double. eg. double second, time; These variable have up to 12 decimal accuracy. 3
4) Character variable: These variable contains a string of alphabets or characters or number as character. These can be declared by using the word char. eg. char call, make These variable accept are character. 5) Long variable: It the word long is placed before any declaration of variable then the declared will be extended accuracy as many computer system for double & integer variable. If long is used for the int variable then the variable accepts the value up to 1 10 21 to 1 10 21 eg. Long int factorial If long is used for the double variable then the variable accept the value up to 24 decimal accuracy. 6) Short variable : If the word short is place before any declaration, then the declared variable will be used to store fairly small accurate value. eg. short int factorial ; 7) Unsigned variable: If the word unsigned is placed before any declaration then the declared variable will be used to store positive number. eg. unsigned int counter; The following are the ranges of variable data types for the variable. Data types char unsigned char Ranges Characters 0 225 char signed char 128 to 127 int 32768 to 32767 unsigned int 0 to 65535 signed int 32768 to 32767 unsigned shot int 0 to 235 sign short int 0 to 235 long int 2147483648 to 2147483647 unsigned long int 0 to 4294967296 singed long int same or long int float double float 6 decimal place 12 decimal place 4
long double float 4 decimal place Symbolic Constant : A symbolic constant is same that substitute for a sequence of character that can t be changed. The character may represent a numeric constant, a character constant or a string. When the program is compiled each occurrence of a symbolic constant is replaced by its corresponding character sequence. They are usually define at the beginning of the program. The symbolic constant may then appear later in the program in place of the numeric constants, character constant etc. that the symbolic constant represent. for eg : C program consists of the following symbolic constant define PI 3.142 define TRUE 1 define FALSE 0 Overflow : In C programming an integer overflow occurs when an arithmetic operation attempts to create a numeric value that is too large to be represented within the available storage space. For instance, adding 1 to the largest value that can be represented constitutes an integer overflow. Underflow : The condition in a C program that can occur when the true result of a floating point operation is smaller in magnitude ( that is closer to zero ) than the smallest value representable as a normal floating point number in the target data type. Arithmetic operators : The arithmetic operators perform arithmetic operations and can be classified in to unary and binary arithmetic operations. The following are the list of the arithmetic operators. Operator Meaning + Addition _ Minus * Multiplication / Division % Modulus 1) Unary minus operators : If the value of any variable is negative then it is assigned with a negative sign by the use of unary minus operators. eg : a ; where negative sign is a unary minus operator. 2) Modulus operator : The modulus operator is indicated by % sign. This operator provides as a reminder of any value between divisions of the two integer variable. eg. a%b. This means that a is divided by b & remainder is the result. Relational Operators : The relational operators are used to compare arithmetic, logical and character expressions. We often compare two quantities and depending upon that we take a decision. 5
Operator Meaning < less than > greater than <= less than or equal to >= greater than or equal to == equal to! = not equal to Logical operator : These operators are used to compare the logical and relational expressions. Operator Meaning && Logical AND Logical OR! Logical NOT Bitwise operator : These operators operate on each bit of data. They are used for testing, complementing or shifting of bits to right or left. Usually they are not useful in the case of float and double variables. Operator Meaning & bitwise AND bitwise OR ^ Bitwise XOR Shift left Shift right ~ Bitwise complement Assignment Operator : This operator evaluates the expression on the right and assigns the resulting value to the variable on the left side. " = " is called as assignment operator. Increment and Decrement Operators : These operators are very much used in C language. They are extensively used in loops Syntex : Increment : ++< variable name > < variablename > + + Decrement : < variable name > < variablename > The + + operator adds 1 to the operand and operator subtract 1 from the operand. Conditional Operator : This operator consists of two symbols : the question mark (? ) and colon ( : ) Syntax : variable = (test expression)? true: false; It has three components the expression, true part and false part. If the expression is true it evaluates to true part after? else it evaluates false part after colon. Method of writing C program : Every column of screen accepts one character on one line. Each character of the statement should be written on one line on the program. At the end of every statement semicolon ; must be provided. Which indicates that the statement is complete. If the space for statement is not sufficient, it can be written an next line. The statement instruction should be written in continuation of the 6
sequence of the statement. If the program is a main program then it is to be written by the first statement as main ( ) main ( ) - main program { - start of program - Program statement } - end of program The opening { brace denotes the start of the program an one line & written below main ( ) statement. The statement are inserted after opening brace & at the end of program. It is closed by } closing brace on one single line.. Comment statement : The comment statement is enclosed in the two states / with * on both sides of the statement is useful for the reading of the program by the operator. It can be used for giving a tittle to the program. The comment statement can be inserted any where in the program. It is non-executable statement of the program. Eg. /* India is my country */ Ex.1] Explain whether following variable names are valid or invalid, if invalid why? 1) n_3_5 valid 2) 5-next invalid because it start with digit. 3) x > y 8 invalid due to > symbol. 4) w.l.o. invalid due to. symbol. 5) x$ invalid due to special symbol $ 6) y invalid due to special symbol Xl. 7) beg invalid due to 8) _beg valid 9) raman valid 10) Raman invalid due to start with capital letter. Ex.2] Correct the following statement in C. 1) float c, d Sol: correct is float c, d; 2) int a, b float c; Sol: correct is int a, b, float c; 3) /* This is a c program. 7
Sol: correct /* This is a c program */ 4) c = ((a + b) c)/d Sol: correct c = ((a + b) c)/d; 5) int a, b, c; sol: correct is int a, b, c; 6) / This is a best program */ Sol: correct /* This is a best program */ Ex.3. Write a C program to print our name with comment statement /* write a program to print our name */ main( ) } { printf ( my name is xyz ); Output: my name is xyz Ex.4 Write a C program to print Hi I am using whatsaap with comment statement. /* write a program to print Hi I am using whatsaap */ main( ) } { printf ( Hi I am using whatsaap ); Output: I am using whatsaap ( 1 mark questions ) Exercise 1. State whether the following statements are TRUE or FALSE : i) Every line in a C program should end with a semicolon. ii) Every C program ends with an END word. iii) In C language, lowercase letters are significant. iv) A line in a C program may have more than one statement. v) The closing brace of the main( ) in a program is the logical end of the program. 2. Write any four keywords in C language. 3. How many keywords in C languages, write any two keywords. 4. Following constant are which type of constant. i) 20, 37.45 8
ii) a, hello 5. Define string constant. 6. Define single character constant. 7. How to write the comment statement in C. 8. Write the syntax of Increment. 9. Write the syntax of decrement. 10. Write the syntax of conditional operator. ( 2 marks questions ) 1. Write types of C tokens. 2. Write any eight keywords in C language. 3. Write types of constant in C. 4. Write types of variable in C. 5. Define string constant. Give some example? 6. Write any two difference between single character constant and string constant. 7. Define symbolic constant 8. Write any two arithmetic operator and their meaning in C. 9. Write any two logical operator and their meaning in C. 10. Write any two relational operator and their meaning in C. 11. Write the syntax of Increment and decrement. 12. Write the syntax of conditional operator and its meaning. ( 3 marks questions ) 1. Write short note on C tokens. 2. Write short note on keywords in C language. 3. Write short note on constant in C. 4. Write short note on variable in C. 5. Write short note on arithmetic operator in C. 6. Explain the method of writing C program 9