Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
127 views

C Tutorial PDF

The document contains code snippets demonstrating various C operators and format specifiers. It shows examples of: 1) Formatted input/output using format specifiers like %d, %f, %s to print integers, floats, and strings with field width and precision control. 2) Arithmetic, assignment, relational, logical, increment/decrement operators and their usage. 3) Bitwise operators like &, |, ^ along with left and right shift operators. 4) The sizeof operator to check the size of basic data types. 5) Conditional operator ?: and examples of printing values in exponential format using %e format specifier.

Uploaded by

Arivazhagan C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views

C Tutorial PDF

The document contains code snippets demonstrating various C operators and format specifiers. It shows examples of: 1) Formatted input/output using format specifiers like %d, %f, %s to print integers, floats, and strings with field width and precision control. 2) Arithmetic, assignment, relational, logical, increment/decrement operators and their usage. 3) Bitwise operators like &, |, ^ along with left and right shift operators. 4) The sizeof operator to check the size of basic data types. 5) Conditional operator ?: and examples of printing values in exponential format using %e format specifier.

Uploaded by

Arivazhagan C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Formatted IO

#include<stdio.h>
void main()
{
printf("N=%3d\n",10);
printf("N=%2d\n",10);
printf("N=%1d\n",10);
printf("N=%7.2f\n",3.141);
printf("N=%.2f\n",5.1234);
printf("N=%.7f\n",3.141);
printf("N=%f\n",3.14);
printf("N=%06.1f\n",5.5);
printf("%-+6.1f=N\n",5.5);
printf("%-6.3f\n",13.1445);

printf("%s\n","HELLO");
printf("%3s\n","HELLO");
printf("%10s\n","HELLO");
printf("%10.3s\n","HELLO");
}
output
------
N= 10
N=10
N=10
N= 3.14
N=5.12
N=3.1410000
N=3.140000
N=0005.5
+5.5 =N
13.145
HELLO
HELLO
HELLO
HEL

#include<stdio.h>
void main()
{
int a=10;
printf("Value of outer a is=%d",a);
{
int a=20;
printf("Value of inner a is=%d",a);
}
}
#include<stdio.h>
void main()
{
int a,b,c,x,y,z;
int p,q,r;

printf("Enter three integer numbers\n");


scanf("%d %*d %d",&a,&b,&c);
printf("%d %d %d \n\n",a,b,c);

printf("Enter two 4-digit numbers\n");


scanf("%2d %4d",&x,&y);
printf("%d %d\n\n",x,y);

printf("Enter two 2-digit numbers\n");


scanf("%d %d",&a,&x);
printf("%d %d\n\n",a,x);

printf("Enter a nine digit number \n");


scanf("%3d %4d %3d",&p,&q,&r);
printf("%d %d %d \n\n",p,q,r);

printf("Enter two 3-digit numbers\n");


scanf("%d %d",&x,&y);
printf("%d %d",x,y);

#include<stdio.h>
void main()
{
char x;
printf("\t\t\t%d",sizeof(x));
printf("\t\t\t%d",sizeof('x'));
}

//character of ASCII Value


#include <stdio.h>
int main()
{
int chr = 69;
printf("Character having ASCII value 69 is %c.",chr);
return 0;
}
/* Program to declare a character variable d and then set it to be 'd' */
#include <stdio.h>
void main()
{
char c;
c = 'd';
printf("This is my character: %c \n", c);
printf(“This is my ASCII value: %d\n”, c);
}

#include <stdio.h>
int main()
{
char chr;
printf("Enter a character: ");
scanf("%c",&chr);

// When %c text format is used, character is displayed in case of character types


printf("You entered %c.\n",chr);

// When %d text format is used, integer is displayed in case of character types


printf("ASCII value of %c is %d.", chr, chr);
return 0;
}

Operators
// C Program to demonstrate the working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;

c = a+b;
printf("a+b = %d \n",c);

c = a-b;
printf("a-b = %d \n",c);

c = a*b;
printf("a*b = %d \n",c);

c=a/b;
printf("a/b = %d \n",c);

c=a%b;
printf("Remainder when a divided by b = %d \n",c);

return 0;
}

#include<stdio.h>
void main()
{
int a=100;
int b=2;
float c=25.0;
float d=2.0;
float result; //int
printf("\n6+a/5*6=%d\n",6+a/5*6);
printf("a+b*c;a/b*b=%d\n",a+b*c);
printf("c/d*d=%f\n",c/d*d);
printf("-a=%d\n",-a);
result=a/c;
printf("a/c=%f\n",result);
}

