C Programming

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View C Programming as PDF for free.

More details

  • Words: 1,278
  • Pages: 6
C-PROGRAMMING

C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie.

Constants, Variables and Keywords Types of C Constants C constants can be divided into two major categories: (a) Primary Constants (b) Secondary Constants

Rules for Constructing Integer Constants (a) An integer constant must have at least one digit. (b) It must not have a decimal point. (c) It can be either positive or negative. (d) If no sign precedes an integer constant it is assumed to be positive. (e) No commas or blanks are allowed within an integer constant. (f) The allowable range for integer constants is -32768 to 32767.

Rules for Constructing Real Constants i.e Floating point numbers (a) A real constant must have atleast one digit. (b)It must have a decimal point. (c) It could either be positive or negative. (d)Default sign is positive. (e) No commas or blanks are allowed.

Rules for Constructing Character Constants (a) A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. Both the inverted commas should point to the left. For example, ’A’ is a valid character constant whereas ‘A’ is not. (b)The maximum length of a character constant can be 1 character.

Variables (a) A variable name is any combination of 1 to 31 alphabets, digits or underscores. (b) The first character in the variable name must be an alphabet or underscore. (c) No commas or blanks are allowed within a variable name. (d) No special symbol other than an underscore can be used in a variable name.

C Keywords Keywords are the words whose meaning has already been explained to the C compiler (or in a broad sense to the computer). The keywords cannot be used as variable names because if we do so we are trying to assign a new meaning to the keyword, which is not allowed by the computer.

General Rules to write a C Program: (a) Each instruction in a C program is written as a separate statement. Therefore a complete C program would comprise of a series of statements. (b) The statements in a program must appear in the same order in which we wish them to be executed; unless of course the logic of the problem demands a deliberate ‘jump’ or transfer of control to a statement, which is out of sequence. (c) Blank spaces may be inserted between two words to improve the readability of the statement. However, no blank spaces are allowed within a variable, constant or keyword. (d) All statements are entered in small case letters. (e) C has no specific rules for the position at which a statement is to be written. That’s why it is often called a free-form language. (f) Every C statement must end with a ;. Thus ; acts as a statement terminator.

Three types of instructions in C: Type declaration instruction



To declare the type of variables used in a C program.

Arithmetic instruction



To perform arithmetic operations between con-stants and variables.

Control instruction



To control the sequence of execution of various statements in a C program.

A decision control instructions: The if statement The if-else statement The conditional operators

The IF statement if ( condition ) statement;

The IF-ELSE statement if( condition1 ) statement 1; else statement 2;

OR if( condition1 ) statement 1; else if( condition2 ) statement 2;

The WHILE loop initialise loop counter ; while ( test loop counter using a condition ) { do this ; and this ; increment loop counter ; }

The FOR statement for ( initialise counter ; test counter ; increment counter ) { do this ; and this ; and this; }

When break is encountered inside any loop, control automatically passes to the first statement after the loop. A break is usually associated with an if.

SWITCH statement The control statement that allows us to make a decision from the number of choices is called a switch. switch ( integer expression ) { case constant 1 : do this ; case constant 2 : do this ; case constant 3 : do this ; default : do this ; }

The switch executes the case where a match is found and all the subsequent cases and the default as well.

First, the integer expression following the keyword switch is evaluated. The value it gives is then matched, one by one, against the constant values that follow the case statements. When a match is found, the program executes the statements following that case, and all subsequent case and default statements as well. unless break is used. If no match is found with any of the case statements, only the statements following the default are executed.

switch Versus if-else Ladder There are some things that you simply cannot do with a switch. These are: (a) A float expression cannot be tested using a switch

(b)Cases can never have variable expressions (c) Multiple cases cannot use same expressions.

Functions and Pointers What is a Function? A function is a self-contained block of statements that perform a coherent task of some kind. Every C program can be thought of as a collection of these functions. (a) C program is a collection of one or more functions. (b) A function gets called when the function name is followed by a semicolon. (c) A function is defined when function name is followed by a pair of braces in which one or more statements may be present. (d) Any function can be called from any other function. Even main( ) can be called from other functions. (e) A function can be called any number of times. (f) The order in which the functions are defined in a program and the order in which they get called need not necessarily be same. (g) A function can be called from other function, but a function cannot be defined in another function. (h) There are basically two types of functions: Library functions Ex. printf( ), scanf( ) etc. User-defined functions Ex. argentina( ), brazil( ) etc.

Why Use Functions (a) Writing functions avoids rewriting the same code over and over. Suppose you have a section of code in your program that calculates area of a triangle. If later in the program you want to calculate the area of a different triangle, you won’t like it if you are required to write the same instructions all over again. Instead, you would prefer to jump to a ‘section of code’ that calculates area and then jump back to the place from where you left off. This section of code is nothing but a function. (b) Using functions it becomes easier to write programs and keep track of what they are doing. If the operation of a program can be divided into separate activities, and each activity placed in a different function, then each could be written and checked more or less independently. Separating the code into modular functions also makes the program easier to design and understand.

What are Pointers? A pointer is a memory variable that stores a memory address. Pointer can have any name that is legal for other variable and it is declared in the same fashion like other variables but it is always denoted by ‘*’ operator.

Features of Pointers: (a) Pointers save memory space. (b) Execution time with pointer is faster because data is manipuletd with the address i.e. direct access to memory allocation. (c) The memory is accessed efficiently with pointers. (d)Pointers are used with data structures. They are useful for representing two-dimensional and multi-dimentional arrays.

Related Documents

C Programming
November 2019 23
C Programming
July 2020 6
C Programming
June 2020 7
C Programming
October 2019 23
C Programming
November 2019 18
C (programming In C)
November 2019 32