Presentation is loading. Please wait.

Presentation is loading. Please wait.

WEEK-2.

Similar presentations


Presentation on theme: "WEEK-2."— Presentation transcript:

1 WEEK-2

2 Numbers in ‘C’ Two general categories: Integers Floats
Unsigned – all positive integers Signed – positive and negative integers Floats 2 2

3 Constants in C There are 3 basic types of constants in C.
An integer constant is an integer-valued number.We will concern here solely with decimal constants like 0, 1, 743, 5280, or -764. A floating point constant is a base-10 number than contains either a decimal point or an exponent or both like 0., 1., 0.2, 50.0, 12.3, , 2E-8 or 0.006e-3. A character constant is a single character enclosed in apostrophes like 'a', 'x', '9', or '?'.

4 Preprocessor directives
Preprocessor: A system program that modifies the C program prior to compilation. Preprocessor directive: An instruction to the preprocessor. Begins with #. Library: A collection of functions, symbols and values. 2 kinds of preprocessor directives: includes and defines.

5 Preprocessor directives
#include <stdio.h>: stdio.h, which stands for "standard input/output header", is the header in the C standard library that contains macro definitions, constants, and declarations of functions and types used for various standard input and output operations. The #include <stdio.h> directive must be included on top of every C program. #define PI : this is a constant macro definition. It associates a name to a value for the duration of the program In this case it associates the symbol PI to the value It is an optional directive.

6 Comments Comments are lines of code that are ignored by the compiler. They are placed for the programmer's benefit. /* this is a comment */ /* this is another comment, it can be spread over multiple lines */

7 Instructions Instructions in C are terminated by a semi-colon (;)
Line changes and tabs are not important to the C compiler. a=3; b=4; c=5; are the same as a=3; b=4; c=5;

8 Skeleton of a program #include <stdio.h>
/* optional additional includes */ /* optional constant macros */ int main (void) { /* optional declarative statements */ /* one or more executable statements */ return (0); }

9 return (0); /* PROGRAM # 1 */ #include <stdio.h>
/* This is my first C program. */ /* It’ll print a message to the display. */ int main (void ){ printf(“Welcome to the C programming language course!\n”); printf("This is our very first C program.\n"); printf(“We wish you a very pleasant experience with C.”); return (0); } 9 9

10 This is what we see after execution:
1st line: Welcome to the C programming language course! 2nd line: This is our very first C program. 3rd line: We wish you a very pleasant experience with C. 10 10

11 Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change values. All variables must be declared at the top of the program. There are three basic types of variables in C: int: for integer (whole numbers). double (or float): for real (floating point numbers). char: for characters.

12 Declaring Variables in ‘C’
All variables in ‘C’ must be declared. Compiler should know The variable name Type of the variable Why? In order to allocate enough memory to it , before the variable can be used. What are data types? char int, long float, double, long double void

13 How to declare? /*declare variable of type character.*/  This Denotes a comment in C char a_character; char letter; /*declare variable of type integer.*/ int an_integer; int number; /*declare variable of type float.*/ float floating_point_number; float average; The following is also valid: int age, number, mark;

14 Where to Declare? Declaration MUST happen at the top of the program, that is, the very first thing that we do, MUST be declaring the variables.

15 Identifiers All variables must have names. There are strict rules for variable names. These rules will apply to function names later so we will call these names identifiers. A declaration is done with the type followed by the identifier ;. Ex: int lifespan; double mass; char letter;

16 Hard rules for identifiers
Rule #1: An identifier must not be a reserved word. Reserved words are used by C exclusively. Here are a few: double, char, int, do, float, if, return, sizeof, void,while, typedef, struct, switch, for, else. See the complete list in the Documents section of the course website. Rule #2: An identifier must contain only letters, digits or underscores. Abc8 is valid, Abc-8 is not. _xyz is valid, atom number is not.

17 Hard rules for identifiers
Rule #3: An identifier must never begin with a digit. U238 and _765 are valid, 7abc and 67_q are not.