#include<stdio.h>
void main()
{ //division by zero not possible
// numerator sign is resultant sign
int a=99;
int b=50;
printf("%d\n",a%b);
printf("%d\n",-a%b);
printf("%d\n",a%-b);
printf("%d\n",-a%-b);

#include<stdio.h>
void main()
{ //value for a, b not assigned then result is garbage value
int a;
int b;
int c=a*b;
printf("%d\n",c);
}

//incrementing constant is not possible


#include<stdio.h>
void main()
{

printf("\n\n\t\t\t%d\n",++2);
}

// C Program to demonstrate the working of increment and decrement operators


#include <stdio.h>
void main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;

printf("++a = %d \n", ++a);

printf("--b = %d \n", --b);

printf("++c = %f \n", ++c);

printf("--d = %f \n", --d);

#include<stdio.h>
void main()
{

int x=5;
printf("\n\n\t\t\tx++=%d\t++x=%d\n",x++,++x);
printf("\n\n\t\t\tx=%d\n",x);
}

// C Program to demonstrate the working of assignment operators


#include <stdio.h>
void main()
{
int a = 5, c;

c = a;
printf("c = %d \n", c);

c += a; // c = c+a
printf("c = %d \n", c);

c -= a; // c = c-a
printf("c = %d \n", c);

c *= a; // c = c*a
printf("c = %d \n", c);

c /= a; // c = c/a
printf("c = %d \n", c);

c %= a; // c = c%a
printf("c = %d \n", c);

// C Program to demonstrate the working of relational operators


#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;

printf("%d == %d = %d \n", a, b, a == b); // true


printf("%d == %d = %d \n", a, c, a == c); // false

printf("%d > %d = %d \n", a, b, a > b); //false


printf("%d > %d = %d \n", a, c, a > c); //false

printf("%d < %d = %d \n", a, b, a < b); //false


printf("%d < %d = %d \n", a, c, a < c); //true

printf("%d != %d = %d \n", a, b, a != b); //false


printf("%d != %d = %d \n", a, c, a != c); //true

printf("%d >= %d = %d \n", a, b, a >= b); //true


printf("%d >= %d = %d \n", a, c, a >= c); //false

printf("%d <= %d = %d \n", a, b, a <= b); //true


printf("%d <= %d = %d \n", a, c, a <= c); //true

return 0;

// C Program to demonstrate the working of logical operators

#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);


printf("(a == b) && (c > b) equals to %d \n", result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) equals to %d \n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) equals to %d \n", result);

result = (a != b) || (c < b);


printf("(a != b) || (c < b) equals to %d \n", result);

result = !(a != b);


printf("!(a == b) equals to %d \n", result);

result = !(a == b);


printf("!(a == b) equals to %d \n", result);

return 0;
}

//Comma operator example


#include <stdio.h>
void main()
{
int num1,num2=1;
num1=num2+1,num2=2;
printf("Num1= %d\n",num1);
}

sizeof Operator
#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
return 0;
}

#include <stdio.h>
void main()
{
int a;
long b;
long long c;
double e;
long double f;

printf("Size of int = %ld bytes \n", sizeof(a));


printf("Size of long = %ld bytes\n", sizeof(b));
printf("Size of long long = %ld bytes\n", sizeof(c));

printf("Size of double = %ld bytes\n", sizeof(e));


printf("Size of long double = %ld bytes\n", sizeof(f));

#include <stdio.h>
void main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%d bytes\n",sizeof(a));
printf("Size of float=%d bytes\n",sizeof(b));
printf("Size of double=%d bytes\n",sizeof(c));
printf("Size of char=%d byte\n",sizeof(d));
printf("Size of 1.55=%d byte\n",sizeof(1.55));
printf("Size of 1.55L=%d byte\n",sizeof(1.55L));
printf("Size of HELLO=%d byte\n",sizeof("HELLO"));
}

C conditional Operator
#include <stdio.h>
int main(){
char February;
int days;
printf("If this year is leap year, enter 1. If not enter any integer: ");
scanf("%c",&February);

// If test condition (February == 'l') is true, days equal to 29.


// If test condition (February =='l') is false, days equal to 28.
days = (February == '1') ? 29 : 28;

printf("Number of days in February = %d",days);


return 0;
}

/* C Program to demonstrate use of bitwise operators */


#include<stdio.h>
int main()
{
unsigned char a = 5, b = 9; // a = 4(00000101), b = 8(00001001)
printf("a = %d, b = %d\n", a, b);
printf("a&b = %d\n", a&b); // The result is 00000001
printf("a|b = %d\n", a|b); // The result is 00001101
printf("a^b = %d\n", a^b); // The result is 00001100
printf("~a = %d\n", a = ~a); // The result is 11111010
printf("b<<1 = %d\n", b<<1); // The result is 00010010
printf("b>>1 = %d\n", b>>1); // The result is 00000100
return 0;
}

Output:
a = 5, b = 9
a&b = 1
a|b = 13
a^b = 12
~a = 250
b<<1 = 18
b>>1 = 4

#include <stdio.h>

main() {

unsigned int a = 60; /* 60 = 0011 1100 */


unsigned int b = 13; /* 13 = 0000 1101 */
int c = 0;

c = a & b; /* 12 = 0000 1100 */


printf("Line 1 - Value of c is %d\n", c );

c = a | b; /* 61 = 0011 1101 */
printf("Line 2 - Value of c is %d\n", c );

c = a ^ b; /* 49 = 0011 0001 */
printf("Line 3 - Value of c is %d\n", c );

c = ~a; /*-61 = 1100 0011 */


printf("Line 4 - Value of c is %d\n", c );

c = a << 2; /* 240 = 1111 0000 */


printf("Line 5 - Value of c is %d\n", c );

c = a >> 2; /* 15 = 0000 1111 */


printf("Line 6 - Value of c is %d\n", c );
}
OUTPUT
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15

C program - Print value in Exponential Format

/*C program to print value in exponential (scientific) format */

#include <stdio.h>
int main()
{
float value=123456.456f;

printf("value: %f (using %%f format specifier)\n",value);


printf("value: %e (using %%e format specifier)\n",value);

return 0;
}

You might also like