C Programing: by Mohammed Tajuddin
C Programing: by Mohammed Tajuddin
BY MOHAMMED TAJUDDIN
C PROGRAMMING
• C Keywords and Identifier
Blank space, newline, horizontal tab, carriage return and form feed.
C PROGRAMMING
• C Keywords
• Keywords are predefined, reserved words used in programming that
have special meanings to the compiler. Keywords are part of the
syntax and they cannot be used as an identifier. For example:
Ex : int money;
C PROGRAMING
• Here, int is a keyword that indicates money is a variable of type int
(integer).
• C Keywords
• auto double int struct
• break else long switch
• case enum register typedef
• char extern return union
• continuefor signed void
• do if staticwhile
• default goto sizeof volatile
• const float shortunsigned
C PROGRAMMING
• C Identifiers
• Identifier refers to name given to entities such as variables, functions,
structures etc.
• int money;
• double accountBalance;
C PROGRAMMING
• Here, money and accountBalance are identifiers.
• The memory size of the basic data types may change according to 32 or 64-bit
operating system.
C PROGAMMING
• Let's see the basic data types. Its size is given according to 32-bit
architecture.
• type variable_list;
C PROGAMMING
• Rules for defining variables
• A variable can have alphabets, digits, and underscore.
• A variable name can start with the alphabet, and underscore only. It
can't start with a digit.
• No whitespace is allowed within the variable name.
• A variable name must not be any reserved word or keyword, e.g. int,
float, etc.
C PROGRAMMING
• Valid variable names:
• int a;
• int _ab;
• int a30;
• Invalid variable names:
• int 2;
• int a b;
• int long;
C PROGRAMMING
• Types of Variables i of variables in c:
• local variable
• global variablen C
• static variable
• automatic variable
• external variable
C PROGRAMMING
• Local Variable
• A variable that is declared inside the function or block is called a local variable.
• void function1(){
• int x=10;//local variable
•}
• You must have to initialize the local variable before it is used.
C PROGRAMMING
• Global Variable
• A variable that is declared outside the function or block is called a global variable. Any
function can change the value of the global variable. It is available to all the functions.
• void function1(){
• int x=10;//local variable
• static int y=10;//static variable
• x=x+1;
• y=y+1;
• printf("%d,%d",x,y);
•}
C PROGRAMMING
• If you call this function many times, the local variable will print the
• same value for each function call, e.g, 11,11,11 and so on. But the
• static variable will print the incremented value in each function call,
• e.g. 11, 12, 13 and so on.
C PROGRAMMING
• Automatic Variable
• All variables in C that are declared inside the block, are automatic
variables by default. We can explicitly declare an automatic variable
using auto keyword.
• void main(){
• int x=10;//local variable (also automatic)
• auto int y=20;//automatic variable
•}
C PROGRAMMING
• External Variable
• We can share a variable in multiple C source files by using an external
variable. To declare an external variable, you need to use extern
keyword.
C PROGRAMMING
• extern int x=10;//external variable (also global)
• program1.c
• #include "myfile.h"
• #include <stdio.h>
• void printValue(){
• printf("Global variable: %d", global_variable);
•}
C PROGRAMMING
• The format specifiers :
• These are used in C for input and output purposes. Using this concept
• the compiler can understand that what type of data is in a variable
• during taking input using the scanf() function and printing using
• printf() function. Here is a list of format specifiers
C PROGRAMMING
• Format Specifier Type
• %c Character
• %d Signed integer
• %e or %E Scientific notation of floats
• %f Float values
• %g or %G Similar as %e or %E
• %hi Signed integer (short)
• %hu Unsigned Integer (short)
• %i Unsigned integer
• %l or %ld or %li Long
C PROGRAMMING
%lf Double
%Lf Long double
%lu Unsigned int or unsigned long
%lli or %lld Long long
%llu Unsigned long long
%o Octal representation
%p Pointer
%s String
%u Unsigned int
%x or %X Hexadecimal representation
%n Prints nothing
%% Prints % character
• #include <stdio.h>
• main() {
• char ch = 'B';
• printf("%c\n", ch); //printing character data
• //print decimal or integer data with d and i
• int x = 45, y = 90;
• printf("%d\n", x);
• printf("%i\n", y);
C PROGRAMMING
• float f = 12.67;
• printf("%f\n", f); //print float value
• printf("%e\n", f); //print in scientific notation
• int a = 67;
• printf("%o\n", a); //print in octal format
• printf("%x\n", a); //print in hex format
C PROGRAMMING
• char str[] = "Hello World";
• printf("%s\n", str);
• printf("%20s\n", str); //shift to the right 20 characters including the
string
• printf("%-20s\n", str); //left align
• printf("%20.5s\n", str); //shift to the right 20 characters including
the string, and print string up to 5 character
• printf("%-20.5s\n", str); //left align and print string up to 5 character
C PROGRAMMING
• constants :
• Fixed value whose value cannot be changed entire execution of
programe.
• Integer constants :
• Decimal
• octal
• Hexa decimal
C PROGRAMMING
Floating point constants
Charecter constants
constants with in single cotations
Ex : ‘1’ , ‘A’ both uppercase and lowercase
C PROGRAMMING
• String constants
• Ex : “robo”, “12345”
C PROGRAMMING
• Pre processor directive constants :