Print “Hello World” in C without using any semicolons

Semicolon is a statement terminator which means it’s the only symbol that tells compiler about the end of a statement.

But,
We still can print “Hello World” without using semicolons using if/while/switch – as they do not terminate with a semicolon, usually.

Here’s how,

Approach #1
1.                                                                                                                          Try it


Approach #2

3.                                                                                                                             Try it


 

Approach #3

2.                                                                                                                                Try it

Actually,  printf is not a void type function, it returns the number of characters printed. If an output error is encountered, a negative value is returned. So, while(!printf(“Hello World”)) – printf returns 11 therefore the condition is false and in return it won’t go in infinite loop.

Also we can use macro

#include<stdio.h>
#define KK  printf(“Hello World\n“)
int main(){
     KK;
}

but here a semicolon is still in use 🙂

Edited:   hmm..you know what! we can use macro to print hello world without a semicolon .. just like this

#include<stdio.h>
#define KK  printf(“Hello World\n“)
int main(){
  ifKK ){ }
}

IF you know another approach, I’d be thankful for sharing it with me.

My name will fly on INSIGHT mission to Mars

Nada
Medhat M.Ibrahim

BOARDING PASS

2nd Mission

http://mars.nasa.gov/participate/send-your-name/insight/?action=getcert&cn=148622735399

 

Want to send your name to Mars too ? Go to: http://mars.nasa.gov/participate/send-your-name/insight/
Last Day to Register: September 8, 2015 (Midnight ET)

Pointers – Part 1

Why Have Pointers ?

Regardless pointers allow new and more ugly types of bugs. Pointers solve two common problems. First, pointers allow different sections of code to share information. Second, pointers enable complex “linked” data structures like Linked Lists and Binary Trees.

What Is A Pointer ? 

A Pointer is a variable which stores the memory address of a variable .

Address of a Variable:   Variable address is a unique number which tells where the variable is stored in the memory that means there are no two variables have the same address.

Declaration:

Pointers are declared like other types .. just add   before the variable name.

     double* Ptr;   // Ptr is a pointer points to a double  variable .
     int sum = 7;
     int* P1 , *P2 , v1 , v2;

– P1 and P2 hold pointers to an int variables.
sum , v1 and v2 are ordinary static variables.

     A variable name directly references a value, and a pointer indirectly references a value.

The & Operator – Reference to:

The address operator   determines “address of ” a variable.
 int* P1 , P2 , v1 , v2;
 P1 = &v1;  

Read like this :

–”p1 equals address of v1“.

pointers2
–Or “p1 points to v1”.

 

 

 

 

 

The * Operator – Dereference:

pointers1
The dereference operator is used to access the value of the pointee.
– Pointer variable “dereferenced” means : ” get the data that p1 points to ” .
The pointer must have a pointee or it’s a runtime error.

The NULL Pointer:

The constant NULL is a special pointer value which encodes the idea of “points to nothing.”  It is a runtime error to dereference a NULL pointer.

Pointer Assignment:

The assignment operation (=) between two pointers makes them point to the same pointee.
**The following code shows two variables: num and numPtr.  num is a simple static variable and numPtr is its pointer, Then we added a second pointer variable named second points to what numPtr points to (num).

 int num = 42;

int* numPtr , *second;

pointers3numPtr = &num;

second = numPtr;

Sharing:

Two pointers which both refer to a single pointee are said to be “sharing”.

Shallow and Deep Copying:

p2 = p1;                 “Shallow Copy / Sharing
-Assigns one pointer to another.
“Make p2 point to where p1 points.”

*p2 = *p1;            “Deep Copying / Copying
-Assigns “Value pointed to by p1 ,to Value pointed to by p2.”

Bad Pointers:

When a pointer is first allocated, it does not have a pointee. The pointer is “uninitialized”  or simply “bad”. Bad pointers are very common. In fact, every pointer starts out with a bad value.
A dereference operation on a bad pointer is a serious runtime error. so, if your code is crashing, a bad pointer should be your first suspicion.

void BadPointer()  {
  int* p;    // allocate the pointer, but not the pointee
*p = 42;  // this dereference is a serious runtime error
}
// What happens at runtime the bad pointer is dereferenced …..

pointers5

Pointer Rules Summary:
No matter how complex a pointer structure gets, the list of rules remains short.

  • A pointer stores a reference to its pointee. The pointee, in turn, stores something useful.
  • The dereference operation on a pointer accesses its pointee. A pointer may only be dereferenced after it has been assigned to refer to a pointee. Most pointer bugs involve violating this one rule.
  • Allocating a pointer does not automatically assign it to refer to a pointee. Assigning the pointer to refer to a specific pointee is a separate operation which is easy to forget.
  • Assignment between two pointers makes them refer to the same pointee which introduces sharing.

Pointers can be used with Static Variables as mentioned earlier, to store the address of the variable in the pointer.
But How to use Pointers to declare a Dynamic Variable:

Assume we want to declare a Dynamic Variable of type float . So, we need to declare a pointer of type float:

First, we declare the pointer of type float

                            float* fPtr;

Second, we will use it to declare the variable:
→ We will use the operator new .

                        fPtr = new float ;

 /* in this line we create new “nameless” variable, and assigns fPtr to “point to” it.
Can be accessed with *fPtr just like an ordinary variable */

We can initialize this nameless variable:
                float* fPtr = new float (17) ;   // Initializes *fPtr to 17

               double* dPtr ;
               dPtr = new double (57.3) ;   // Initializes *dPtr tp 57.3

 

The Memory Management
The Heap ( freestore ) :

Heap” memory, also known as “dynamic” memory, is an alternative to local stack memory.
NOTE:  Static Variables are stored in the local stack memory , While Dynamic Variables are stores in the Heap .

Local memory is quite automatic — it is allocated automatically when a program executed and it is deallocated automatically when a program exits. Heap memory is different in every way. The programmer explicitly requests the allocation of a memory “block” of a particular size, and the block continues to be allocated until the programmer explicitly requests that it be deallocated. Nothing happens automatically.

Heap reserved for dynamically allocated variable , all new dynamic variable consumes memory in freestore.
If too many → could use all freestore memory . In turn, future “new” operations will fail if the freestore is full.

The heap may be of a fixed size, or it may appear to be of a fixed but extremely large size backed by virtual memory.
In either case, it is possible for the heap to get “full” if all of its memory has been allocated and so it cannot satisfy an allocation request.

So, if we have allocated dynamic variables, we have to deallocate the memory in the heap after the program exits.  HOW? Using the delete operator

     – Example:    

                                int *p;
                                        p = new int (5) ;
                                … //Some processing
                               delete p;


De-allocates the dynamic memory “pointed to by p ” .

Dangling Pointers:

delete p ;
– Destroys dynamic memory .
– But p still points there !
♦ called ” dangling pointer ” .
– If p is then dereferenced ( *p ) .
♦ unpredictable results !

Avoiding dangling pointers
– Assign pointer to NULL after delete:
delete p ;
p = NULL ;

Heap Memory Summary:

Heap memory provides greater control for the programmer — the blocks of memory can be requested in any size, and they remain allocated until they are deallocated explicitly.
Heap memory can be passed back to the caller since it is not deallocated on exit, and it can be used to build linked structures such as linked lists and binary trees. The disadvantage of heap memory is that the program must make explicit allocation and deallocate calls to manage the heap memory. The heap memory does not operate automatically and conveniently the way local memory does.

 

→ To Be Continued in Part 2 .