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
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
Getting Started
Basic Structure
Basic Structure
A C program basically consists of the following parts:
Pre-processor Commands
Functions
Variables
Statements & Expressions
Comments
Getting Started
Basic Syntax
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.
Tokens
o Keywords
o Identifiers
o Literals
o Strings
o Punctuators
o Operators
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
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
Valid and Invalid Identifiers
Valid Identifiers:-
VarName VAR2NAME _var11 var_name_11 int_100
Invalid identifiers:-
var-name 11varname int var name$var_name
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’, ‘#’
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.
Punctuators
Square brackets [ ]
Parenthesis ( )
Curly Braces { }
Comma ,
Terminator ;
Colon :
Asterisk *
Ellipses …
Equal =
Pre-processor #
Basic Operators used in C
Arithmetic: + - * / %
Assignment: = += -= *= /= %=
Increment/Decrement: ++ --
Relational: == <= >= < >
Logical: ! &&||
Conditional: ? :
and many more…
Getting Started
Data Types
Data Types
Fundamental int
float
double
char
void
Derived Arrays
Structure
many more ….
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
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
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
Getting Started
Variables and Constants
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
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
Getting Started
Console I/O
Console Input Output
printf()
scanf()
printf()
Used to display formatted output on the console.
Syntax:
printf(“Answer = %d”, i );
Format Specifier
Integer VariableFormatted Text
scanf()
Used to take input from the console.
Syntax:
scanf(“%d”, &i );
Format Specifier
Integer VariableAddress of Operator