IT101 IT Fundamentals 2024-2025 PDF
Document Details
Uploaded by PoshMoldavite9169
Assiut University
2025
Dr. Islam Taj-Eddin, Dr. Ali Hussein Ahmed, Dr. Tarik Mohamed Abdel-Kader Ibrahim, Dr. Ebram Kamal William Aziz
Tags
Summary
This document is lecture notes for an IT101 course on IT fundamentals offered by Assiut University for 2024-2025. It introduces the basics of C programming, covers concepts like data types, variables, and control statements.
Full Transcript
IT101 IT FUNDAMENTALS Dr. Islam Taj-Eddin Dr. Ali Hussein Ahmed Dr. Tarik Mohamed Abdel-Kader Ibrahim Dr. Ebram Kamal William Aziz IT Department, Faculty of Computers and Information Assiut University 2024-2025 Enroll The course on Thinqi LMS https://aun.edu....
IT101 IT FUNDAMENTALS Dr. Islam Taj-Eddin Dr. Ali Hussein Ahmed Dr. Tarik Mohamed Abdel-Kader Ibrahim Dr. Ebram Kamal William Aziz IT Department, Faculty of Computers and Information Assiut University 2024-2025 Enroll The course on Thinqi LMS https://aun.edu.eg/thinqi Code=IT101-1 LECTURE 7&8 Programming 3 What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system. 4 Why Learn C? It is one of the most popular programming language in the world If you know C, you will have no problem learning other popular programming languages such as Java, Python, C++, C#, etc, as the syntax is similar C is very fast (compiled programming language), compared to other programming languages, like Java and Python 5 Difference between C and C++ C++ was developed as an extension of C, and both languages have almost the same syntax The main difference between C and C++ is that C++ support classes and objects, while C does not. 6 C Programming Language What is C? C is a structured, relatively low-level, portable programming language. Why study C? Many popular software tools are written in C. Has strongly influenced many other languages. java, C++, Perl, etc. Forces the user to understand fundamental aspects of programming. Very concise language. C, cont. Is C object-oriented? No. C++ (its successor) is. Can a non OO language be useful? Yes. Is C a hard language to learn? No, but it does take a little getting used to. What is the ANSI part? American national standards institute – uniform standard definition of C for portability. C Install IDE An IDE (Integrated Development Environment) is used to edit AND compile the code. Popular IDE's include Code::Blocks, Eclipse, and Visual Studio. These are all free, and they can be used to both edit and debug C code. Note: Web-based IDE's can work as well, but functionality is limited. We will use Code::Blocks in our tutorial, which we believe is a good place to start. You can find the latest version of Codeblocks at http://www.codeblocks.org/. Download the mingw-setup.exe file, which will install the text editor with a compiler. Online c compiler https://www.programiz.com/c-programming/online-compiler/ 9 C Data Types 10 What do program instructions look like? A simple program has at least these three main parts variable declaration variable initialization main body 11 Variables in Programming Represent storage units in a program Used to store/retrieve data over life of program Type of variable determines what can be placed in the storage unit Assignment – process of placing a particular value in a variable Variables must be declared before they are assigned The value of a variable can change; A constant always has the same value 12 Naming variables When a variable is declared it is given a name Good programming practices Choose a name that reflects the role of the variable in a program, e.g. Good: customer_name, ss_number; Bad : cn, ss; Don’t be afraid to have long names if it aids in readability Restrictions Name must begin with a letter; otherwise, can contain digits or any other characters. C is CASE SENSITIVE! Use 31 or fewer characters to aid in portability 13 Variable Declaration All variables must be declared in a C program before the first executable statement! Examples: main(){ int a, b, c; float d; } 14 C Variable Names Variable names in C may only consist of letters, digits, and underscores and may not begin with a digit Variable names in C are case sensitive ANSI standard requires only 31 or fewer characters. Enhances portability to follow this rule Should be very descriptive 15 Variable assignment After variables are declared, they must (should) be given values. This is called assignment and it is done with the ‘=‘ operator. Examples: float a, b; int c; b = 2.12; c = 200; 16 Basic C variable types There are four basic data types in C: char A single byte capable of holding one character in the local character set. int An integer of unspecified size float Single-precision floating point double Double-precision floating point 17 char variable type Represents a single byte (8 bits) of storage Can be signed or unsigned Internally char is just a number Numerical value is associated with character via a character set. ASCII character set used in ANSI C Question: what is the difference between: printf(“%c”, someChar); printf(“%d”, someChar); 18 int variable type Represents a signed integer of typically 4 bytes (32 bits) 19 Char vs. int Consider the following declarations: int j = 4; char k = 4; In memory, these appear as: j 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 k 0 0 0 0 0 1 0 0 They are both perfectly valid ways to represent the number 4. In one case (int), there is much more "wasted" memory. In the other case (char), there is a much stricter limit on how large the number can be if you choose to change it. 20 Char, cont. Why would you not always use char to represent a small number, such as 4? Consider what happens in this case: char j = 4; j = j + 300; Int c = 'e'; #include main(){ char c; int j; j = 100; c = 100; printf("%d %d\n", j, c); printf("%c %c\n", j, c); j = 'h'; c = 'h'; printf("%c %c\n", j, c); printf("%d %d\n", j,c); } 25 float and double variable types Represent typically 32 and/or 64 bit real numbers 26 Additional variable types Note that other types can be constructed using the modifiers: short, long, signed, unsigned The precise sizes of these types is machine-specific We will not worry about them for the time being 27 Declaring variables All variables must always be declared before the first executable instruction in a C program Variable declarations are always: var_type var_name; int age; float annual_salary; double weight, height; In most cases, variables have no meaningful value at this stage. Memory is set aside for them, but they are not meaningful until assigned. 28 Assigning values to Variables Either when they are declared, or at any subsequent time, variables are assigned values using the “=“ operator. Examples int age = 52; //joint declaration/assignment double salary; salary = 150000.23; age = 53; //value may change at any time 29 Assignment, cont. Be careful to assign proper type – contract between declaration and assignments must be honored int x=2.13 double x = 3; char c = 300; 30 Type conversions in expressions Implicit type conversion C permits mixing of constants and variables of different types in an expression. This automatic type conversion is know as implicit type conversion. If the operands are of different types the lower type is automatically converted to the higher type before the operation proceeds. 31 Order of Data Types long double Highest double float long int short char Lowest 32 33 Explicit Conversion Many times there may arise a situation where we want to force a type conversion in a way that is different from automatic conversion. Consider for example the calculation of number of female and male students in a class Ratio = female_students /male_students Ratio = (float) female_students / male_students 34 Structure of a C program So far our C programs are as follows: #include int main(){ return 0; } Let’s learn more about the structure of “program body” 35 Program Body - declarations Always begins with all variable declarations. Some examples: int a, b, c; int d, e; int f; int g = 1, h, k=3; double pi = 3.1415926; 36 Statements Note: all statements end with a semicolon! Statements can (with a few exceptions) be broken across lines or ganged on a single line Commas separate multiple declarations Blank lines have no effect Extra spaces between tokens has no effect. Comments are ignored by the compiler 37 Program Body – Executable Statements Executable statements always follow variable declarations/initializations Executable statements include any valid C code that is not a declaration, ie valid C code to do things like: “multiply the value of a by 10 and store the result in b” “add 1 to the value of j and test whether it is greater than the value of k” “store 5.2 in the variable x” (ie assignment) “print the value of x,y, and z, each on a separate line” 38 The printf Executable Statement The only executable statements we’ve seen to this point are Assignments The printf and scanf functions Assignment expressions with simple operators (+, -) Very hard to write any program without being able to print output. Must look at printf in more detail to start writing useful code. 39 printf(), cont. Sends output to standard out, which for now we can think of as the terminal screen. General form printf(format descriptor, var1, var2, …); format descriptor is composed of Ordinary characters copied directly to output Conversion specification Causes conversion and printing of next argument to printf Each conversion specification begins with % 40 More on format statements The format descriptor/specifier in its simplest form is one of: %s sequence of characters known as a String Not a fundamental datatype in C (really an array of char) %d Decimal integer (i.e. base ten) %f Floating point Note that there are many other options. These are the most common, though, and are more than enough to get started. 41 Invisible characters Some special characters are not visible directly in the output stream. These all begin with an escape character (ie \); \n newline \t horizontal tab \a alert bell 42 Printf() examples Easiest to start with some examples printf(“%s\n”, “hello world”); print hello world as a string followed by a newline character printf(“%d\t%d\n”, j, k); print the value of the variable j as an integer followed by a tab followed by the value of the variable k as an integer followed by a new line. printf(“%f : %f : %f\n”, x, y, z); print the value of the floating point variable x, followed by a space, then a colon, then a space, etc. 43 Reading keyboard input To be useful, program must be able to read data from external source, e.g. User input from keyboard Database File Socket In next slide we cover the scanf library function. It is like printf but reads user-typed input rather than prints it. 44 Scanf function In , so no new #include(‘s) Basic syntax scanf( format-specifier, &var1, &var2, etc.); Format-specifier is identical to printf We do not need to understand everything here, just enough to do some basic I/O Examples int a; scanf(“%d”,&a); double x; scanf(“%f”,&x); Blocks program until user enters input! 45 Another technique for passing data from the keyboard main() can also be written as main(int argc, char *argv[]) If main is written in this way, information can be passed directly from the keyboard to the program at the time of execution For example, we may run a program called test1.exe as: PROMPT > test1.exe Ali Saleh When test1.exe is run the two tokens Ali and Saleh are passed into the program and can be obtained by querying argv and argc Note: this involves some concepts that go beyond what we have learned so far. We will understand fully later. 46 Passing data, cont. When this technique is used, each token is stored as a separate element in the array argv The first token passed to the program is stored in argv, the second token in argv, etc. argc stores the (integer) number of tokens passed in A simple example will clarify this 47 argc/argv example int main (int argc, char* argv[]){ printf(“%s %d %s \n”, “you entered”, argc, “arguments”); printf(“%s: %s\n”, “the zeroth arg is the program name”, argv); printf(“%s: %s\n”, “the first argument is”, argv); printf(“%s: %s\n”, “the second argument is, argv); } > argv_example hello world you entered 3 arguments the zeroth argument is the program name: argv_example the first argument is hello the second argument is world 48 argc/argv cont. Note that to do this completely generally we would need to use a while or for loop We will study while loops shortly Also note that argv reads all arguments as Strings (i.e. sequences of characters). So, we cannot simply pass two number and add them, for example. First, we would have to convert to a numeric type. 49 Converting String to integer An important function when using argv/argc is atoi. atoi converts a String argument to an integer in the following way: int input, output; input = atoi(argv); output = input + 1; printf("%d\n", output); > test2 333 334 50 Reading single characters Another important pair of functions for keyboard input/output is getchar/putchar getchar reads a single character from the input stream; putchar write a single character to the standard out for example: int c; c = getchar(); putchar(c); 51 While loops A simple technique for repeating a statement or group of statements until some specified condition is met General form: while (expr){ statement1; statement2;.. } If expr evaluates to true (i.e. not 0), then perform statement1, etc. Otherwise, skip to end of while block. Repeat until expr evaluates to false (i.e. 0). 52 While example #include int main(int argc, char* argv[]){ int counter; counter = 1; while (counter < argc){ printf("%s %d: %s\n", "argument number", counter, argv[counter]); counter = counter + 1; } return 0; } 53 If example int main(int argc, char *argv[]){ int counter = 1; if (argc < 2){ printf("%s\n", "error; must enter at least one argument!"); exit(1); } while (counter < argc){ printf("%s %d: %s\n", "argument number", counter, argv[counter]); counter = counter + 1; } } 54 Getchar/putchar example #include int main(){ int c; c = getchar(); while (1){ putchar(c); c = getchar(); } } 55 More control statements for loops Much like while loops but more general and sometimes more convenient switch statements Nice alternative to messy if-else sequences do-while loops Slight variant on while loops goto statements Typically to be avoided break and continue statements 56 Control statements, cont. Main points These additional statements don't allow you to do anything that you couldn't have done with while and if However, they do make it much cleaner and more concise to express certain logic. Often so much so that without them a solution may be too difficult to come up with. 57 For loops General syntax for ( expr1; expr2; expr3 ){ statement1; ……… } This is exactly equivalent to: expr1; while (expr2) { statement1; expr3; } 58 For loops, cont. Expr1 Called the initializer; evaluated once and only once before the loop executes for the first time. Expr2 The termination condition; evaluated at the beginning of each iteration of the loop. If it evaluates to false, loop ends. Expr3 Evaluated at the end of each iteration of the loop. 59 For loops, cont. Simple, most obvious, and most typical usage: for ( j = 0; j < n; j++ ) {…} (write out the equivalent while statement!) Infinite loop: for (; ; ) { … } May also combine expressions: for (j=0, k=m; j < k; j++, k--){ … } 60 For loops, cont Arithmetic expressions Initialization, loop-continuation, and increment can contain arithmetic expressions. If x equals 2 and y equals 10 for ( j = x; j