INTRODUCTION TO C.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Structure of a C program
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
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.
© 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.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
CS212: Object Oriented Analysis and Design Lecture 2: Introduction to C++
Dennis Ritchie 1972 AT & T Bell laboratories (American Telephone and Telegraph) USA 1www.gowreeswar.com.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
GUJARAT KNOWLEDGE VILLAGE Faculty Name:- Prof H.M.Patel Students Name:- SONI VISHAL THAKKER BHAVIN ZALA JAYDIP ZALA.
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.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
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.
Numbers in ‘C’ Two general categories: Integers Floats
C++ Lesson 1.
Chapter Topics The Basics of a C++ Program Data Types
Data types Data types Basic types
Chap. 2. Types, Operators, and Expressions
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
LESSON 3 IO, Variables and Operators
Java Primer 1: Types, Classes and Operators
Basic Elements of C++.
Object Oriented Programming
Fundamental of Programming (C)
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
C Short Overview Lembit Jürimägi.
Introduction to C Programming Language
Introduction to C Programming
Basic Elements of C++ Chapter 2.
Visit for more Learning Resources
11/10/2018.
Operators and Expressions
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.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
פרטים נוספים בסילבוס של הקורס
Variables in C Topics Naming Variables Declaring Variables
Basics of ‘C’.
Introduction to C Programming
Govt. Polytechnic,Dhangar
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.
Chapter 2: Introduction to C++.
C programming Language
WEEK-2.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Language C Language.
C++ Programming Basics
Module 2 Variables, Data Types and Arithmetic
Variables in C Topics Naming Variables Declaring Variables
DATA TYPES There are four basic data types associated with variables:
C Language B. DHIVYA 17PCA140 II MCA.
Variables in C Topics Naming Variables Declaring Variables
Course Outcomes of Programming In C (PIC) (17212, C203):
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

INTRODUCTION TO C

Today’s Outline Brief history of C Constants and Variables Keywords Data Types in C Operators Storage Class Functions Conditional statements

Brief history of C DENNIS RITCHIE

Brief history of C C is one of the most widely used programming languages of all time. C has a wide range of flexibility and “You can program ANYTHING in C.”

PROGRAM STRUCTURE A C program basically has the following form: Pre-processor Commands Functions Variables Statements & Expressions Comments #include <stdio.h> int main() { /* My first program */ printf("Hello, World! \n"); return 0; }

CONSTANTS CONSTANTS : They are those data items that RETAIN same values in a program. CONSTANTS ALPHANUMERIC NUMERIC CHARACTER ‘A’ STRING “ABC” INTEGER 1 , -2,0 FLOATING 7.9 , 2.00

VARIABLES THESE ARE DATA ITEMS WHOSE VALUES MAY OR MAY NOT CHANGE DURING THE COURSE OF PROGRAM Variable name should start with an alphabet/underscore and should not contain any blank spaces.

KEYWORDS KEYWORDS : These convey a special meaning to the C compiler and cannot be used as variable names. If for else while

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

DATA TYPES

DATA TYPES

MODIFIERS Modifiers The data types explained above have the following modifiers. short long signed unsigned

Type Bytes Range short int 2 -32,768->+32,767(32kb) unsigned short int 2 0 -> +65,535 (64Kb) unsigned int 4 0 -> +4,294,967,295 ( 4Gb) int 4 -2,147,483,648 -> +2,147,483,647 ( 2Gb) long int 4 -2,147,483,648 -> +2,147,483,647 ( 2Gb) signed char 1 -128 -> +127 unsigned char 1 0 -> +255 float 4 double 8 long double 12

TYPE CAST Type casting is a way to convert a variable from one data type to another data type. For example, if you want to store a long value into a simple integer then you can type cast long to int. You can convert values from one type to another explicitly using the cast operator as follows: (type_name) expression

OPERATORS C Supports a rich set of built in operators. These are : Arithmetic Operators ( + - * / %) Relational Operators (< <= > >= == !=) Logical Operators ( && || !) Assignment Operators (= += -= *= /= %=) Increment And Decrement Operators ( ++ --) Bitwise Operators (& | ^ >> <<) Special Operators( , sizeof()) Ternary Operator (? : )

