Top Banner
Getting Started What you’ll learn: 1. Basic Structure of a C Program 2. Basic Syntax of C language 3. Data Types 4. Defining Variables and Constants 5. Console I/O
25
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1.getting started with c

Getting Started

What you’ll learn:

1. Basic Structure of a C Program

2. Basic Syntax of C language

3. Data Types

4. Defining Variables and Constants

5. Console I/O

Page 2: 1.getting started with c

Getting Started

Basic Structure

Page 3: 1.getting started with c

Basic Structure

A C program basically consists of the following parts:

Pre-processor Commands

Functions

Variables

Statements & Expressions

Comments

Page 4: 1.getting started with c

Getting Started

Basic Syntax

Page 5: 1.getting started with c

Character Set

Letters: A-Z a-z

Digits: 0-9

Symbols: + - * / % = _ | \ ~ ^ / ? ! @ # $ & : ; “ ‘ ` , . < > () {} []

ASCII symbols: 256 characters including the above ones.

Unicode symbols: UTF-8 can represent all 1,114,112 Unicode characters.

Page 6: 1.getting started with c

Tokens

o Keywords

o Identifiers

o Literals

o Strings

o Punctuators

o Operators

Page 7: 1.getting started with c

Keywords

32 Keywords from C89

auto double int struct break else long switch

case enum register typedef char extern return union

const float short unsigned continue for signed void

default goto sizeof volatile do if static while

5 Keywords from C99

_Bool _Imaginary

restrict _Complex

inline

6 Keywords from C11

_Alignas _Atomic

_Noreturn _Alignof

_Generic _Static_assert

Page 8: 1.getting started with c

Identifiers

Rules for defining Identifiers

It can only contain letters, digits, or an underscore.

It should not start with a digit. It can start with a letter or an underscore.

A keyword should not be used as an identifier.

Since C is a case sensitive language so uppercase letters differ from lowercase ones.

Apple apple

Page 9: 1.getting started with c

Valid and Invalid Identifiers

Valid Identifiers:-

VarName VAR2NAME _var11 var_name_11 int_100

Invalid identifiers:-

var-name 11varname int var name$var_name

Page 10: 1.getting started with c

Literals

Literals are also called constants. We have following type of constants:

Integer constant 1, -11, 0xC, 010

Floating constant -32.56, 0.9787

Character constant ‘a’, ‘1’, ‘#’

Page 11: 1.getting started with c

Strings

String literal is a sequence of characters.

E.g. “Hello”

It is terminated by ‘\0’.

E.g. “Hello” will be represented as “Hello\0” in memory.

Page 12: 1.getting started with c

Punctuators

Square brackets [ ]

Parenthesis ( )

Curly Braces { }

Comma ,

Terminator ;

Colon :

Asterisk *

Ellipses …

Equal =

Pre-processor #

Page 13: 1.getting started with c

Basic Operators used in C

Arithmetic: + -   *  /   %

Assignment: =   +=   -=  *=   /= %=

Increment/Decrement: ++ --

Relational: == <= >= < >

Logical: ! &&||

Conditional: ? :

and many more…

Page 14: 1.getting started with c

Getting Started

Data Types

Page 15: 1.getting started with c

Data Types

Fundamental int

float

double

char

void

Derived Arrays

Structure

many more ….

Page 16: 1.getting started with c

For integer types

MODIFIERS SIZE RANGE

Int 4 bytes -2147483648 to 2147483647

Unsigned int 4 bytes 0 to 4294967295

Signed int 4 bytes -2147483648 to 2147483647

Short int 2 bytes -32768 to 32767

Unsigned short int 2 bytes 0 to 65535

Signed short int 2 bytes -32768 to 32767

Long int 4 bytes -2147483648 to 2147483647

Signed long int 4 bytes -2147483648 to 2147483647

Unsigned long int 4 bytes 0 to 4294967295

Page 17: 1.getting started with c

For floating types

MODIFIERS SIZE RANGE

Float 4 bytes -3.4e-38 to +3.4e+38

Double 8 bytes -1.7e-308 to +1.7e+308

Long double 8 bytes -1.7e-308 to +1.7e+308

Page 18: 1.getting started with c

For character types

MODIFIERS SIZE RANGE

Char 1 byte 0 to 255

Unsigned char 1 byte 0 to 255

Signed char 1 byte -128 to +127

Page 19: 1.getting started with c

Getting Started

Variables and Constants

Page 20: 1.getting started with c

Variables

It refers to a storage location in the memory space whose value can be manipulated.

int a = 10;

a = 20;

10

20

value at memory location

data type

identifier

valueof the variable

value changed

Page 21: 1.getting started with c

Constants

It refers to a storage location in the memory space whose value is fixed.

const int a = 10;

a = 20;

10

10

value at memory location

data type

identifier

valueof the variable

value can’t be changed

keyword

Page 22: 1.getting started with c

Getting Started

Console I/O

Page 23: 1.getting started with c

Console Input Output

printf()

scanf()

Page 24: 1.getting started with c

printf()

Used to display formatted output on the console.

Syntax:

printf(“Answer = %d”, i );

Format Specifier

Integer VariableFormatted Text

Page 25: 1.getting started with c

scanf()

Used to take input from the console.

Syntax:

scanf(“%d”, &i );

Format Specifier

Integer VariableAddress of Operator