C Language Brief history In 1972, Dennis Ritchie designed C and it was used on PDP-11 mini- computers In 1974, Unix was rewritten in C C++ and C Advantages.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

C Language.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
C Intro.
C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Kernighan/Ritchie: Kelley/Pohl:
Chapter 3: Beginning Problem Solving Concepts for the Computer Programming Computer Programming Skills /1436 Department of Computer Science.
Structure of a C program
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Computer Science 210 Computer Organization Introduction to C.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
Programming With C.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to Programming
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Introduction to C Programming I Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
C++ Lesson 1.
CSCE 206 Structured Programming in C
Computer Science 210 Computer Organization
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
A bit of C programming Lecture 3 Uli Raich.
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to C Topics Compilation Using the gcc Compiler
C Basics.
Introduction to C Programming Language
Introduction to C Programming
Computer Science 210 Computer Organization
CMSC 104, Section 4 Richard Chang
פרטים נוספים בסילבוס של הקורס
Variables in C Topics Naming Variables Declaring Variables
פרטים נוספים בסילבוס של הקורס
Introduction to C Programming
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
Variables in C Topics Naming Variables Declaring Variables
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Variables in C Declaring , Naming, and Using Variables.
C programming Language
Introduction to C Topics Compilation Using the gcc Compiler
Programming Language C Language.
Variables in C Topics Naming Variables Declaring Variables
C Language B. DHIVYA 17PCA140 II MCA.
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
INTRODUCTION TO C.
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

C Language Brief history In 1972, Dennis Ritchie designed C and it was used on PDP-11 mini- computers In 1974, Unix was rewritten in C C++ and C Advantages and disadvantages

A short example #include main ( ) { printf (“Is anybody out there?\n”); printf (“Hello world!\n”); }

/* Another example-read items, compute their average */ #include main ( ) { int ItemsRead = 0; double ThisItem, Sum = 0.0; printf (“Enter as many items as you want\n”); printf (“Terminate with non-double or EOF marker\n”); while (scanf (“%lf”, &ThisItem ) ==1 ) { ItemsRead ++; Sum += ThisItem; } printf( “The average of %d items was %f\n”, ItemsRead, Sum / ItemsRead ); return 0; }

Simple C 1. Create a c program Any line or screen editor will do. Vi editor is also a good choice. For simple ones, one can use cat > fileName as well. 2. Compile the program cc fileName.c will create a executable file called a.out regardless of the fileName.

Compile with a style To create a complied file with the same name: Use cc fileName.c –o fileName Will create a file with the fileName instead of a.out

Compiling Problems It is not as straightforward as the Code Warrior you did in C++ course. However, you will get used to it once you do more. Unix debugger dbx is available for debugging.

Memory leak in C Memory leaks (in which malloc() memory is never released with corresponding free() calls) and buffer overruns (writing past memory that has been allocated for an array, for example) are some of the common problems and can be difficult to detect.

Example #include #include "memwatch.h" int main(void) { char *ptr1; char *ptr2; ptr1 = malloc(512); ptr2 = malloc(512); ptr2 = ptr1; free(ptr2); free(ptr1); }

What is the problem with that program? The code in Listing 1 allocates two 512- byte blocks of memory, and then the pointer to the first block is set to the second block. As a result, the address of the second block is lost, and there is a memory leak.

Debug Tools There are various ways to track down user- space and kernel problems using debug tools on Linux. Build and debug your source code with these tools and techniques: Memory tools: MEMWATCH and YAMD strace GNU debugger (gdb) Magic key sequence

Compiling Process 1. cc command translate the source file to an equivalent assembly program. 2. Once an equivalent assembly language is generated, another program called assembler is called to convert the program into binary file or machine code file. 3. Finally, linker is called to combine the binary file with the library to create a executable file.

cc versus gcc gcc – the GNU C Compiler, it was written by Richard Stallman and it is now maintained by Cyguns solutions. gcc exists for practically all major operating systems and processor architectures and is widely used in embedded systems. 1. It is the complier we are using in Linux. 2. When you invoke cc, you are invoking gcc

