1 / 14

‘C’ Programming Structures and Commands

‘C’ Programming Structures and Commands. By Sidhant Garg. Brief Introduction Of C. C was developed between 1969-1973 by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.

palani
Download Presentation

‘C’ Programming Structures and Commands

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ‘C’ Programming Structures and Commands By Sidhant Garg

  2. Brief Introduction Of C • C was developed between 1969-1973 by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System. • Unlike previously developed languages C provides facilities for structured programming and allows lexical variable scope and recursion • C is one of the most popular programming languages and there are a very few computer architecture for which the C compiler does not exist.

  3. Structure of C C programs basically contains the following: • Preprocessor Commands • Variables • Constants • Input/output • Functions

  4. Preprocessor commands • The #include pre-processor directive is used to link required library files to the main program file (.cpp) • Some of the common header files and their functions are as follows: • Stdio.h : Standard input /output functions • Math.h: Basic math functions such as tan, log, pow(x,y) etc. • String.h: String operations such as strcpy, strcat etc. • Stlib.h: Standard functions such as malloc, rand, srand etc. • Ctype.h: Character classification functions such as isalnum, isalpha, isdigit etc. • Iso646.h: To use macros for logical operators • E.g. #include<stdio.h> void main() { int x; scanf(“%d”,x); //needs the stdio.h header file }

  5. Example of a program to convert celsius to fahrenheit • #include <stdio.h> • /* print Fahrenheit-Celsius table • for fahr = 0, 20, ..., 300 */ • main() • { • intfahr, celsius; • int lower, upper, step; • lower = 0; /* lower limit of temperature scale */ • upper = 300; /* upper limit */ • step = 20; /* step size */ • fahr = lower; • while (fahr <= upper) { • celsius = 5 * (fahr-32) / 9; • printf("%d\t%d\n", fahr, celsius); • fahr = fahr + step; • }

  6. Variables – Data types • In C, variables can be broadly of the following data types: • char – character type • char stringname [x] (x=1,2..n)- string of ‘x’ characters • int array [x] – array containing ‘x’ elements • int- integer type • float- real numbers including decimals etc. • double- extended form of float (larger range) • The above can also be used in conjunction with the following ‘datatype’ modifiers: • long • short • signed • unsigned cont…

  7. Variables – Data types cont... • In addition to the previously described standard datatypes, C allows the following user defined datatypes: • typedef- Allows users to change the name of a standard data type. For e.g. typedef float money will allow the user to use money as a data type • enum- Enumerated data types similar to C++ e.g. enum color{red,blue,white} • struct- • Structures are heterogeneous user-defined datatypes • They are often used to create nodes and lists • They are like the data members of classes but do not have the concept of associated functions ` e.g. struct features{ int height; double weight; char[5] color; }

  8. Constants • In C, constants can be defined in the following two ways- • Using #define e.g. #define pi 3.14 • Using const e.g. const double x=3.0 • Constants are often declared globally instead of being used directly in the main program.

  9. Input / Output • User input from the keyboard is done using ‘scanf ‘ • Output to the screen is done using ‘printf’ • Here are the format specifiers that are required • %d - int • %c - char • %f - float • %lf -double • %s -string • %x- hexadecimal • For e.g. #include<stdio.h> void main() { int x; scanf(“Enter integer: %d”,x); printf(“The integer you entered is: %d,x,”. Goodbye!”); }

  10. Functions • Functions are modules or subprograms that accept certain parameters and return values to the calling function / main program • They have the following format: Return_type function_name(parameters) { local variables; C statements; return return_value; }

  11. In C all the function arguments are passed by value. This means that the functions are given the values of it arguments in temporary variable rather than the originals. • It usually leads to more compact programs with fewer extraneous variables, because parameters can be treated as conveniently initialized local variables in the called routine

  12. For example, here is a • version of power that makes use of this property. • /* power: raise base to n-th power; n >= 0; version 2 */ • int power(int base, int n) • { • int p; • for (p = 1; n > 0; --n) • p = p * base; • return p; • } • The parameter n is used as a temporary variable, and is counted down (a for loop that runs backwards) until it becomes zero; • there is no longer a need for the variable i. Whatever is done to n inside power has no effect on the argument that power was • originally called with.

  13. Other Concepts • Operators and Expressions • Arrays • Linked Lists • Pointers • Stacks and Queues • Hash Tables

  14. QUESTIONS

More Related