Jan Faigl. Department of Computer Science. Faculty of Electrical Engineering. Czech Technical University in Prague. Lecture BE5B99CPL – C Programming. Senior Lecturer, Department of Computer Science & Engineering,. Kongu Engineering The first chapter deals with the fundamental concepts of C language. "Computer science and engineering is the systematic study of algorithmic processes-their theory, analysis, design, efficiency, implementation, and.
|Language:||English, Spanish, Japanese|
|Genre:||Business & Career|
|ePub File Size:||19.56 MB|
|PDF File Size:||16.25 MB|
|Distribution:||Free* [*Regsitration Required]|
C Programming: The Essentials for Engineers and Scientists. Authors; (view Part of the Undergraduate Texts in Computer Science book series (UTCS) Pages i-xvii. PDF · Programming Preliminaries. David R. Brooks. Pages PDF. PDF | Tad W Patzek and others published An Introduction to Computer Programming for Engineers and An Introduction to Computer Programming for Engineers and Scientists The quadratic iterator with a control parameter c. PDF | The programming languages have become important to develop computer programs. It is essential to write efficient programs in order to.
Functions and modular programming. One simple method of debugging is to place print statements throughout the program to display the values of variables. Several string constants are shown below: Both upper and lower case letters are permitted, though common usage favors the use of lowercase letters for most type of identifiers. Some of runtime errors are:
Several complex logical expressions that make use of these variables are shown below: Assignment Operators: There are several different assignment operators in C. All of them are used to form assignment expressions which assign the value of an expression to an identifier. The assignment operator is used to assign a value to an identifier, where as the equality operator is used to determine if two expressions have the same value.
These operators cannot be used in place of one another. Assignment expressions are often referred as assignment statements.
In such situation, the assignments are carried out from right to left. C contains the following five additional assignment operators: They are also known as shorthand assignment operators. The use of shorthand assignment operator has three advantages: What appears on the left-hand side need not be repeated and therefore it becomes easier to write.
The statement is more concise and easier to read. The statement is more efficient. Unary Operators: C includes a class of operators that act upon a single operand to produce a new value, Such operators known as unary operators. Unary operators usually precede their single operands, though some unary operators are written after their operands.
The common use unary operators in C are: Decrement Operator - 3. Unary minus - 4. The operand used with each of these operators must be a single variable. The increment or decrement operators can each be utilized two different ways, depending on whether the operator is written before or after. If the operator is written before the operand then it is called as prefix unary operator. If the operator is written after the operand, then it is called as postfix unary operator. When prefix is used, the operand will be altered in value before it is utilized for its intended purpose within the program.
Similarly, when postfix is used, the operand will be altered in value after it is utilized. The most common use unary operator is unary minus, where a numeric constant, variable expression is preced by a minus sign. Unary minus is distinctly difference from the arithmetic operator which denotes subtraction -. The subtractor operator requires two separate operands. Several examples of unary minus are -0x7fff A C program includes an integer variable i whose initial value is 1.
Suppose the program includes the following three printf statements. The precedence of unary increment or decrement or unary minus or plus is same and associativity is right to left.
Bitwise Operators: Bitwise Operators are used for manipulating data at bit level. These operators are used for testing the bits, or shifting them right or left. Bitwise operators can be applied only to integer type signed or unsigned and not to float or double. They are Er. Comma Operator: The comma operator can be used to link the related expression together. A comma linked list of expression are evaluated left to right and the value of right-most expression is the value of combined expression.
For example, Downloaded from www. Size of Operator: The size of operator is used with an operand to return the number of bytes it occupies. It is a compile time operand. The operand may be a variable, a constant or a data type qualifier. The associativity of size of right to left. For e. Suppose that i is an integer variable, x is a floating-point variable, d is double-precision variable and c is character type variable.
The statements: The operator?: Simple conditional operations can be carried out with conditional operator. An expression that make use of the conditional operator is called a conditional expression. Such an expression can be written in place of traditional ifelse statement. A conditional expression is written in the form: Computer Programming in C expression1? If expression1 is true, the value of expression2 is the value of conditional expression.
If expression1 is false, the value of expression3 is the value of conditional expression. For example: Rightshift Less than Less than or equal to Greater than Greater than or equal to. Write the general form of ternary operator and explain with example. PU 2. What is a datatype? Write down the various types of it in C-program. PUBack 3. What are the various types of operators used in C language?
Give a table that shows their precedence and associatively. PU 4. Define the data types. Describe different data types. PUBack 5. Write a program to enter two numbers. Make the" comparison between them with conditional operator. If the first number is greater than second, perform multiplication otherwise division 6. What are operators? Describe unary, binary and ternary operators with examples.
Chapter 5 Input and Output Operations A program is a set of instructions that takes some data and provides some data after execution. The data that is given to a program is known as input data.
Similarly, the data that is provided by a program is known as output data. Generally, input data is given to a program from a keyboard a standard input device or a file. The program then proceeds the input data and the result is displayed on the screen monitor a standard output device or a file.
Reading input data from keyboard and displaying the output data on screen, such input output system is considered as conio input out. When a program needs data, it takes the data through the input functions and sends the results to output devices through the output functions.
As keyboard is a standard input device, the input functions used to read data from keyboard are called standard input functions. The standard input functions are scanf , getchas , getch , gets , etc. Similarly, the output functions which are used to display the result on the screen are called standard output functions. The standard output functions are printf , putchas , putch , puts , etc. The standard library stdio.
The contents of the header file become part of the source code when it is compiled. Formatted functions ii. Unformatted functions. Formatted Functions: Formatted functions allow the input read from the keyboard or the output displayed on screen to be formatted according to our requirements. The input function scanf and output.
While displaying a certain data on screen, we can specify the number of digits after decimal point, number of spaces before the data, the position where the output is to be displayed, etc, using formatted functions. Formatted Input: Formatted input refers to an input data that has been arranged in a particular format.
For example, consider the following data. The first be read into a variable int, the second into float, and the third into char. This is possible in C using the scanf function. The input data can be entered into the computer from a standard input device keyboard by means of the C library function scanf.
This function can be used to enter any combination of numerical values, single characters and strings. The function returns the number of data items that have been entered successfully. The general syntax of scanf function is scanf control string, arg1, arg2, , argn where, control string refers to a string containing certain required formatting information so also known as format string and arg1, arg2, , argn are arguments that represent the individual input data items.
Actually, the arguments represent pointers that indicate the addresses of the data items within the computers memory. The control string consists of individual groups of characters, with one character group for each input data item. In its simplest form, a single character group will consist of the percentage sign, followed by a conversion character which indicates the types of corresponding data item.
Within the control string, multiple character groups can be contiguous, or they can be separated by whitespace i. If whitespace characters are used to separate multiple character groups in the control string, then all consecutive white-space characters in the input data will be read but ignored.
The use of blank spaces as character group separators is very common. Several of the more frequently used conversion characters are listed below:. Meanings Data item is a single character Data item is a decimal integer Data item is a floating-point value Data item is a floating-point value Data item is a floating point value.
Conversion Charater Table Example: Different methods for entering the inputs: If the string were written as Eastern College of Engineering then only the single letter E would be assigned to line. Since the first lowercase letter in this case a would be interpreted as the first character beyond the string. If the character within the brackets is simply the circumflex followed by a newline character, then string entered from the standard input device can contain any ASCII characters except the newline characters line feed.
Thus, the user may enter whatever he or she wishes and then presses the Enter Key. The Enter Key will issue the newline character, thus signifying the end of the string. The consecutive nonwhitespace characters that define a field. It is possible to limit the number of such characters by specifying a maximum field width for that data item.
The data item may contain fewer characters than the specified filed width. However, the number of characters in the actual data item cannot exceed the specified field width. Any characters that extend beyond the specified filed width will not be read. Such leftover characters may be incorrectly interpreted as the components of the next data item.
The second will be assigned to a long decimal integer variable, and the third will be assigned to a double precision variable. The control string in the second scanf function indicates that the first data item will have a maximum field width of 3 characters and it will be assigned to short octal integer variable, the second data item will have a maximum field width of 7 characters and it will be assigned to a long hexadecimal integer variable, and the third data item will have a maximum field width of 15 characters and it will be assigned to double precision variable.
In most version of C, it is possible to skip over a data item, without assigning it to the designated variable or array. This feature is referred to as assignment suppression. However will not be assigned partno because of asterisk, which is interpreted as an assignment suppression character.
Formatted Output: Formatted output refers to the output of data that has been arranged in a particular format.
The printf is a built-in function which is used to output data from the computer onto a standard output device i. The printf statement provides certain features that can be used to control the alignment and spacing of print-outs on the terminals. The general form of printf statement is printf control string, arg1, arg2,. The arguments can be written as constants, single variable or array names, or more complex expressions.
Function references may also be included. In contrast to scanf function, the arguments in a printf function do not represent memory addresses and therefore are not preceded by ampersands. An individual character group in control string will consist of the percent sign, followed by a conversion character indicating the type of the corresponding data item. Multiple character group can be contiguous, or they can be separated by other characters, including whitespace character.
These other characters are simply transferred directly to the output device, where they are displayed. The use of blank spaces are character group separators is particularly common. Meaning Data item is displayed as a single data Data it Data item is displayed as floating point value with an exponent em is displayed as a signed decimal Data item is displayed as floating point without an exponent Data item is displayed as floating point value using either e-type or f-type conversion Data item is displayed as a signed decimal integer Data item is displayed as an octal integer w Data item is displayed as a string ithout a leading zero Data item is displayed as an unsigned decimal integer Data item is displayed as a hexadecimal integer without the leading 0x.
Conversion Charater Table Note: So, the output generated will be fastener Control string: The general syntax of control string: Flags [Optional] T he flag affect the appearance of the output. They must be placed immediately after the percent sign.
Data item is left justified within the field. The blank spaces required to fill the. Without this flag, only negative data items are precededby a sign. Causes leading zeros to appear instead of leading blanks. Applies only to data items that are right justified within a field whose minimum size is larger than the data item: A blank space will precede each positive signed numeric data item. Field Width [Optional] The field width is an integer specifying the minimum output field width.
If the number of characters in the corresponding data item is less than the specified field width then the data item will be preceded by enough leading blanks to fill the specified field. If the number of characters in the data item exceeds the specified field width, then additional space will be allocated to the data item so that the entire data item will be displayed. Precision [Optional] The operation of precision field depends on the types of conversion. It must start with a period.
Format for Integer Output: Computer Programming in C where w is the integer number specifying the minimum field width of output data. If the length of the variable is less than the specified field width, then the variable is right justified with leading blanks. Format for floating point output: The general form: Computer Programming in C Output of Strings: Unformatted Functions: Unformatted functions do not allow the user to read or display data in desired format.
These library functions basically deal with a single character or a string of characters. The functons getchar , putchse , gets , puts , getch , getche , putch are considered as unformatted functions. The getchar function reads a character from a standard input device. The putchar function displays a character to the standard output device. Enter gender M or F: M Our gender is: M Note: The functions getch and getche reads a single character the instant it is typed without waiting for the enter key to be hit.
The difference between them is that getch reads the character typed without echoing it on the screen, while getche reads the character and echoes displays it on the screen. The general syntax of getch: Second character: However, using getche function, we can see what we have typed. In both cases, input accepted as soon as the character typed. The last getch simply takes a character but doesnot store it anywhere. So, its work is merely to hold the output screen until a key is pressed.
The gets function is used to read a string of text containing whitespaces, until a newline character is encountered. It offers an alternative function of scanf function for reading strings. Unlike scanf function, it doesnot skip whitespaces. Write short noteson: PU 5. Distinguish between getc a getchar. PU 6. Write the various input unformatted function and describe any two. Write the various output unformatted function and describe any two.
Chapter 6 Control Statements The statements which alter the flow of execution of the program are known as control statements. In the absence of control statements, the instruction or statements are executed in the same order in which they appear in the program. Sometimes, we may want to execute some statements several times. Sometime we want to use a condition for executing only a part of program.
So, control statements enable use to specify the order in which various instruction in the program are to be executed. There are two types of control statements: Loops Loops are used when we want to execute a part of program or block of statement several times. So, a loop may be defined as a block of statements which are repeatedly executed for a certain number of times or until a particular condition is satisfied.
There are three types of loop statements in C: For 2. While 3. The control statement in loop decides whether the body is to be executed or not. For Loop: For loops is useful to execute a statement for a number of times. When the number of repetitions is known in advance, the use of this loop will be more efficient. Thus, this loop is also known as determinate or definite loop.
Flowchart of For Loop: Print" Enter a number whose factorial is to be calculated. Read num. Initilize fact to 1 and counter i to1 5. Print fact as factorial of the number num. While Loop: After the execution, again the condition is checked and if it is found to be true then again the statements in the body of loop are executed. This means that these statements are executed continuously till the condition is true and when it becomes false, the loop terminates and the control comes out of the loop.
Each execution of the loop body is known as iteration. Body of Loop Next statement out of loop fig: Enter the number: The do The body of this loop may contain a single statement or a block of statements.
The general syntax is:. If the condition is true, then again the loop body is executed and this process continues until the condition becomes false. Unlike while loop, here a semicolon is placed after the condition. In a while loop, first the condition is evaluated and then the statements are executed whereas in do while loop, first the statements are executed and then the condition is evaluated. So, if initially the condition is false the while loop will not execute at all, whereas the do while loop will always execute at least once.
Differences between while loop and do while loop: The body of the loop may not be executed at all if the condition is not satisfied at the very first attempt. Nesting of loops: When a loop is written inside the body of another loop, then it is known as nesting of loops.
Any type of loop can be nested inside any other type of loop. For example, a for loop may be nested inside another for loop or inside a while or do Similarly, while and do while loops can be nested.
Decisions Since decision making statements control the flow of execution, they also fall under the category of control statements.
Following are decision making statements: The if statement is a powerful decision making statement and is used to control the flow of execution of statements. This is a bi-directional condition control statements. This statement is used to test a condition and take one of two possible actions, If the condition is true then a single statement or a block of statements is executed one part of the program , other wise another single statement or a block of statements is executed other part of the program.
In C, any non-zero value is regarded as true while zero is regarded as false. Flowchart of if control statement Here if the condition is true non-zero then statement1 is executed, and if it is false zero , then the next statement which is immediately after the if control statement is executed. For eg: The if..
It is used when there are two possible actions one when a condition is true and the other when it is false. The syntax is: Flowchart of if After this the control transfers to the next statement which is immediately after the if Enter a number: Nested if We can have another if This is called nested if.. This is a type of nesting in which there is an if This type of nesting is frequently used in programs and is also known as else if ladder.
This is a multi-directional conditional control statement. Sometimes, there is a need in program to make choice among number of alternatives. For making this choice, we use the switch statement. The expression following the switch keyword can be any C expression that yields an integer value or a character value. It can be value of any integer or character variable, or a function call returning on integer, or an arithmetic, logical, relational, bitwise expression yielding integer value.
The constants following the case keywords should be of integer or character type.
These constants must be different from each other. The statements under case can be any valid C statements like if Writing a switch statement inside another is called nesting of switches.
Firstly, the switch expression is evaluated then value of this expression is compared one by one with every case constant. If the value of expression matches with any case constant, then all statements under that particular case are executed. If none of the case constant matches with the value of the expression then the block of statements under default is executed. Out of switch fig: Computer Programming in C case 2: Enter your choice: The statements of case 3 and default are also executed in addition to the statements of case2.
This is known as falling through cases. Suppose we dont want the control to fall through the statements of all the cases under the matching case, then we can use break statement. Break statement: Break statement is used inside lops and switch statements.
Sometimes it becomes necessary to come out of the loop even before the loop condition becomes false. In such a situation, break statement is used to terminate the loop. This statement causes an immediate exit from that loop in which this statement appears. It can be written as i. The continue statement is used to bypass the remainder of the current pass through a loop. The loop does not terminate when a continue statement is encountered.
Instead the remaining loop statements are skipped and the computation proceeds directly to the next pass through the loop. The general syntax: The goto statement is used to alter the normal sequence of program execution by unconditionally transferring control to some other part of the program. The goto statement transfers the control to the labeled statement somewhere in the current function. The general syntax of goto statement: Whenever, the statement goto label, is encountered, the control is transferred to the statement that is immediately after the label.
Generally, the use of goto statement is avoided as it makes program illegible and unreliable.
This statement is used in unique situations like Branching around statements or group of statements under certain conditions Jumping to the end of a loop under certain conditions, thus bypassing the remainder of loop during current pass. Jumping completely out of the loop under certain conditions, terminating the execution of a loop.
We have already known that we can jump out of a loop using either the break statement or goto statement. In a similar way, we can jump out of a program by using the library function exit. In case, due to some reason, we wish to break out of a program and return to the operating system. The general syntax is if condition exit 0 ; The exit function takes an integer value as its argument.
Normally zero is used to indicate normal termination and non zero value to indicate termination due to some error or abnormal condition. Computer Programming in C printf Enter your choice: Write a program to check whether the number is prime or not. Enter number: Computer Programming in C printf Enter value of x: Enter the number to be checked: What is a control statement? Differentiate between do while and while statement with the help of flow charts 2. What do you mean by control statements?
Describe the application of break and continue in C-programming. Explain with examples 3. Write a program to check the given number is prime or not. Write a program to check number is palindrome or not. What is control statement?
Describe different control statement 6. Describe the different types of looping statement used in C with flow chart. Write a menu driven program using switch statement having the following options. Chapter 7 Functions A function is defined as a self contained block of statements that performs a particular task. This is a logical unit composed of a number of statements grouped into a single unit. It can also be defined as a section of a program performing a specific task.
The function main is always present in each program which is executed first and other functions are optional. Advantages of using Functions: The advantages of using functions are as follows: Generally a difficult problem is divided into sub problems and then solved. This divide and conquer technique is implemented in C through functions. A program can be divided into functions, each of which performs some specific task. So, the use of C functions modularizes and divides the work of a program.
When some specific code is to be used more than once and at different places in the program, the use of function avoids repetition of that code.
The program becomes easily understandable. It becomes simple to write the program and understand what work is done by each part of the program.
Functions can be stored in a library and reusability can be achieved. Types of Functions C program has two types of functions: Library Functions 2. User defined functions Library Functions: These are the functions which are already written, compiled and placed in C Library and they are not required to be written by a programmer.
The functions name, its return type, their argument number and types have been already defined. We can use these functions as required. User defined Functions: These are the functions which are defined by user at the time of writing a program. The user has choice to choose its name, return type, arguments and their types.
The job of each user defined function is as defined by the user. A complex C program can be divided into a number of user defined functions. Writing, compiling, and debugging C programs. Hello world. PDF 2 Variables and datatypes, operators. PDF 3 Control flow. Functions and modular programming. Variable scope. Static and global variables. PDF 4 More control flow. Input and output.
PDF 5 Pointers and memory addressing. Arrays and pointer arithmetic. Searching and sorting algorithms. PDF 6 User-defined datatypes, structs, unions, bitfields.
Memory allocation. Linked lists, binary trees. Pages The Basics of C Programming. Data Types, Operators, and Functions. Selection and Repetition Constructs. More About Modular Programming.
User-Defined Data Objects. Searching and Sorting Algorithms. Basic Statistics and Numerical Analysis. Back Matter Pages