Download presentation
Presentation is loading. Please wait.
Published byPhoebe Evans Modified over 9 years ago
1
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake
2
Chapter 2: Introduction to C Background Structure of C Programs Identifiers Data types Variables Constants
3
Background A structured language Allows programmer to concentrate on the problem at hand Runs on different hardware platforms-popular Derived from ALGOL Dennis Ritchie developed C
4
Structure of a C Programs 1. Preprocessor directives 2. Global Declarations 3. Main 4. Local Definitions 5. Statements 6. Return type
5
. 1. Preprocessor directives Special instructions to the preprocessor to include library systems, header files-eg. input/output library Starts with # sign, no space between # and include Preferable in the first line.h or header files 2. Global Declarations are v isible to the whole program
6
. 3. Main only one in the whole program eg. int main(void) {} are used ; ends each statements /* This is a comment*/ Inner comments are not allowed 4. Local Definitions definition in the function 5. Statements - instructions to the computer 6. Return type
7
Identifiers Allows us to name the data and other objects Valid names are capital letters, lowercase letters, digits and the underscore The first character cannot be a digit System libraries use underscore as the first character by custom. Names cannot be 30-plus special names known as reserved words-more on appendix B Good identifiers are Descriptive but short
8
Standard Data Types 1. void has no type, plays the role of generic type 2. int - integer type without a fraction, can be short int, int,long int. Integer data type can be divided into signed or unsigned types eg. Pg 33, Table 2-4 3. char- includes any value that can be represented in the computer’s alphabet (ASCII), normally size is 1 byte Difference between a and x? 0110 0001 and 0111 1000 4. float is a number with fractional part,can be float double, and long double, is always signed type pg35, table 2-5
9
Logical Data Type Boolean can represent only two values, referred to as true and false or on/off C supports logical type through the integer type, any nonzero represent true and zero represent false
10
Variables Variable are named memory locations that have a type They can be changed or manipulated Each variable must be declared and defined They are used to hold the data required by the program for its operation. Its type can be char, int, and float but not void eg char a, int i, float tax. See pg 36, fig2-9, pg 37, Table2-7
11
Variable Initialization Variable can also be initialized at the time of declaration Must be defined before using them Initializer establishes the first value that the variable will contain int number, sum=0; int number; int sum; They are not initialized automatically
12
Constants Constants are data types that cannot be changed during the execution of a program Examples, pg 39 Table 2-8 int eg. 123 long int eg. 32,271L unsigned long int eg. 76,542LU Default is signed integer or long signed integer
13
Float Constants They are numbers with decimal parts. Default form is double double 2.5 long double 3.1415926536L
14
Character Constants Character constants are enclosed between single quotes Backslash is the escape character is used when it cannot be entered from keyboard Example of character set are: null character ‘\0’ alert(bell) ‘\a’ newline ‘\n’ Table 2-10
15
. String Constants: is a sequence of zero or more characters enclosed in double quotes “ “hello\n” “WELCOME!” “” is an empty string while ‘\0’ is a null character
16
Coding Constants Literal Contants for eg, 5, a, “hello world”, a+5 Defined Constants #define SALESTAX.0825 Memory Constants Memory constants use a type qualifier eg. const float PI = 3.14
17
Formatted Input/Output Input reads formatted data from keyboard The keyboard is called Standard input file Output The monitor is called Standard output file The keyboard input is usually buffered, ie a temporary storage area holds inputs until one complete line has been recieved
18
The standard output function is printf The standard input function is scanf printf needs two things: instructions for formatting data and the actual data to be printed. Format string is coded like (“…”) printf (“%d\n%d”, a,b) Conversion codes are: character ( c ), integer(d), and floating poing ( f )
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.