introduction to computer systems hello world and gcc
play

Introduction to Computer Systems Hello World and gcc Eric McCreath - PowerPoint PPT Presentation

Introduction to Computer Systems Hello World and gcc Eric McCreath Hello World The classic first program: #include<stdio.h> void main() { printf("Hello World\n"); } Or #include<stdio.h> int main() {


  1. Introduction to Computer Systems Hello World and gcc Eric McCreath

  2. Hello World The classic first program: #include<stdio.h> void main() { printf("Hello World\n"); } Or #include<stdio.h> int main() { printf("Hello World\n"); return 0; } to compile and run: % gcc -Wall -o hello hello.c % ./hello 2

  3. Moving Back to C C gives you good control of the underlying hardware while being high level enough for you to handle a large code base. If you are an OO programmer then it will take some acclimatization. Operators don't operate on objects but on basic data types. You don't have a set of objects where classes give you a clear way of understanding these objects, rather you have lots of memory that you understand however you wish. logic view on memory 3

  4. Compiler and man pages Use '-Wall'. This asks the GNU C compiler for more verbose warning messages. Do this with all your programs. gcc -Wall -o myprog myprog.c Man pages are very useful in working out what system calls or library calls do. Sometimes there are multiple man pages with the same name, use "man -a" to list them all. Or use the volume number if your know it (man pages are organized in volumes of related pages). If you don't know the name of a call then use -k to help you. 4

  5. ltrace and strace 'ltrace' and 'strace' are your friends. 'ltrace' lists the library calls made by a running process as it is running. Whereas, 'strace' list the system calls. If you have a program that does something similar to what you wish to get working then you can use 'ltrace' or 'strace' to see how that program does it. If your program dies at some point then 'ltrace' or 'strace' are often quick ways of inspecting at what point the program died. 5

  6. Simple IO 'printf' can be used to output textual data to standard out. 'scanf' can be used to obtain and parse textual data from standard in. #include<stdio.h> #include<stdlib.h> #define NAMELEN 100 int main() { char name[NAMELEN]; if (scanf("%s",name) > 0) { printf("Hello %s\n",name); } else { printf("error scanning name.\n"); } return 0; } 6

  7. Integer An integer is a basic data type in C. It is denoted 'int'. An 'int' stores positive and negative integer values within a fixed number of bits. However, it is also used to store booleans and flags. The code below sums the two numbers you give it. #include<stdio.h> #include<stdlib.h> int main() { int value1, value2, sum; scanf("%d %d",&value1, &value2); sum = value1 + value2; printf("%d\n",sum); return 0; } 7

  8. while loop In programming often you wish to repeat the same code a number of times. The 'while' loop provides a simple way of doing this. The code within the 'while' loop repeats when the condition evaluates to true. The condition is evaluated at the very beginning and then re-evaluated before the code within the while loop is repeated. The code given below prints the numbers 1 to 10. #include<stdio.h> int main() { int i; i = 1; while (i <= 10) { printf("%d\n",i); i = i + 1; } return 0; } 8

  9. Exersises Get the "hello world" program working. Write a program that prints the even numbers between 2 and 20. Write a program that sums a list of positive numbers you give it (you can use -1 to indicate the end of this list). 9

Recommend


More recommend