Morteza Noferesti
Have the same meaning as variables in algebra ◦ Single alphabetic character ◦ Each variable needs an identifier that distinguishes it from the others a = 5 x = a + b valid identifier in C may be given representations containing multiple characters ◦ A-Z, a-z, 0-9, and _ (underscore character) ◦ First character must be a letter or underscore (no, _no 9no) ◦ Usually only the first 32 characters are significant ◦ There can be no embedded blanks (student no) ◦ Identifiers are case sensitive (area, Area, AREA, ArEa) ◦ Keywords cannot be used as identifiers
Before using a variable, you must declare it All variables must be defined with a name and a data type. Data_Type Identifier; int width; // width of rectangle float area; // result of calculating area stored in it char separator; // word separator Data_Type Identifier = Initial_Value; int width = 10; // width of rectangle float area = 255; // result of calculating area stored in it char seperator = ‘,’; // word separator Data_Type Identifier, Identifier, Identifier; • int width, length, temporary; • float radius, area = 0;
When we declare a variable ◦ Space is set aside in memory to hold a value of the specified data type ◦ That space is associated with the variable name ◦ That space is associated with a unique address Visualization of the declaration ◦ int width = 95; // get width form user ◦ // &width is 22ff40 ◦ // *&width is 95 ◦ // sizeof width is 4
Minimal set of basic data types ◦ primitive data types int float double char Void The size and range of these data types may vary among processor types and compilers
Modify the behavior of data type to which they are applied: ◦ Size qualifiers: alter the size of the basic data types: short: multiply by 0.5 long: multiply by 2 short can be applied to: int long can be applied to: int and double ◦ Sign qualifiers: can hold both positive and negative numbers, or only positive numbers.: signed: + and - unsigned: + they can be applied to : int and char
A floating-point value contains a decimal point ◦ 33.5 0.0 -657.983 .2 6. For example, the value 150.4582 is represented in scientific notation as ◦ 1.504582 X 102 and is represented in exponential notation (by the computer) as ◦ 1.504582E+02 This notation indicates that 1.504582 is multiplied by 10 raised to the second power (E+02) ◦ The E stands for “exponent”
Char ◦ char c; ◦ c = 'A'; // d = 65; String ◦ printf("string is array of char!!!"); ◦ printf("example of escape sequence is \n");
The sizeof keyword returns the number of bytes of the given expression or type returns an unsigned integer result ◦ sizeof variable_Identifier; ◦ sizeof (variable_Identifier); ◦ sizeof (Data_Taype); Example: ◦ int x; ◦ printf("size of x = %d", sizeof x); ◦ printf("size of long long = %d", sizeof(long long)); ◦ printf("size of x = %d", sizeof (x))
Type Casting In C Language. Type casting is a way to convert a variable from one data type to another data type. For example, if you want to store a long value into a simple integer then you can type cast long to int.
If an operand is long double, the other operand is converted to long double! Else if an operand is double, the other operand is converted to double! Else if an operand is float, the other operand is converted to float! Else if an operand is unsigned long, the other operand is converted to unsigned long! Else if an operand is long, the other operand is converted to long! Else if an operand is unsigned int, the other operand is converted to unsigned int!
char ch; int i; float f; double d; result= (ch/i) + (f*d) - (f+i); int double float int double float double double double
May lose the data! char ch; int i; float f; double d; … ch=i; i=f; f=ch; f=i;
sourc rce destin inatio tion The lost data char signed char If char>127, destination will be negative short int char 8 most significant bits int char 8 most significant bits (16 bits system) 24 most significant bits(32bits system) long int char 24 most significant bits int short int No lost data! (16 bits system) 16 most significant bits(32 bits system) long int int 16 most significant bits No lost data! (16 bits system) float int The mantissa part double float Precision lost & the data is rounded long double double Precision lost & the data is rounded
sizeof of(int) sizeof eof(char)
#include <stdio.h> int main() { int num; #include <stdio.h> int a=10,b=3; int main() float c; { c=a/b; float a=10; printf("the result without casting %f\n",c); int b=3; float c=a/b; c=(float)a/b; printf("%f",c); printf("the result with casting %f\n",c); printf("%d", a/b); return 0; } }
This function provides for formatted output to the screen. The syntax is: ◦ printf("format", var1, var2 , … ) ; The format includes, some text and conversion specifications A conversion specifier begins with the % character. After the % character come the following in this order: ◦ [ flags ]:control the conversion( optional ) ◦ [ width ]:the number of characters to print( optional ) ◦ [ .precision ]:the amount of precision to print for a number type( optional ) ◦ [ modifier ]:overrides the size (type) of the argument( optional ) ◦ [ type ]:the type of conversion to be applied( required ) Example:%[ flags ][ width ][ .precision ][ modifier ] type
These functions take input in a manner that is specified by the format argument and store each input field into the following arguments in a left to right fashion Each input field is specified in the format string with a conversion specified which specifies how the input is to be stored in the appropriate variable Other characters in the format string specify characters that must be matched from the input, but are not stored in any of the following arguments If the input does not match then the function stops scanning and returns A white space character may match with any white space character (space, tab, carriage return, newline, vertical tab, or form feed) or the next incompatible character
The conditional operator (?:) is used to simplify an if/else statement ◦ Condition ? Expression1 : Expression2; The statement above is equivalent to: if (Condition) Expression1; else Expression2; Which are more readable?
Example: if/else conditional statement: statement: (total > 12 ) ? grade = ‘P’: grade if (total > 12) = ‘F’; grade = ‘P’; else OR grade = ‘F’; grade =( total > 12 ) ? ‘P’: ‘F’;
Example: if/else statement: if (total > 12) printf (“Passed!! \ n”); else printf (“Failed!! \ n”); Conditional Statement: printf (“%s!! \ n”, total > 12 ? “Passed”: “Failed”);
The rules specify which of the operators will be evaluated first ◦ For example: x = 3 * a - ++b%3; Precedence Operator Associativity Level 1 (highest) () left to right 2 unary right to left 3 * / % left to right 4 + - left to right = += -= *= /= 5 (lowest) right to left %=
how would this statement be evaluated? ◦ : x = 3 * a - ++b % 3; ◦ What is the value for X, for: a = 2, b = 4? x = 3 * a - ++b % 3; x = 3 * a - 5 % 3; x = 6 - 5 % 3; x = 6 – 2; x = 4;
If we intend to have a statement evaluated differently from the way specified by the precedence rules, we need to specify it using parentheses ( ) ◦ x = 3 * a - ++b % 3; Consider having the following statement: ◦ x = 3 * ((a - ++b)%3); ◦ the expression inside a parentheses will be evaluated first The inner parentheses will be evaluated earlier compared to the outer parentheses
how would this statement be evaluated? ◦ x = 3 * ((a - ++b)%3); ◦ What is the value for X, for: a = 2, b = 4? x = 3 * ((a - ++b) % 3); x = 3 * ((a - 5) % 3); x = 3 * ((-3) % 3); x = 3 * 0; x = 0;
Recommend
More recommend