I – PROGRAMMING in C and C++ SBSA1102

I – PROGRAMMING in C and C++ SBSA1102

SCHOOL OF COMPUTING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING UNIT – I – PROGRAMMING IN C and C++ SBSA1102 PROGRAMMING IN C and C++ UNIT – I BASIC OF C PROGRAMMING INTRODUCTION TO C • C is a procedural programming language. • It was initially developed by Dennis Ritchie in the year 1972. • It was mainly developed as a system programming language to write an operating system. FEATURES OF C • It is a robust language with rich set of built-in functions and operators that can be used to write any complex program. • The C compiler combines the capabilities of an assembly language with features of a high-level language. • Programs Written in C are efficient and fast. This is due to its variety of data type and powerful operators. • It is many time faster than BASIC. • C is highly portable this means that programs once written can be run on another machines with little or no modification. • Another important feature of C program, is its ability to extend itself. • A C program is basically a collection of functions that are supported by C library. We can also create our own function and add it to C library. • C language is the most widely used language in operating systems and embedded system development today. STRUCTURE OF A C PROGRAM HEADER FILE # include<stdio.h> Main ( ) int main( ) { Variable Declaration int a = 10; Body of program printf (“%d”, a); Return return 0; } HEADER FILE • The first and foremost component is the inclusion of header file. It is a file with extension .h • Example : stdio.h define input and output function • Syntax : # include MAIN METHOD • The next part of C program is to declare the main ( ) function • Syntax : int main ( ) { } VARIABLE DECLARATION • The next part of any C program is variable declaration. • It refers to variables that are to be used in function • Syntax : int main ( ) { int a ; ….. BODY OF THE PROGRAM • It refers to the operation that are performed in functions • Syntax: Int main ( ) { Int a; Printf (“%d”, a); …. RETURN STATEMENT • This is last part in any C program in return statement • It refers to returning of statement of values from function • Syntax: C - DATA TYPES • The data-type in a programming language is the collection of data with values having fixed meaning as well as characteristics. Some of them are an integer, floating point, character, etc. • A data-type in C programming is a set of values and is determined to act on those values. • C provides various types of data-types which allow the programmer to select the appropriate type for the variable to set its value. C DATA TYPES ARE USED TO • Identify the type of a variable when it declared. • Identify the type of the return value of a function. • Identify the type of a parameter expected by a function. C PROVIDES THREE TYPES OF DATA TYPES • Primary(Built-in) Data Types: void, int, char, double and float. • Derived Data Types: Array, References, and Pointers. • User Defined Data Types: Structure, Union, and Enumeration. PRIMARY (BUILT-IN) DATA TYPES DERIVED DATA TYPES USER DEFINED DATA TYPES • C allows the feature called type definition which allows programmers to define their identifier that would represent an existing data type. There are three such types: C – TOKENS • The compiler breaks a program into the smallest possible units and proceeds to the various stages of the compilation, which is called token. IDENTIFIERS • Identifiers are names given to different names given to entities such as constants, variables, structures, functions etc. Rules for Identifiers • An identifier can only have alphanumeric characters (a-z , A-Z , 0-9) (i.e. letters & digits) and underscore( _ ) symbol. • Identifier names must be unique • The first character must be an alphabet or underscore. • You cannot use a keyword as identifiers. • Only first thirty-one (31) characters are significant. • Must not contain white spaces. • Identifiers are case-sensitive. KEYWORDS • The C Keywords must be in your information because you cannot use them as a variable name. CONSTANTS • Constants are like a variable, except that their value never changes during execution once defined. • C Constants is the most fundamental and essential part of the C programming language. • Constants in C are the fixed values that are used in a program, and its value remains the same during the entire execution of the program. • Constants are also called literals. • Constants can be any of the data types. • It is considered best practice to define constants using only upper-case names. CONSTANTS TYPES • Constants are categorized into two basic types, and each of these types has its subtypes/categories. OPERATORS • C operators are symbols that are used to perform mathematical or logical manipulations. • The C programming language is rich with built-in operators. • Operators take part in a program for manipulating data and variables and form a part of the mathematical or logical expressions. TYPES C - INPUT AND OUTPUT • Input means to provide the program with some data to be used in the program • Output means to display data on screen or write the data to a printer or a file. • C programming language provides many built-in functions to read any given input and to display data on screen when there is a need to output the result. scanf() and printf() functions • The standard input-output header file, named stdio.h contains the definition of the functions printf() and scanf(), which are used to display output on screen and to take input from user respectively. getchar() & putchar() functions • The getchar() function reads a character from the terminal and returns it as an integer. This function reads only single character at a time. • The putchar() function displays the character passed to it on the screen and returns the same character. This function too displays only a single character at a time. gets() & puts() functions • The gets() function reads a line from stdin (standard input) into the buffer pointed to by str pointer, until either a terminating newline or EOF (end of file) occurs. • The puts() function writes the string str and a trailing newline to stdout (standard output) Difference between scanf() and gets() • The main difference between these two functions is that scanf() stops reading characters when it encounters a space, but gets() reads space as character too. DECISION MAKING IN C • Decision making is about deciding the order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met. • C language handles decision-making by supporting the following statements, o if statement o switch statement o conditional operator statement (? : operator) o goto statement Decision making with if statement • The if statement may be implemented in different forms depending on the complexity of conditions to be tested. • The different forms are, o Simple if statement o if....else statement o Nested if. .. else statement o Using else if statement Simple if statement • The general form of a simple if statement is, • If the expression returns true, then the statement-inside will be executed, otherwise statement-inside is skipped and only the statement-outside is executed. if...else statement • The general form of a simple if...else statement is, • If the expression is true, the statement-block1 is executed, else statement-block1 is skipped and statement-block2 is executed. Nested if. .. else statement • The general form of a nested if. else statement is, • if expression is false then statement-block3 will be executed, otherwise the execution continues and enters inside the first if to perform the check for the next if block, where if expression 1 is true the statement-block1 is executed otherwise statement-block2 is executed. else if ladder • The expression is tested from the top(of the ladder) downwards. As soon as a true condition is found, the statement associated with it is executed. SWITCH STATEMENT IN C • Switch statement is a control statement that allows us to choose only one choice among the many given choices. • The expression in switch evaluates to return an integral value, which is then compared to the values present in different cases. • It executes that block of code which matches the case value. • If there is no match, then default block is executed (if present). • The general form of switch statement is, Rules for using switch statement • The expression (after switch keyword) must yield an integer value i.e the expression should be an integer or a variable or an expression that evaluates to an integer. • The case label values must be unique. • The case label must end with a colon (:) • The next line, after the case statement, can be any valid C statement. • break statements are used to exit the switch block. It isn't necessary to use break after each block, but if you do not use it, then all the consecutive blocks of code will get executed after the matching block. EXAMPLE Difference between switch and if • if statements can evaluate float conditions. switch statements cannot evaluate float conditions. • if statement can evaluate relational operators. switch statement cannot evaluate relational operators i.e they are not allowed in switch statement. LOOPING STATEMENTS IN C • In any programming language including C, loops are used to execute a set of statements repeatedly until a particular condition is satisfied. • As per the above diagram, if the Test Condition is true, then the loop is executed, and if it is false then the execution breaks out of the loop. • After the loop is successfully executed the execution again starts from the Loop entry and again checks for the Test condition, and this keeps on repeating. TYPES OF LOOP • There are 3 types of Loop in C language, namely: o while loop o for loop o do while loop The for loop is executed as follows: • It first evaluates the initialization code.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    165 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us