gcc interpretation of filename Suffixes.c --- C source code which must be pre- processed before compilation..i --- C source code has been pre-processed..cc,.cxx,.cpp,.C --- C++ source file as in.c.ii --- C++ source that has been pre-proce….h --- C header file.s --- Assembly code.S --- Assembly code which must be pre-processed

Compiling C++ Programs To compile and link C++ programs, you should invoke the complier as g++ rather than gcc. This is especially important at link time because it force the linker to link in certain libraries required by C++ programs.

Basic Rules in C The basic unit of a C program is a character. A-Z, a-z, 0-9, white space: blanks, newlines, tabs, … and the following 29 characters: ! # % ^ & * ( ) _ - + = [ ] { } | \ ; : “ ‘,. / ? ~ White space is important for readability.

Comments in C 1. C comments begins with a /* and end with */ and there should be no space between the asterisk and the slash. 2. No nest in comment in C. Common error: /* 1 */ /* This comment ends early on the next line. /* 2 */ * This is not in the comment ! /* 3 */ * Nether is this;

Identifiers and Keywords in C 1. A name may use A-Z, a-z, 0-9, _ Begin with a digit is prohibited. 2. The length cannot exceed 31 chars and in some situations 6 is the limit. 3. Similar to the UNIX, c is case sensitive. 4. Avoid using the 32 keywords. They are:

Keywords in C auto, break, case, char, const, continue, default, do, double, else, enum, extern float, for, goto, if, int, long, register, return short, signed, sizeof, static, struct, switch typedef, union, unsigned, void, volatile, while

Pre-Processor in C #include #define Minimum Wage 5.75 #---the first non-white-space character line begins with # will be treated as pre-processor inside indicates the *** file doe not reside in the current directory, (it resides in the system directory).h --- h refer to the header file similar to C++

Pointers in C Pointer is a variable to store an address of another object. That object is accessed by dereferencing the Pointer. Pointer is one of the most important concept in C language

How to define a pointer? int *p; int x = 8; int *p = &x; /* p points at x */ ++*p will change x to 9 while *P++ will make the pointer points to a different number in memory.

Pointer in C You can give a pointer any name you like, such as: int *p, int *ptr, int *Ptr, int *xPointer However, we recommend use *xptr or xPtr to point x and *yPtr to point y. * --- is called indirection operator or dereferencing operator.

Initializing Pointer Pointer should be initialized either when they are declared or in an assignment statement. int *xPtr, *yPtr, *zeroPtr; int x = 8, y = 9, zero = 0; xPtr = &x; yPtr = & y; zeroPtr = NULL or zeroPtr = 0; NULL is a symbolic constant defined in the header file (and in several other header files)

p address p 8 The computer will reserve a address for p. However, we don’t care where it will store it. What we care is p points to the variable by store the address in it. ++*p will make 8 becomes 9 and *p++ will make p points to another address

Program Example #include Void Swap (int * const X, int * const Y) { int Temp; Temp = *X; *X = *Y; *Y = Temp; } main (void) { int A = 5, B = 7; Swap (&A, &B); printf (“%/d %/d\n”, A, B); /* Must pass the address */ return 0; }

Input from c Programs scanf #include main (void) { int x; double y; printf (“Enter an integer and a real: “); scanf ( “%d %lf”, &x, &y); Print (“You entered %d and %f\n”, x, y); }

Example #include int main ( ) { printf(“\”So what? \“ said she.\n”); return 0; }

Input in c #include int main( void ) { int intvar; printf("The address of intvar is %p.\n", &intvar); /* Notes 1 and 2 */ printf("\nEnter an integer value: "); scanf("%d", &intvar); /* Note 3 */ printf("The value you entered was %d.\n", intvar); return 0; }

Notice the problem when you run the program 1. When you input a legal integer. 2. When you input a serious of characters. 3. When you input a number that is too big to the computer.