Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

C Programming

Data types and Functions


Primitive Data types
Primitive data types
• Int – Whole number
• Float – numbers with a decimal part
• Char – special integer to store single
characters.

ASCII stands for - American Standard Code for Information


Interchange
Modifiers used with primitive Data Types
• Signed, unsigned, and short may be
applied to character and integer data types.
• The modifier long can also be applied to
double
• The range of int or signed int is -32768 to
+32767 and it uses 2 bytes, but sometimes
the program requires only the positive values
and these values may exceed the range of
int.
• In such cases the data type can be made
unsigned by adding the negative range to the
positive one.
• Thus the range of unsigned int becomes: 0 to
65535.
• The unsigned int uses only 2 bytes. Thus we
can say that the unsigned data type uses the
same number of bytes as the signed data.
• The real data types cannot be made
unsigned.
Declaring and Initializing Variables
• In C, a variable is declared by specifying a type name followed by the variable. For example,
int h;
• Declares h to be a variable of type int.
• The declaration allocates space for h but does not initialize it to any value.
• You must not assume that a variable contains any value unless you explicitly assign a value to it.
• You can declare several variables of the same type in one statement as in:
int a, b, c; // declares 3 variables of type int
• The variables are separated by commas, with a semicolon after the last one.
• You can declare a variable and give it an initial value in one statement, as in:
int h = 14;
• This declares h to be int and gives it a value of 14.
• Assigning a variable its first value is called as variable initialization.
Casting
Casting
• Try this Example
Storage Classes -Extern
A storage class defines the scope (visibility) and life-time of variables and/or functions within a C Program.
Storage Classes -Static
Escape Sequences
An escape sequence is a sequence of characters that does not represent itself when used inside a character or string
literal, but is translated into another character or a sequence of characters that may be difficult or impossible to
represent directly.

https://www.geeksforgeeks.org/escape-sequences-c/
Printf and Scanf
• The printf() function is used in the C programming language to display information on the screen. It’s the all-
purpose “Hey, I want to tell the user something” display-text command. It’s the universal electric crayon for
the C language’s scribbling muscles.
• The scanf() function, which is used to snatch a string of text from the keyboard and save it in the cuddly, warm
paws of a string variable.

• To make scanf() work, you need two things. First, you need a storage place to hold the text you
enter. Second, you need the scanf function itself.
• The storage place is called a string variable. String means a string of characters — text. Variable
means that the string isn’t set — it can be whatever the user types.
• A string variable is a storage place for text in your programs.
Printf and Scanf

Try these
Form of C Program

This code use old fashioned C Programming


With ANSI Standard C the style has changed
We will follow the ANSI Standard C Style
Functions
• A function is a part of a program that do a particular task
• It is a reusable independent section from other independent sections in the code
Functions
Function With Values
Function Prototyping
• To write a good code in C, You should declare what type of value
function returns and what type of parameters it accepts in two
places in your code.
1. At the beginning of the code in the global scope. (this is
called function prototyping)
2. In the definition of the function itself

You might also like