2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

C++ Basics Variables, Identifiers, Assignments, Input/Output.
Chapter 3: Beginning Problem Solving Concepts for the Computer Programming Computer Programming Skills /1436 Department of Computer Science.
Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University.
Structure of a C program
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Introduction Computer program: an ordered sequence of instructions whose objective is to accomplish a task. Programming: process of planning and creating.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Chapter 2: C Fundamentals Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 1 Introducing C.
Gator Engineering 1 Chapter 2 C Fundamentals Copyright © 2008 W. W. Norton & Company. All rights reserved.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
Chapter 2: Java Fundamentals
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Chapter 2: C Fundamentals Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 2 C Fundamentals.
1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.Semantic © In this session we will.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
Gator Engineering 1 Chapter 2 C Fundamentals (Continued) Copyright © 2008 W. W. Norton & Company. All rights reserved.
Chapter 2: C Fundamentals Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 2 C Fundamentals.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
CMSC 1041 Variables in C Declaring, Naming, Using Variables.
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.
1 Chapter 2 C Fundamentals. Computer Architecture Central Processing Unit CPU Main memory Data bus Address.
COP 3275: Chapter 02 Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA.
Numbers in ‘C’ Two general categories: Integers Floats
C++ Lesson 1.
Asst.Prof.Dr. Tayfun ÖZGÜR
C Fundamentals Chapter 2 Copyright © 2008 W. W. Norton & Company.
Variables, Identifiers, Assignments, Input/Output
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
CSE 220 – C Programming C Fundamentals.
LESSON 3 IO, Variables and Operators
CMSC 104, Section 4 Richard Chang
Introduction to C Programming
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CMSC 104, Section 4 Richard Chang
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.
פרטים נוספים בסילבוס של הקורס
Variables in C Topics Naming Variables Declaring Variables
פרטים נוספים בסילבוס של הקורס
Introduction to C Programming
Govt. Polytechnic,Dhangar
Variables in C Topics Naming Variables Declaring Variables
UMBC CMSC 104 – Section 01, Fall 2016
Variables in C Declaring , Naming, and Using Variables.
C Fundamentals Chapter 2 Copyright © 2008 W. W. Norton & Company.
WEEK-2.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Language C Language.
Variables in C Topics Naming Variables Declaring Variables
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
C Language B. DHIVYA 17PCA140 II MCA.
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

2. C FUNDAMENTALS

Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or not to C: "); printf("that is the question.\n"); return 0; }

Comments Begin with /* and end with */. /* This is a comment */ May extend over more than one line. /* This comment starts on one line, but ends on another. */ Warning: Failing to close a comment will cause the compiler to ignore part of your program. printf("Hello"); /* forgot to close this comment... … printf("Goodbye"); /* so it ends here */ In C99, comments can also be written this way: // This is a comment. This type of comment is terminated by the end of the line.

Example: Computing the Volume /* Illustrates the use of variables, expressions, and assignment statements */ #include int main(void) { int height, length, width, volume; height = 5; length = 20; width = 10; volume = height * length * width; printf("The height is %d\n", height); printf("The length is %d\n", length); printf("The width is %d\n", width); printf("The volume is %d\n", volume); return 0; }

Variables Variables must be declared before they are used. Variables can be declared one at a time: int height; int length; int width; int volume; or several can be declared at the same time: int height, length, width, volume;

Variables Always initialize variables before using them. Wrong: int height; printf("The height is %d\n", height); Right: int height; height = 5; printf("The height is %d\n", height); An variable can be initialized in its declaration: int height = 5; Variable names must be legal identifiers.

Identifiers Identifiers may contain letters, digits, and underscores. An identifier must begin with a letter or underscore. Examples of legal identifiers: times10 get_next_char _done It’s usually best to avoid identifiers that begin with an underscore.

Identifiers Examples of illegal identifiers: 10times get-next-char C is case-sensitive: it distinguishes between upper- and lower-case letters in identifiers. For example, the following identifiers are all different: eof eoF eOf eOF Eof EoF EOf EOF

Keywords auto break case char const continue default do double else enum extern float for goto if inline* int long register restrict* return short signed sizeof static struct switch typedef union unsigned void volatile while _Bool* _Complex* _Imaginary* *C99 only The following keywords may not be used as identifiers:

Keywords Keywords (with the exception of _Bool, _Complex, and _Imaginary ) and names of library functions (e.g., printf ) must be written using only lowercase letters.

The General Form of a Simple Program The simplest C programs have the following form: int main(void) { declarations statements } The word int may be omitted (although it’s good practice to include it). The use of void is also optional. Each declaration and each statement (except for compound statements) must end with a semicolon.

The General Form of a Simple Program C99 does not require that declarations precede statements. However, each variable must be declared before it is used for the first time

Program Layout C programs are—for the most part—“free-format.” Spaces and newline characters can be deleted, except where this would cause two symbols to merge into one: #include int main(void){int height,length,width,volume;height=5; length=20;width=10;volume=height*length*width; printf("The height is %d\n",height); printf("The length is %d\n",length); printf("The width is %d\n",width); printf("The volume is %d\n",volume);return 0;}

Program Layout Extra spaces and newline characters can be inserted, except where this would divide a symbol: #include int main ( void ) { int height, length, width, volume ; height = 5 ; length = 20 ; width = 10 ; volume = height * length * width ; printf ( "The height is %d\n", height ) ; printf ( "The length is %d\n", length ) ; printf ( "The width is %d\n", width ) ; printf ( "The volume is %d\n", volume ) ; return 0; } Note: Each command that begins with # must be on a separate line.

Example: Converting Fahrenheit to Celsius /* Illustrates macro definition, floating-point numbers, and scanf */ #include #define FREEZING_PT 32.0 #define SCALE_FACTOR (5.0 / 9.0) int main(void) { float fahrenheit, celsius; printf("Enter Fahrenheit temperature: "); scanf("%f", &fahrenheit); celsius = SCALE_FACTOR * (fahrenheit - FREEZING_PT); printf("\nCelsius equivalent is: %.1f\n", celsius); return 0; }

Defining Constants A macro definition has the form #define identifier replacement-list Macros often represent constants: values that won’t change during program execution: #define N 100 Macro definitions are handled by the C preprocessor. Wherever the identifier appears later in the source file, the preprocessor replaces it by the replacement list.

Defining Constants If the replacement list contains operators, it is best to enclose it in parentheses. #define EOF (-1) #define TWO_PI (2* ) By convention, names of C constants contain only upper-case letters. Advantages of defining constants: Programs are easier to read. Programs are easier to modify. Helps reduce errors of inconsistent use, as well as typographical errors.

In Class Examples Print to the screen Print n, n2 and n3 for a value n entered by the user. Calculate and print the surface area and volume for a box with dimensions entered by the user Calculate and print net and gross pay