18 Soft rules for identifiers
Rule #4: An identifier should not be a standard identifier. A standard identifier is a name used by C but is not a reserved word. printf, scanf are examples of standard identifiers. Rule #5: All-capital names should be used only for constant macros. Variables and function should never use capital letters. Never mix upper-case and lower-case letters in a name.

19 How to select names for variables, functions, etc. in ‘C’?
Use the set of rules for picking up VALID names in C Use meaningful and descriptive names so we need less comments. Rules For Picking up Names 1. Names in ‘C’ are Case Sensitive, i.e., ‘C’ makes distinction between upper and lower-case letters. e.g., int number; int Number; /* number and Number are two different integer variables.*/ 2. User defined names cannot be the same as C keywords. i.e., names such as for, while, if are NOT valid user defined names. int for; /* invalid variable name. for is C keyword. */

20 Rules For Picking up Names
3. Every name must start with A letter of alphabet (upper, lower) Underscore ( _ ) e.g., int age; float Salary; double _code; 4. The remainder of the name may be Upper or lower case letters of alphabets Decimal digits (0-9) Underscore

21 What is a ‘C’ Program? A collection of keywords, variables, operators, expressions, statements, different data types, etc. that together performs one or more than one task. Therefore, we need to know the followings: C Keywords If For While C Different data types Integer  int Floating Point  float Character  char Void  void

22 Valid C Variables int month; float average; double PI; char name; C Operators Arithmetic Assignment and Compound Assignment Sizeof Relational Logical Conditional Increment/Decrement C Statements C Expressions, A valid sequence of operands and operators to calculate a value, usually expressing a math, logical calculation, …

23 ‘C’ Keywords Terms with pre-defined meaning in ‘C’. We must use the keywords within their intended meanings. Keyword definition cannot be changed.

24 Example: case used as part of switch
sizeof gives the size of the variable in byte for used for looping purposes do used as part of do-while char a data type double a data type long a data type return used to return a value from a function static used in combination with data types void a data type int a data type float a data type if used for conditional purposes else used a part of if switch used for selection purposes struct used to define a data structure enum used to define user defined data type while used for looping purposes

25 Numbers in C in a table int integer 2 -32,768 to +32,767 long
Data Type Purpose Bytes Range int integer 2 -32,768 to +32,767 long long integer 4 -2,147,483,648 to +2,147,483,647 unsigned unsigned integer 0 to 65,535 unsigned long unsigned long integer 0 to 4,294,967,295 float floating point 3.4E+/-38 double double float 8 1.7E+/-308 long double long double float 16 1.7E+/-4932

26 Are used to format the print out.
Escape Sequences Are used to format the print out. (\) symbol is referred to as the escape character AND is used to signify a escape sequence. Sequence Purpose \n New line \t Tab \" To print a double quote \\ To print a backslash

27 Note On the Use of Different Data Types
Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on your machine, you may need 1 byte for characters, 2 bytes for integers and 4 bytes for float. You also know that integers are in fact a subset of float & as such you may decide to use float type for all numerical values. Although this is mathematically correct however you would not have an efficient memory management here. You could have used 2-byte for integers but you used 4-byte instead!

28 Placeholders in I/O Statements
Placeholders (or conversion specifiers) will substitute the value of the variable inside the output string (printf). You must absolutely match the placeholder with the variable type. %lf: long floating point (double). %d: decimal (int). %c: character (char). \n: a special character meaning that a new line will be inserted.

29 Integer placeholders %d is the default integer placeholder. When used it will simply display the value as is without any padding. To add padding, to have columns for example, we need formatted placeholders. %nd will reserve n places to display the number. Justification will be to the right. The negative sign takes one place. If the value is 17 and %4d is used, then it will display 2 spaces followed by 17 on the screen. __17 These are spaces, not underscores.

30 Integer placeholders The number is always displayed in its entirety, even when the format is too narrow. With and a %3d placeholder, you would see -1234, therefore using 5 spaces instead of the 3 requested. A negative number change the justification to the left of the field. With a value of and a %-8d placeholder, you will get -1234___ . 3 trailing blanks

31 Make yourself ready for the lab!


Download ppt "WEEK-2."

Similar presentations


Ads by Google