Download presentation
Presentation is loading. Please wait.
Published byIrene Page Modified over 9 years ago
1
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1
2
Week 2 Introduction to C Programming Outline A Simple C Program: Printing a Line of Text Data Types Another Simple C Program: Adding Two Integers Arithmetic in C Decision Making: Equality and Relational Operators
3
2.1Recap: a simple program is the header file containing basic commands needed to write programs Comments (xxxx) are inside the signs /* xxxx */ You put it for another programmer to read, and the computer will ignore your comments 5. Enter the following codes in the editor window #iain() { #include void main() { /*Comments, for humans to read and computers to ignore, go here */ printf("259201 - Computer Programming for Engineers!\n"); } #include
4
2.1 More on Simple Program void main() You need to have only 1 main() program in your project void means the program does not return anything { the codes are inside these brackets }
5
2.1 printf() printf( "Welcome to C!\n" ); Statement inside “ ” the quote is displayed Every executable statement needs to end with semicolon (;) Escape character (\) specifies the special character or commands that follows, e.g., \n, \t
6
2.1Common Escape-Character Sequences
7
#include void main() { /*Comments, for humans to read and computers to ignore, go here */ printf("259201 - Computer Programming for Engineers!\n"); } 2.2Running the program
8
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.2Data Types in C-Language Six Basic Types covered: 1) integer (-15, 1, 21, 385, 9180) 2) float (-1.3568, -1.2, 81.0, 834.67, 1000) 3) Octal (012 = 10, 027 = 23) 4) Hexadecimal (0x12 = 18) 5) Character (‘a’, ‘0’, ‘p’, ‘:’) 6) String (“the world”, “Facebook”, “LOL”) 8
9
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Declare before using it 9
10
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Standard (ANSI) C data type Most common intAn integer (4 Bytes, 32 bits) floatA floating point (real) number (32 bits) char (kar)A single byte of memory, enough to store a character Less common longAn integer of increase range and memory unsignedAn integer (positive and zero only) used to increase the range shortAn integer of reduced range (2 Bytes, 16 bits) doubleA floating point number of increased range(64 bits) long double, unsigned int, etc. 10
11
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Example 1: int vs. short int #include void main() { int num1 = 33000; short int num2= 33000; printf("The data is: %d \n",num1); printf("The data is: %d \n",num2); } 11 33000 = 32768+32 -32768+32 = -32536
12
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Declare variables Examples int a; char boy = 'u'; short int num2= 32; intnum4, num1 = 0, num3; char ch1 = 'z', ch2, ch3 = '5'; float x = 1.456, y; 12
13
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Use the place holder to specify type and location of displayed data 13 %d (integer)
14
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14
15
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Arithmetics + Add6+4 = 7 -Subtract6-4 = 2 *Multiply6*4 = 24 /divide integer6/4 = 1 float6/4=1.5 %mod (modulo reduction)6%4 = 2 15
16
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. A simple sum program #include void main() {int num1=10,num2=12,num3; char ch1='+'; num3 = num1 + num2; printf(" the sum of 10 + 12 is: %d \n",num3); printf("Again the sum of %d + %d is: %d \n",num1,num2,num3); printf("Still,the sum of %d %c %d is: %d \n",num1,ch1,num2,num3); } 16
17
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Mathematical Precedence Rule 17 1() [] ->. :: Grouping, scope, array/member access 2 ! ~ - + * & ++x --x (most) unary operations, sizeof and type casts 3* / % Multiplication, division, modulo modulo 4+ -Addition and subtraction 5 >Bitwise shift left and right 6 >=Comparisons: less-than,... 7== != Comparisons: equal and not equal 8&Bitwise AND 9^Bitwise exclusive OR 10|Bitwise inclusive (normal) OR 11&&Logical AND 12||Logical OR 13 ?: Conditional expression Conditional expression (ternary operator)ternary operator 14 = += - = *= /= %= &= |= ^= >= Assignment operators 15,Comma operator
18
Example
19
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Shorthand notation 19
20
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Comparison 20 x == y i > 10 a + b != c
21
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Comparison 21
23
Enter two integers, and I will tell you the relationships they satisfy: 3 7 3 is not equal to 7 3 is less than 7 3 is less than or equal to 7
24
Enter two integers, and I will tell you the relationships they satisfy: 22 12 22 is not equal to 12 22 is greater than 12 22 is greater than or equal to 12 Enter two integers, and I will tell you the relationships they satisfy: 7 7 7 is equal to 7 7 is less than or equal to 7 7 is greater than or equal to 7
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.