OPERATORS UNARY OPERATORS These act on single operands.eg: + - ! ~ * BINARY OPERATORS These act on tw0 operands.eg: + - * / % INCREMENT OPERATOR Two + signs together indicate an increment operator. i.e. ++ DECREMENT OPERATOR Two - signs together indicate a decrement operator. i.e. - -

OPERATORS Prefix and Postfix increment and decrement operators x- - ;  postfix-decrement operator - -x ;  prefix decrement operator x++ ;  postfix-increment operator ++x ;  prefix increment operator The prefix increment operator adds one to its operand. This incremented value is used in the expression to get the result of the expression. In the postfix form, the increment or decrement takes place after the value is used in expression evaluation.

Precedence of Operators

EXPRESSIONS Arithmetic operations are evaluated using BODMAS rule and then assigned to a variable on left hand side. C permits mixing of constants and variables of different types in an expression. During evaluation it adheres to strict rules of type conversion. A ‘lower’ type is automatically converted to ‘higher’ type before the operation proceeds. The result is of higher type. This is called Implicit type conversion. C performs type conversion automatically. However there are instances when we force a type conversion in a way that is different from the automatic conversion. This is called explicit type conversion. SYNTAX : (type name)expression

THE PREPROCESSOR A directive is given as a line of source code starting with the # symbol and is processed by the preprocessor before the actual compilation takes place. eg. #include, #define Include directives use <x.h> (brackets) to indicate that the compiler should look in a “standard” place, such as /usr/include/… They use “x.h” (double quotes) to indicate that it should look first in the current directory.

THE PREPROCESSOR Your source code Enhanced and obfuscated source code Object code Preprocessor Compiler

MACROS A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro . There are two kinds of Macros. They differ mostly in what they look like when they are used. Object like macros resemble data objects when used. Function like macros resemble function calls. Object like Macros: SYNTAX:- #define identifier string Example:- #define count 100 Function like Macros: SYNTAX:- #define identifier(f1,f2,….fn) string here f1,f2,…..fn are the arguments of thee macros. Example:- #define CUBE(x) (x*x*x) #define ABS(x) (((x)>0)?(x):(-(x)))

STORAGE CLASS 1> The auto storage class is the default storage class for all local variables. { int mount; auto int month; } 2> The register storage class is used to define local variables that should be stored in a register instead of RAM.  { register int miles; }

The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values between function calls. The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern', the variable cannot be initialized as all it does is point the variable name at a storage location that has been previously defined.

INPUT C has a pre defined function scanf() to take input. Syntax: scanf(“%x”,&n); For integer For long long scanf(“%d”,&n); scanf(“%lld”,&n); For float For double scanf(“%f”,&n); scanf(“%lf”,&n); For character For string char c; char s[10]; scanf(“%c”,&c); scanf(“%s”,s); For taking one character as input we can use getchar() function. Syntax: char c=getchar();

OUTPUT C has a pre defined function printf() to take input. Syntax: printf(“%x”,n); For integer For long long printf(“%d”,n); printf(“%lld”,n); For float For double printf(“%f”,n); printf(“%lf”,n); For character For string char c; char s[10]; printf(“%c”,c); printf(“%s”,s); For giving one character as output we can use putchar() function. Syntax: char c=‘a’; putchar(c);

SAMPLE PROGRAM #include<stdio.h> /* This is a header file */ int main() /* Here starts the Main Function */ { int a, b, c; /* Declaration of variables */ int r; scanf(“%d”,&r); /* Taking input a=2; b=5; /* Initialization of variables */ c=a+b+r; printf(“Sum is %d”, c); /* Printing */ return 0; }

FUNCTIONS A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions. A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function return_type function_name( parameter list ) { body of the function }

Call Type Description Call by value This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. Call by reference This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.

CONDITIONAL STATEMENTS These are the statements use to check logical conditions and execute the enclosed code depending upon the output YES / NO or TRUE/FALSE There are basically two conditional statements in C: if-else switch-case

If-else statement

Nested if-else statement If (test condition 1) { if (test condition 2) statement 1; } else statement 2; statement 3;

Switch-case statement switch (expression) { case value 1: block 1; break; case value 2: block 2; ………. default: default block; }

Difference between if - else and switch – case It can check only for equality. It can check for equality as well as inequality. It can compare only a variable and a constant. It can compare 2 variables,2 constants and even a variable and a constant. It can compare only integers and characters. It can compare any two types of data.

That’s all folks !!! 