C Programming Structures and Commands By Aradhya Malhotra

‘C’ Programming Structures and Commands By Aradhya Malhotra

Background C is a general purpose programming language that was founded in 1972 by Dennis Ritchie Unlike previously developed languages C provides facilities for structured programming and allows lexical variable scope and recursion It is the predecessor to C++ but and does not support the Object Oriented Programming Style (OOPS) paradigm

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

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. Iso 646. 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 }

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…

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; }

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

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!”); }

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_value; }

Additional Concepts Operators and Expressions Arrays Linked Lists Pointers Stacks and Queues Hash Tables
- Slides: 10