C Programming
Mid-Level (features of both low & high level), Structural (ability to break program into parts/block), Procedural Oriented programming language developed by Dennis Ritchie in 1972. |
Hello World Program
#include<stdio.h>
int main()
{
printf("Hello World");
return(0);
}
|
Comments in C
Used for documenting code, Compiler ignores the comments in c program. Two Types: |
1. Single Line Comments: Starts with double slash (//) |
2. Multi Line Comments: Starts with /* & Ends with */ |
printf Function
Used to show output on the screen/console. |
printf(format_string, data_to_print);
|
printf("%s", "Pushpender Singh");
|
Format string can be %d (integer), %c (character)** |
%s (string), %f (float), %lf (double) |
scanf Function
Used to take command line input from the user. e.g. |
Syntax: scanf("format string", argument_list);
|
scanf("%d", &num); // Take integer & store it in num variable
|
Data Types in C
A data type specifies the type of data that a variable |
There are 4 Types of DataTypes: |
1. Basic/Fundamental: int, char, float, double |
2. Derived: array, pointer, structure, union |
3. Enumeration Data Type: enum |
4. Void Data Type: void |
|
|
Variables in C
Variable is a Name of Memory Location, Used to Store & Access Data |
Syntax to declare variable: DataType VariableName = Value;
|
|
General Rules for Constructing Variable Name
1. Variable can have alphabets, digits & underscore. |
2. Can start with the alphabet & underscore only. It CANNOT start with a digit. |
3. No whitespace is allowed within the variable name. |
4. CANNOT be any Reserved word or Keyword e.g. int, float, etc. |
Declaring Variable of Other DataTypes
// Create variables
int age = 35;
float percentage = 94.86; // 7 digit Decimal Precision
double pi_value = 3.14159265359; // 15 Decimal Precision
char section = 'A';
// String: Array of Characters
char name[] = "Pushpender Singh";
// Print variables
printf("Age: %d\n", age);
printf("Percentage: %f\n", percentage);
printf("Percentage (Only 1 Decimal): %.1f\n", percentage);
printf("Value of Pi: %lf\n", pi_value);
printf("Section: %c\n", char);
printf("Name: %s\n", name);
|
Data Types
Data Type |
Size |
Description |
int |
2 or 4 bytes |
e.g 20, 30, etc |
float |
4 bytes |
Hold Upto 7 Decimal Digit |
double |
8 bytes |
Hold Upto 15 Decimal Digit |
char |
1 byte |
e.g. 'A', 'B', etc |
Type Casting
// Implicit Casting (Automatic)
float lol = 10; // int to float
printf("%f", lol); // Output: 10.000000
int noice = 95.555 // float to int
printf("%d", noice); // Output: 95
// Explicit Casting (Manual)
float sum = (float) 5 / 2; // int to float
printf("%f", sum); // 2.500000
|
|
|
|