Lexical Elements, Operators, and the C Cystem

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.
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.
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.
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
Introduction to C Language
Computer Science 210 Computer Organization Introduction to C.
COMPUTER SCIENCE, KOREA UNIVERSITY Chapter2 Lexical Elements, Operators,and the C System Rim, Hae-Chang Department of Computer Science and Engineering.
UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 2.
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.
#include int main(void) { printf("Hello, world!\n"); return 0; } entry point called on program start only one main( ) in any program # for preprocessor.
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
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.
Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
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.
Lecture 2. Outline Sample programming question Sample C program Identifiers and reserve words Program comments Preprocessor directives Data types and.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
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.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Arithmetic Expressions
CSCE 206 Structured Programming in C
The Machine Model Memory
Computer Science 210 Computer Organization
Computer Programming Techniques Semester 1, 1998
Tokens in C Keywords Identifiers Constants
Input/output.
CS1010 Discussion Group 11 Week 4 – Overview of C programming.
Learning C Language.
C Short Overview Lembit Jürimägi.
Lecture2.
Data Type.
Visit for more Learning Resources
Computer Science 210 Computer Organization
#include <stdio.h> int main(void) { printf("Hello, world!\n");
Data Type.
Chapter 11 Introduction to Programming in C
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
Your questions from last session
Chapter 11 Programming in C
Programming Languages and Paradigms
Session 1 – Introduction to Computer and Algorithm (Part 2)‏
Lexical Elements & Operators
An Overview of C.
DATA TYPES There are four basic data types associated with variables:
Course Outcomes of Programming In C (PIC) (17212, C203):
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 pointers files 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 #include <stdio.h> int main(void) { int i = 1, sum = 0; while (i <= 5) { sum += i; ++i; } printf("sum = %d\n", sum); return 0; 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 C compiler a.out

Compiler Task generate an executable binary file from a source file check if all the words are correct (lexical analyzer) check if the grammar is correct (syntax analyzer) optimization code generation

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

Constants integer floating character ‘a’, ‘7’ ‘+’, ‘\n’ 17 is a decimal 023 is an octal  023 is 238 (which is 19 in base 10). 0x23 is a hexadecimal  0x23 is 2316 (which is 35 in base 10) floating 1.0 3.14 character ‘a’, ‘7’ ‘+’, ‘\n’

String Constant “any thing you can put here” special character 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 ( ) [ ] -> . ++ -- (postfix) (L2R) ! ~ + - & (type) sizeof ++ -- (prefix) (R2L) * / % (L2R) + - (L2R) << >> (L2R) < <= > >= (L2R) == != (L2R) & ^ | (L2R) && || (L2R) k = (n > 0) ? a : b; = += -= *= /= %= &= ^= |= <<= >>= (R2L) bitwise operators: & | ^ ~ << >>

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

Increment and Decrement i++ and ++i are different if (++I > 10) ….. if (i++ > 10) …. 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 <stdio.h> 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 <stdio.h> #include <stdlib.h> 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; RAND_MAX is at least 32767, in GNU 2,147,483,647 ( the largest with 32 bits) you cannot change it. randoms below 50: rand() % 50

gcc compiler gcc OR g++ are popular they call C++ is a superset of C preprocessor compiler assembler linker The GNU Project is a free software, mass collaboration project, announced on September 27, 1983, by Richard Stallman at MIT - wikipedia

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” ......