Fundamentals of C Programming PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document provides an introduction to C programming concepts and elements such as data, information, programs, and executing and running C programs. It also describes the different types of programming languages, and the role of compilers and interpreters.
Full Transcript
FUNDAMENTAL S OF C LANGUAGE C-Language What is Computer language?- It is used for communication between user and machine. C programming language developed at bell labs by Dennis Ritchie in 1972. Introduction to Programming Languages Data Data is...
FUNDAMENTAL S OF C LANGUAGE C-Language What is Computer language?- It is used for communication between user and machine. C programming language developed at bell labs by Dennis Ritchie in 1972. Introduction to Programming Languages Data Data is raw, unorganized facts that need to be processed. Data can be something simple and seemingly random and useless until it is organized. For example, Each student's test score is one piece of data. Information When data is processed, organized, structured or presented in a given context so as to make it useful, it is called information. The average score of a class or of the entire school is information that can be derived from the given data. Program A Program is a set of instructions written by following the syntax of programming languages to perform a well defined task. Execution of a Program or Running a Program The process of Loading or Storing instructions of a program in the computers main memory is called as execution of program. The instructions in the main memory are ready to be executed by the micro processor. Types of Computer Languages Computer programming languages are mainly classified into two parts 1) Low level programming language 2) High level programming language Low level programming language Low level programming language means machine level language or binary language in the form of 0’s and 1’s. Machine can easily understand the low level language. High level programming language High level programming language is a language that programmers or user can easily understand. There are two ways to translate high level code into machine level 1) Compiler 2) Interpreter Compiler A compiler is a piece of code that translates the high level language into machine language. When a user writes a code in a high level language such as C and wants it to execute, a specific compiler which is designed for C is used before it will be executed. The compiler scans the entire program first and then translates it into machine code which will be executed by the computer processor and the corresponding tasks will be performed. Interpreter Interpreters are not much different than compilers. They also convert the high level language into machine readable binary equivalents. But it converts one statement at a time. Each time when an interpreter gets a high level language code to be executed, it converts the code into an intermediate code before converting it into the machine code. Algorithm An algorithm is a step by step representation of a given problem in English like statements. Algorithm is a problem solving activity. Every algorithm must be made up of following steps. o Step1: Start o Step2: Body of the algorithm. o Step3: Stop. Examples for an Algorithm: ▪ Write an algorithm to print “Hello World” on the console. Step1: Start Step2: Print “Hello World” by using printf statement. Step3: Stop. ▪ Write an algorithm to find the addition of two numbers. Step1: Start Step2: Input two values into the variables ‘a’ and ‘b’ from the keyboard. Step3: Find the addition of ‘a’ and ‘b’ i.e., a+b and assign the resultant value to the variable ‘c’ Step4: Print the addition value ‘c’. Step5: Stop Flowchart The pictorial presentation of program is called Flowchart. It is a tool and technique to find out solution of programming problems through some special symbols. It is a way to represent program using geometrical patterns. The graphical or visual representation of algorithm is called as flow chart. The flow charts are easier to understand the flow of the solution. Flow charts are drawn with the standard symbols accepted worldwide. Each standard flow chart symbol represents on action to be performed such as Start or Stop, input operations Read or Write, decision making etc. Standard flowchart symbols Basic structure of C Programming Documentation Section: it consists of a set of comment lines giving the name of the program. Preprocessor Statements or Header File Section: The preprocessor statement begins with # symbol and is also called the preprocessor directive. These statements instruct the compiler to include C preprocessors such as header files and symbolic constants before compiling the C program. Some of the preprocessor statements are listed below Global Declaration Section: The variables are declared before the main ( ) function as well as user defined functions is called global variables. These global variables can be accessed by all the user defined functions including main ( ) function. The main ( ) function : Each and Every C program should contain only one main ( ). The C program execution starts with main ( ) function. No C program is executed without the main function. The main ( ) function should be written in small (lowercase) letters and it should not be terminated by semicolon. Main ( ) executes user defined program statements, library functions and user defined functions and all these statements should be enclosed within left and right braces. Local Declarations The variable declaration is a part of C program and all the variables are used in main ( ) function should be declared in the local declaration section is called local variables. Program statements These statements are building blocks of a program. They represent instructions to the computer to perform a specific task (operations). An instruction may contain an input-output statements, arithmetic statements, control statements, simple assignment statements and any other statements and it also includes comments that are enclosed within. The comment statements are not compiled and executed and each executable statement should be terminated with semicolon. User defined functions These are subprograms, generally, a subprogram is a function and these functions are written by the user are called user defined functions. These functions perform user specific tasks and this also contains set of program statements. They may be written before or after a main () function and called within main () function. This is an optional to the programmer Rules to write C program The following are some of rule to write C programs. All C statements must end with semicolon. C is case sensitive. That is upper case and lower case letter are distinct. A C statement can be written in one or it can split into multiple lines. Braces must always match upon pairs, i.e., every open brace { must have a matching closed brace }. No restriction in using blank spaces or blank line in program segments. But spaces not allowed within variable, constant or keyword. Comments cannot be nested. For example */ is not valid. A comment can be split into more number of lines. The global variables can be declared outside of all functions. E.g. #include main { printf(“Hello”); } Language Fundamentals Character Set: character set is used to form words, numbers and expressions depending upon the computer on which the programs run. The characters in the C are categorized in the following way. Tokens Keywords Identifiers In C the names of variables, functions, labels and various other user-defined items are called as identifiers. Rules for naming identifiers: The first character of an identifier should be either an alphabet or an underscore, and then it can be followed by any of the character, digit, or underscore. It should not begin with any numerical digit. In identifiers, both uppercase and lowercase letters are distinct. Therefore, we can say that identifiers are case sensitive. Commas or blank spaces cannot be specified within an identifier. Keywords cannot be represented as an identifier. The length of the identifiers should not be more than 31 characters. Identifiers should be written in such a way that it is meaningful, short, and easy to read. Variables Variables are containers for storing data values. Rules for naming variables: A variable name can only have letters (both uppercase and lowercase letters), digits and underscore. The first letter of a variable should be either a letter or an underscore. There is no rule on how long a variable name (identifier) can be. However, you may run into problems in some compilers if the variable name is longer than 31 characters. Syntax: type variableName = value; Where type is one of C types(such as int), variableName is the name of the variable (such as x or myname). The equal sign is used to assign a value to the variable. e.g. int myNum = 15; Data types Ranges of primary data types Types of Operators in C Operator Precedence and Associativity When the expression involves more than one operation, then which operation is to be performed first is decided by the precedence of that operator. Highest precedence operation is solved first. The operators with the same precedence are evaluated according to there associativity. i.e. the expression is evaluated either from left to right or right to left. In short, operator precedence determines a rank for the operators in an expression while associativity determines the grouping of operations among operators of the same precedence. Expressions It is a statement in C language. An expression in C is a combination of operands and operators to obtain some computation. e.g z=x+y; Arithmetic expressions: An arithmetic expression is an expression that consists of operands and arithmetic operators. e.g. 1 + 16 / Relational expression: Relational expression is an expression which involves relational operators. e.g. x%2 = = 0 / a>=9 Logical expressions: Logical expressions is an expression which involves logical operators e.g. ( x > 4 ) && ( x < 6 ) / x > 10 || y 22) ? 'Married': 'Unmarried';