Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Introduction to C Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Computer Programming w/ Eng. Applications
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.
Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2.
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
1 CSSE 332 Preprocessor, Array and Strings. 2 External library files libname.a or libname.so Special functionality is provided in the form of external.
C programming.
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.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Guide To UNIX Using Linux Third Edition
Basic Input/Output and Variables Ethan Cerami New York
1 Lecture 3  Lexical elements  Some operators:  /, %, =, +=, ++, --  precedence and associativity  #define  Readings: Chapter 2 Section 1 to 10.
1 Structured Programming in C Welcome to CPSC 206.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C PROGRAMMING LECTURE 17 th August IIT Kanpur C Course, Programming club, Fall by Deepak Majeti M-Tech CSE
Computer Science 210 Computer Organization Introduction to C.
CS 11 C track: lecture 1 Preliminaries Need a CS cluster account cgi-bin/sysadmin/account_request.cgi Need to know UNIX ITS.
COMPUTER SCIENCE, KOREA UNIVERSITY Chapter2 Lexical Elements, Operators,and the C System Rim, Hae-Chang Department of Computer Science and Engineering.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
Functions Why we use functions C library functions Creating our own functions.
#include int main(void) { printf("Hello, world!\n"); return 0; } entry point called on program start only one main( ) in any program # for preprocessor.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Lecture 1 cis208 January 14 rd, Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
Khalid Rasheed Shaikh Computer Programming Theory 1.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Minimal standard C program int main(void) { return 0 ; }
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
CSc 352: Basic C Programming Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
1 Lexical Elements, Operators, and the C System. 2 Outline Characters and Lexical Elements Syntax Rules Comments Keywords Identifiers Constants String.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Unary, Binary, logical Operations, Explicit type conversion Lecture 6 Instructor: Haya Sammaneh.
1 C Basics. 2 The C Language Spirit Made by professional programmers for professional programmers Very flexible, very efficient, very liberal Does not.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include Standard header file (.h) Library.
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.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
CSCE 206 Structured Programming in C
Computer Science 210 Computer Organization
Computer Programming Techniques Semester 1, 1998
C Short Overview Lembit Jürimägi.
Lecture2.
Computer Science 210 Computer Organization
Lexical Elements, Operators, and the C Cystem
توابع ورودي-خروجي.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Lexical Elements, Operators, and the C Cystem
A First Book of ANSI C Fourth Edition
Assignment Operators Topics Increment and Decrement Operators
#include <stdio.h> int main(void) { printf("Hello, world!\n");
Assignment Operators Topics Increment and Decrement Operators
Introduction to C Programming
Chapter 11 Programming in C
Lexical Elements & Operators
An Overview of C.
Introduction to C Programming
Assignment Operators Topics Increment and Decrement Operators
Getting Started With Coding
Presentation transcript:

Lexical Elements, Operators, and the C Cystem

C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers char *p, s[100], **twod; files ifp = fopen(“my file”, “r”); float minimum(float x, float y) { if (x < y) return x; else return y; }

Syntax and Compiler The compiler understands some words and a simple grammar words –32 keywords: if, int, return,..... –identifier; i, sum –constants: 1, 0, 5 –string: “sum = %d\n” –operators: =, <=, +=, ++ grammar –keyword identifier ; –while (expression) statement | statements #include int main(void) { int i = 1, sum = 0; while (i <= 5) { sum += i; ++i; } printf("sum = %d\n", sum); return 0; } C compiler a.out

Compiler Task –generate a binary file from a source file 1.check if all the words are correct (lexical analyzer) 2.check if the grammar is correct (syntax analyzer) 3.optimization 4.code generation

Identifiers ::= | | –underscore is a letter in C some identifiers are used in the library –printf, scanf, fopen, sqrt, pow, exp, sin,.. – _print

Constants integer –17 is a decimal –017 is an octal –0x1F is a hexadecimal floating –1.0 –3.14 character ‘a’, ‘7’ ‘+’, ‘\n’

String Constant “any thing you can put here” special chatacter should be preceded by a backslash it’s an array of characters library functions for strings –strcat, strcmp, strcpy, strleng

Operators Although C has very few keywords, it has a large number of operators ( ) [ ] -> ! ~ * & (type) sizeof (right to left) * / % + - > >= == != & ^ | && || k = (n > 0) ? a : b; (right to left) = += -= *= /=(right to left)

Precedence – – 5 /* left to right */ * 3 (1 + 2 ) * 3 j *= k = m + 5/* right to left */ k++--/* left to right */ ++--k/* right to left */

Increment and Decrement i++ and ++i are different Exercises int a=1, b=2, c=3, d=4; a*b/c a*b%c+1 ++a * b - c— 7 - -b * ++d

Strange Assignments a = (b = 2) + (c =3); a = b = c = 0; j *= k + 3; /* j = j*(k+3) OR j = j*k +3 */ j *= k = m + 5;

/* Some powers of 2 are printed. */ #include int main(void) { int i = 0, power = 1; while (++i <= 10) printf("%6d", power *= 2); printf("\n"); return 0; }

C Environments header files –usually contain only function prototype, NOT function code –then, where are the codes? –standard library is linked automatically –you should specify other libraries (math)

#include int main(void) { int i, n; printf("\n%s\n%s", "Some randomly distributed integers will be printed.", "How many do you want to see? "); scanf("%d", &n); for (i = 0; i < n; ++i) { if (i % 10 == 0) putchar('\n'); printf("%7d", rand()); } printf("\n\n"); return 0; }

gcc compiler gcc OR g++ are popular –C++ is a superset of C they call 1.preprocessor 2.compiler 3.assembler 4.linker The GNU Project is a free software, mass collaboration project, announced on September 27, 1983, by Richard Stallman at MIT - wikipediafree softwaremass collaborationRichard StallmanMIT

gcc options perform parts of 4 steps specify output files and format; “gcc xxx.c –o x” -ansi OR specify differences warning options “ –w” debugging options “ –g “ “-ggdb” “-g3 optimization “-O0” “-O2” preprocessor options assembler options linker options “-lm” read